CIS 35A: Introduction to Java Programming

Home | Green Sheet | Lectures | Assignments | FAQ | Grades

XML

XML
StAX
Create XMLStreamReader obje

Classes for creating an XMLStreamReader object

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

Common methods of the XMLInputFactory class

Method Description
newInstance() A static method that returns an XMLInputFactory object.
createXMLStreamReader(in) Returns an XMLStreamReader object for the specified Reader or StreamInput object. This method throws an XMLStreamException.

Code that creates an XMLStreamReader 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 (XMLStreamException e)
{
    e.printStackTrace();
}
Previous | Create XMLStreamWriter object | Write XML | Create XMLStreamReader object | Read XML | Class that works with an XML file