Example usage for javax.xml.stream XMLEventReader hasNext

List of usage examples for javax.xml.stream XMLEventReader hasNext

Introduction

In this page you can find the example usage for javax.xml.stream XMLEventReader hasNext.

Prototype

@Override
public boolean hasNext();

Source Link

Document

Check if there are more events.

Usage

From source file:XMLEventReaderDemo.java

public static void main(String[] args) throws Exception {
    XMLInputFactory factory = XMLInputFactory.newInstance();
    Reader fileReader = new FileReader("Source.xml");
    XMLEventReader reader = factory.createXMLEventReader(fileReader);

    while (reader.hasNext()) {
        XMLEvent event = reader.nextEvent();
        if (event.isStartElement()) {
            StartElement element = (StartElement) event;
            System.out.println("Start Element: " + element.getName());

            Iterator iterator = element.getAttributes();
            while (iterator.hasNext()) {
                Attribute attribute = (Attribute) iterator.next();
                QName name = attribute.getName();
                String value = attribute.getValue();
                System.out.println("Attribute name/value: " + name + "/" + value);
            }//  ww  w. jav  a  2s  . c om
        }
        if (event.isEndElement()) {
            EndElement element = (EndElement) event;
            System.out.println("End element:" + element.getName());
        }
        if (event.isCharacters()) {
            Characters characters = (Characters) event;
            System.out.println("Text: " + characters.getData());
        }
    }
}

From source file:Main.java

public static void main(String args[]) throws Exception {
    XMLInputFactory xmlif = XMLInputFactory.newInstance();
    XMLEventReader xmler = xmlif.createXMLEventReader(new FileReader("yourFile.xml"));
    XMLEvent event;/* w w w. j ava 2  s .  c  om*/
    while (xmler.hasNext()) {
        event = xmler.nextEvent();
        if (event.isStartElement()) {
            System.out.println(event.asStartElement().getName());
        } else if (event.isCharacters()) {
            System.out.println(event.asCharacters().getData());
        }
    }
}

From source file:Main.java

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

    XMLEventReader xmlEventReader = xmlInputFactory.createXMLEventReader(new FileInputStream("input.xml"));
    InterestingElementFilter filter = new InterestingElementFilter();
    XMLEventReader interestingElementReader = xmlInputFactory.createFilteredReader(xmlEventReader, filter);

    while (interestingElementReader.hasNext()) {
        XMLEvent xmlEvent = interestingElementReader.peek();
        if (xmlEvent.isStartElement()) {
            System.out.println(xmlEvent.asStartElement().getName());
        }/*ww w  . ja v a2 s  .c  o  m*/
        interestingElementReader.next();
    }
}

From source file:Main.java

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

    XMLInputFactory factory = XMLInputFactory.newInstance();
    System.out.println("FACTORY: " + factory);

    XMLEventReader r = factory.createXMLEventReader(filename, new FileInputStream(filename));

    while (r.hasNext()) {
        XMLEvent e = r.nextEvent();
        System.out.println(e.toString());
    }//from ww  w. ja  v a2  s. com
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    XMLInputFactory xif = XMLInputFactory.newInstance();
    XMLEventReader xmlr = xif.createXMLEventReader((new FileInputStream(new File("./file.xml"))));

    boolean inline = false;
    StringBuffer sb = new StringBuffer();
    while (xmlr.hasNext()) {
        XMLEvent event = xmlr.nextEvent();

        if (event.isStartElement()) {
            StartElement element = (StartElement) event;
            if ("data".equals(element.getName().toString().trim())) {
                inline = true;//from   www .  ja  va 2  s. co m
            }
        }

        if (inline) {
            sb.append(xmlr.peek());
        }

        if (event.isEndElement()) {
            EndElement element = (EndElement) event;
            if ("data".equals(element.getName().toString().trim())) {
                inline = false;
                System.out.println(sb.toString());
                sb.setLength(0);
            }
        }
    }
}

From source file:StaxEvent.java

public static void main(String[] args) throws Exception {
    StaxEvent ms = new StaxEvent();

    XMLEventReader reader = XMLInputFactory.newInstance()
            .createXMLEventReader(new java.io.FileInputStream("yourXML.xml"));
    XMLEventWriter writer = XMLOutputFactory.newInstance().createXMLEventWriter(System.out);

    while (reader.hasNext()) {
        XMLEvent event = (XMLEvent) reader.next();

        if (event.getEventType() == event.CHARACTERS) {
            writer.add(ms.getNewCharactersEvent(event.asCharacters()));
        } else {/*w w  w .j  a  v a 2  s .c  o m*/
            writer.add(event);
        }
    }
    writer.flush();
}

From source file:org.eclipse.swordfish.core.configuration.xml.SaxParsingPrototype.java

/**
 * @param args/*from   ww  w.ja v a 2s . c  o  m*/
 */
public static void main(String[] args) throws Exception {
    XMLInputFactory inputFactory = XMLInputFactory.newInstance();
    LinkedList<String> currentElements = new LinkedList<String>();
    InputStream in = SaxParsingPrototype.class.getResource("ComplexPidXmlProperties.xml").openStream();
    XMLEventReader eventReader = inputFactory.createXMLEventReader(in);
    Map<String, List<String>> props = new HashMap<String, List<String>>();
    // Read the XML document
    while (eventReader.hasNext()) {
        XMLEvent event = eventReader.nextEvent();
        if (event.isCharacters() && !event.asCharacters().isWhiteSpace()) {
            putElement(props, getQualifiedName(currentElements), event.asCharacters().getData());

        } else if (event.isStartElement()) {
            System.out.println(event.asStartElement().getName());
            currentElements.add(event.asStartElement().getName().getLocalPart());
            for (Iterator attrIt = event.asStartElement().getAttributes(); attrIt.hasNext();) {
                Attribute attribute = (Attribute) attrIt.next();
                putElement(props, getQualifiedName(currentElements) + "[@" + attribute.getName() + "]",
                        attribute.getValue());

            }
        } else if (event.isAttribute()) {
        } else if (event.isEndElement()) {
            String lastElem = event.asEndElement().getName().getLocalPart();
            if (!currentElements.getLast().equals(lastElem)) {
                throw new UnsupportedOperationException(lastElem + "," + currentElements.getLast());
            }
            currentElements.removeLast();
        }
    }

    eventReader.close();
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    File source = new File(args[0]);
    File target = new File(source + ".new");

    XMLInputFactory inputFactory = XMLInputFactory.newInstance();
    XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
    InputStream in = new FileInputStream(source);
    XMLEventReader reader = inputFactory.createXMLEventReader(in);

    OutputStream out = new FileOutputStream(target);
    XMLEventWriter writer = outputFactory.createXMLEventWriter(out);
    XMLEvent event;//from  w w w  .ja va 2s .co m

    boolean deleteSection = false;
    while (reader.hasNext()) {
        event = reader.nextEvent();
        if (event.getEventType() == XMLStreamConstants.START_ELEMENT
                && event.asStartElement().getName().toString().equalsIgnoreCase("companies")) {
            deleteSection = true;
            continue;
        } else if (event.getEventType() == XMLStreamConstants.END_ELEMENT
                && (event.asEndElement().getName().toString().equalsIgnoreCase("companies"))) {
            deleteSection = false;
            continue;
        } else if (deleteSection) {
            continue;
        } else {
            writer.add(event);
        }
    }
}

From source file:MainClass.java

public static void main(String[] args) throws Exception {
    XMLInputFactory inputFactory = XMLInputFactory.newInstance();
    XMLEventReader reader = inputFactory.createXMLEventReader(new FileInputStream(new File("a.xml")));
    reader = inputFactory.createFilteredReader(reader, new EventFilter() {
        public boolean accept(XMLEvent event) {
            return false;
        }//  w ww . j a v  a  2 s .c  o  m
    });
    reader = inputFactory.createFilteredReader(reader, new ElementOnlyFilter());

    System.out.println(reader.hasNext());
    System.out.println(reader.next());
}

From source file:Main.java

public static String digest(InputStream in) throws Exception {
    MessageDigest messageDigest = null;
    XMLInputFactory inputFactory = XMLInputFactory.newFactory();
    messageDigest = MessageDigest.getInstance("MD5");
    XMLEventReader eventReader = inputFactory.createXMLEventReader(in);
    while (eventReader.hasNext()) {
        XMLEvent event = eventReader.nextEvent();

        if (event.isStartElement()) {
            messageDigest.update(event.asStartElement().getName().toString().getBytes());
        } else if (event.isEndElement()) {
            messageDigest.update(event.asEndElement().getName().toString().getBytes());
        }//from  ww  w.j  a  va 2 s.c om
    }
    StringBuffer result = new StringBuffer();
    byte[] digest = messageDigest.digest();
    for (byte b : digest) {
        result.append(Integer.toString((b & 0xff) + 0x100, 16).substring(1));
    }
    return result.toString();
}