Example usage for javax.xml.stream XMLInputFactory createXMLEventReader

List of usage examples for javax.xml.stream XMLInputFactory createXMLEventReader

Introduction

In this page you can find the example usage for javax.xml.stream XMLInputFactory createXMLEventReader.

Prototype

public abstract XMLEventReader createXMLEventReader(java.io.InputStream stream) throws XMLStreamException;

Source Link

Document

Create a new XMLEventReader from a java.io.InputStream

Usage

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  a  v  a2s . 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: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 va  2 s  .c  om
    });
    reader = inputFactory.createFilteredReader(reader, new ElementOnlyFilter());

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

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   w  ww  .j a  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: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());
        }/*from w ww . ja v  a  2  s  .  c  om*/
        interestingElementReader.next();
    }
}

From source file:EventFilterExample.java

public static void main(String[] args) throws Exception {
    File file = new File("text.xml");

    XMLInputFactory inputFactory = XMLInputFactory.newInstance();
    XMLEventReader reader = inputFactory.createXMLEventReader(new FileInputStream(file));

    System.out.println("Unfiltered Count = " + countEvents(reader));

    reader = inputFactory.createXMLEventReader(new FileInputStream(file));

    EventFilter filter = new ElementOnlyFilter();
    reader = inputFactory.createFilteredReader(reader, filter);

    System.out.println("Filtered Count = " + countEvents(reader));
}

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);
            }//  w  w w  .  ja va 2  s  .  c  o m
        }
        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 {
    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;/*w ww . j a  v  a  2s  .c  om*/

    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:org.eclipse.swordfish.core.configuration.xml.SaxParsingPrototype.java

/**
 * @param args//from  w  w w  .  j a  v  a2 s.  co 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 {
    XMLInputFactory inputFactory = XMLInputFactory.newInstance();
    inputFactory.setXMLResolver(new XMLResolver() {
        public Object resolveEntity(String publicID, String systemID, String baseURI, String namespace)
                throws XMLStreamException {
            System.out.println(systemID);
            return null;
        }/*from  w w  w . j  a  va2 s.c o m*/
    });

    XMLEventReader reader = inputFactory.createXMLEventReader(new FileInputStream("dtd.xml"));
    XMLEventWriter writer = XMLOutputFactory.newInstance().createXMLEventWriter(System.out);
    writer.add(reader);
    writer.flush();

}

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  w  w w .  j a va  2s  . 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();
}