Example usage for javax.xml.stream XMLStreamReader isStartElement

List of usage examples for javax.xml.stream XMLStreamReader isStartElement

Introduction

In this page you can find the example usage for javax.xml.stream XMLStreamReader isStartElement.

Prototype

public boolean isStartElement();

Source Link

Document

Returns true if the cursor points to a start tag (otherwise false)

Usage

From source file:Main.java

public static void main(String[] args) throws Exception {
    XMLInputFactory xif = XMLInputFactory.newFactory();

    FileInputStream xml = new FileInputStream("input.xml");
    XMLStreamReader xsr = xif.createXMLStreamReader(xml);
    xsr.nextTag(); // Advance to "Persons" tag
    xsr.nextTag(); // Advance to "Person" tag

    JAXBContext jc = JAXBContext.newInstance(Person.class);
    Unmarshaller unmarshaller = jc.createUnmarshaller();
    List<Person> persons = new ArrayList<Person>();
    while (xsr.hasNext() && xsr.isStartElement()) {
        Person person = (Person) unmarshaller.unmarshal(xsr);
        persons.add(person);/*from  w  ww  .  ja va2s.  co m*/
        xsr.nextTag();
    }

    for (Person person : persons) {
        System.out.println(person.getName());
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    String filename = "yourXML.xml";

    XMLInputFactory xmlif = null;

    xmlif = XMLInputFactory.newInstance();
    xmlif.setProperty(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES, Boolean.TRUE);
    xmlif.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, Boolean.FALSE);
    xmlif.setProperty(XMLInputFactory.IS_NAMESPACE_AWARE, Boolean.TRUE);
    xmlif.setProperty(XMLInputFactory.IS_COALESCING, Boolean.TRUE);

    System.out.println("FACTORY: " + xmlif);
    System.out.println("filename = " + filename);

    FileInputStream fis = new FileInputStream(filename);

    XMLStreamReader xmlr = xmlif.createFilteredReader(xmlif.createXMLStreamReader(fis), new Main());

    int eventType = xmlr.getEventType();
    printEventType(eventType);// w  w  w. j  a v a2  s. c o  m

    while (xmlr.hasNext()) {
        eventType = xmlr.next();
        printEventType(eventType);
        printName(xmlr, eventType);
        printText(xmlr);

        if (xmlr.isStartElement()) {
            printAttributes(xmlr);
        }
        printPIData(xmlr);
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    String filename = "yourXML.xml";

    XMLInputFactory xmlif = null;

    xmlif = XMLInputFactory.newInstance();
    xmlif.setProperty(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES, Boolean.TRUE);
    xmlif.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, Boolean.FALSE);
    xmlif.setProperty(XMLInputFactory.IS_NAMESPACE_AWARE, Boolean.TRUE);
    xmlif.setProperty(XMLInputFactory.IS_COALESCING, Boolean.TRUE);

    System.out.println("FACTORY: " + xmlif);
    System.out.println("filename = " + filename);

    FileInputStream fis = new FileInputStream(filename);

    XMLStreamReader xmlr = xmlif.createFilteredReader(xmlif.createXMLStreamReader(fis), new MyFilter());

    int eventType = xmlr.getEventType();
    printEventType(eventType);/*from  w w  w  . j  a  v a 2s .  c om*/

    while (xmlr.hasNext()) {
        eventType = xmlr.next();
        printEventType(eventType);
        printName(xmlr, eventType);
        printText(xmlr);

        if (xmlr.isStartElement()) {
            printAttributes(xmlr);
        }
        printPIData(xmlr);
    }
}

From source file:MyStreamFilter.java

public static void main(String[] args) throws Exception {
    String filename = "yourXML.xml";

    XMLInputFactory xmlif = null;

    xmlif = XMLInputFactory.newInstance();
    xmlif.setProperty(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES, Boolean.TRUE);
    xmlif.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, Boolean.FALSE);
    xmlif.setProperty(XMLInputFactory.IS_NAMESPACE_AWARE, Boolean.TRUE);
    xmlif.setProperty(XMLInputFactory.IS_COALESCING, Boolean.TRUE);

    System.out.println("FACTORY: " + xmlif);
    System.out.println("filename = " + filename);

    FileInputStream fis = new FileInputStream(filename);

    XMLStreamReader xmlr = xmlif.createFilteredReader(xmlif.createXMLStreamReader(fis), new MyStreamFilter());

    int eventType = xmlr.getEventType();
    printEventType(eventType);/*from   ww w.j a  v a 2s.c  om*/

    while (xmlr.hasNext()) {
        eventType = xmlr.next();
        printEventType(eventType);
        printName(xmlr, eventType);
        printText(xmlr);

        if (xmlr.isStartElement()) {
            printAttributes(xmlr);
        }
        printPIData(xmlr);
    }
}

From source file:Main.java

public static void printStartElement(XMLStreamReader xmlr) {
    if (xmlr.isStartElement()) {
        System.out.print("<" + xmlr.getName().toString());
        printAttributes(xmlr);/*w ww. j  a  v  a 2  s.  c o m*/
        // System.out.print(</codeTitle><cnTitle></cnTitle><codeKeywords></codeKeywords><codeComments></codeComments>");
    }
}

From source file:Main.java

public static void skipToEndElement(XMLStreamReader in) throws XMLStreamException {
    if (!in.isStartElement()) {
        return;/* w  ww  .ja  v  a  2 s .c o m*/
    }

    int tagDepth = 1;
    while (tagDepth > 0 && in.hasNext()) {
        in.next();
        if (in.isStartElement()) {
            tagDepth++;
        } else if (in.isEndElement()) {
            tagDepth--;
        }
    }
}

From source file:Main.java

private static void skipStartDocument(XMLStreamReader parser) throws XMLStreamException {
    while (!parser.isStartElement()) {
        parser.nextTag();/*from  w  w  w.  jav a 2  s  .  co m*/
    }
}

From source file:StaxCursorTest.java

private static void printStartElement(XMLStreamReader xmlr) {
    if (xmlr.isStartElement()) {
        System.out.print("<" + xmlr.getName().toString());
        printAttributes(xmlr);/*from w w  w  .  j  a va  2 s  . co  m*/
        System.out.print(">");
    }
}

From source file:Main.java

public static void skipToStart(XMLStreamReader xmlRdr, String elementName) throws XMLStreamException {
    while (xmlRdr.hasNext()) {
        if (xmlRdr.isStartElement() && xmlRdr.getLocalName().equals(elementName)) {
            break;
        }//from   w  w w . ja v a2 s  .c  om
        xmlRdr.next();
    }
}

From source file:Main.java

public static boolean isStartElement(XMLStreamReader xmlRdr, String tagName) {
    //return isElement(xmlRdr, XMLStreamConstants.START_ELEMENT, tagName);

    return xmlRdr.isStartElement() && xmlRdr.getLocalName().equals(tagName);
}