Example usage for javax.xml.stream XMLInputFactory newInstance

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

Introduction

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

Prototype

public static XMLInputFactory newInstance() throws FactoryConfigurationError 

Source Link

Document

Creates a new instance of the factory in exactly the same manner as the #newFactory() method.

Usage

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:CursorApproachEventObject.java

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

    XMLInputFactory xmlif = XMLInputFactory.newInstance();
    xmlif.setEventAllocator(new XMLEventAllocatorImpl());
    allocator = xmlif.getEventAllocator();

    XMLStreamReader xmlr = xmlif.createXMLStreamReader(filename, new FileInputStream(filename));

    int eventType = xmlr.getEventType();

    while (xmlr.hasNext()) {
        eventType = xmlr.next();//from   w  w  w . java 2 s . c om
        if ((eventType == XMLStreamConstants.START_ELEMENT) && xmlr.getLocalName().equals("Book")) {
            StartElement event = getXMLEvent(xmlr).asStartElement();
            System.out.println("EVENT: " + event.toString());
        }
    }
}

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  www. j av a 2  s .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:StAXTest.java

public static void main(String[] args) throws Exception {
    String urlString;//  ww  w  .  ja  v  a2s  . c om
    if (args.length == 0) {
        urlString = "http://www.w3c.org";
        System.out.println("Using " + urlString);
    } else
        urlString = args[0];
    URL url = new URL(urlString);
    InputStream in = url.openStream();
    XMLInputFactory factory = XMLInputFactory.newInstance();
    XMLStreamReader parser = factory.createXMLStreamReader(in);
    while (parser.hasNext()) {
        int event = parser.next();
        if (event == XMLStreamConstants.START_ELEMENT) {
            if (parser.getLocalName().equals("a")) {
                String href = parser.getAttributeValue(null, "href");
                if (href != null)
                    System.out.println(href);
            }
        }
    }
}

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

/**
 * @param args/*from w  ww. ja va2 s .  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

private static void createXMLInputFactory() {
    // Create the factory
    xmlInputFactory = XMLInputFactory.newInstance();
    xmlInputFactory.setProperty(XMLInputFactory.IS_VALIDATING, false);
    xmlInputFactory.setProperty(XMLInputFactory.IS_NAMESPACE_AWARE, false);
}

From source file:Main.java

public static XMLStreamReader getXMLStreamReader(InputStream inputStream) {
    XMLInputFactory xmlInputFactory = XMLInputFactory.newInstance();
    return createXmlStreamReader(xmlInputFactory, inputStream);
}

From source file:Main.java

public static XMLStreamReader buildXmlStreamReader(String xml) throws XMLStreamException {
    XMLInputFactory factory = XMLInputFactory.newInstance();
    return factory.createXMLStreamReader(new StringReader(xml));
}

From source file:Main.java

/**
 * StAX parse XML resource into XMLStreamReader
 * @return XMLStreamReader/*from w w w  .  ja  va  2s . com*/
 * @throws IOException
 * @throws XMLStreamException
 */
public static XMLStreamReader parse(URL url) throws IOException, XMLStreamException {
    InputStream input = url.openStream();
    XMLInputFactory xif = XMLInputFactory.newInstance();
    XMLStreamReader xmlStreamReader = xif.createXMLStreamReader(input);

    return xmlStreamReader;
}

From source file:Main.java

public static XMLStreamReader parse(File xmlFile) throws IOException, XMLStreamException {
    FileReader input = new FileReader(xmlFile);
    XMLInputFactory xif = XMLInputFactory.newInstance();
    XMLStreamReader xmlStreamReader = xif.createXMLStreamReader(input);

    return xmlStreamReader;
}