How to create an XMLStreamReader object

Classes for creating an XMLSreamReader object:

  • javax.xml.stream.XMLInputFactory
  • javax.xml.stream.XMLStreamReader
  • javax.xml.stream.XMLStreamException

Common constructors of the xxxx classes:

Constructors Description
newInstance()

A static method that returns an XMLInputFactory object.

createXMLStreamReader(in) Returns an XLStreamReader object fot the specified Reader or StreamInput object.

Example 1: Code that creates an XLMStreamReader object

// create the XMLInputFactory object
XMLInputFactory INPUTfACTORY = XMLInputFactory.newInstance();
try
{
// create a XMLStreamReader object
FileReader fileReader = new FileReader("products.xml");
XMLStreamReader reader = inputFactory.createXMLStreamREader(fileReader);
// Read XML here.
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
catch (XMLStreaException e)
{
e.printStackTrace();
}

Descriptions:

  • To read XML from a file, you can supply a FileReader object as the argument for the create XMLStreamReader method.

More info can be found here: