Example usage for javax.xml.stream XMLInputFactory createXMLStreamReader

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

Introduction

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

Prototype

public abstract XMLStreamReader createXMLStreamReader(java.io.InputStream stream) throws XMLStreamException;

Source Link

Document

Create a new XMLStreamReader from a java.io.InputStream

Usage

From source file:Main.java

public static XMLStreamReader parse(String fileName) throws IOException, XMLStreamException {
    FileInputStream input = new FileInputStream(fileName);
    XMLInputFactory xif = XMLInputFactory.newInstance();
    XMLStreamReader xmlStreamReader = xif.createXMLStreamReader(input);

    return xmlStreamReader;
}

From source file:Main.java

/**
 * StAX parse XML resource into XMLStreamReader
 * @return XMLStreamReader/*from ww w  .  ja  v a2  s  .  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

/**
 * Creates {@link javax.xml.stream.XMLStreamReader} of a {@link InputStream}. The start document is already skipped.
 * //from   w  w w.j a v  a 2 s . co  m
 * @param inputStream
 *            never <code>null</code>
 * @return reader the {@link XMLStreamReader} from the passed {@link InputStream}, never <code>null</code>
 * @throws XMLStreamException
 *             - input stream could not be parsed as xml
 */
public static XMLStreamReader createReader(InputStream inputStream) throws XMLStreamException {
    XMLInputFactory factory = XMLInputFactory.newInstance();
    XMLStreamReader reader = factory.createXMLStreamReader(inputStream);
    skipStartDocument(reader);
    return reader;
}

From source file:Main.java

/**
 * Determines if the given stream contains XML content. The stream will be
 * buffered and reset if necessary.//from w w w  . j  a v a  2 s .  c o  m
 * 
 * @param stream
 *            The InputStream to read.
 * @return true if the stream contains XML content; false otherwise.
 */
public static boolean isXML(InputStream stream) {
    if (!stream.markSupported()) {
        stream = new BufferedInputStream(stream, 1024);
    }
    stream.mark(1024);
    byte[] bytes = new byte[1024];
    try {
        try {
            stream.read(bytes);
        } finally {
            stream.reset();
        }
    } catch (IOException iox) {
        throw new RuntimeException("Failed to read or reset stream. " + iox.getMessage());
    }
    try {
        XMLInputFactory factory = XMLInputFactory.newInstance();
        XMLStreamReader reader = factory.createXMLStreamReader(new ByteArrayInputStream(bytes));
        // If XML, now in START_DOCUMENT state; seek document element.
        reader.nextTag();
    } catch (XMLStreamException xse) {
        return false;
    }
    return true;
}

From source file:Main.java

private static XMLStreamReader createXmlStreamReader(XMLInputFactory xmlInputFactory, InputStream inputStream) {
    XMLStreamReader xmlStreamReader = null;
    try {/*from   w w w.  ja  v  a2 s  .c  o m*/
        xmlStreamReader = xmlInputFactory.createXMLStreamReader(inputStream);
    } catch (XMLStreamException e) {
        e.printStackTrace();
    }
    return xmlStreamReader;
}

From source file:de.bloxel.engine.util.JAXBUtils.java

public static <T> T unmarschal(final InputStream inputStream, final Class<T> aClass) {
    try {/*from ww  w.  j a  v  a  2  s .com*/
        // http://jaxb.java.net/faq/index.html#classloader
        final JAXBContext jc = JAXBContext.newInstance(getPackageCanonicalName(aClass));
        final Unmarshaller unmarshaller = jc.createUnmarshaller();
        // http://jaxb.java.net/guide/Unmarshalling_is_not_working__Help_.html
        unmarshaller.setEventHandler(new javax.xml.bind.helpers.DefaultValidationEventHandler());
        final XMLInputFactory staxFactory = XMLInputFactory.newInstance();
        final XMLStreamReader xmlReader = staxFactory.createXMLStreamReader(inputStream);
        return unmarshaller.unmarshal(xmlReader, aClass).getValue();
    } catch (final JAXBException e) {
        throw new RuntimeException(String.format("Can't load unmarschal '%s'", aClass), e);
    } catch (final XMLStreamException e) {
        throw new RuntimeException(String.format("Can't load unmarschal '%s'", aClass), e);
    }
}

From source file:edu.harvard.i2b2.timeline.lifelines.QueryClient.java

public static OMElement getQueryPayLoad(String queryXML) throws XMLStreamException {
    OMFactory fac = OMAbstractFactory.getOMFactory();
    OMNamespace omNs = fac.createOMNamespace("http://mgh.harvard.edu/i2b2", "");
    OMElement method = fac.createOMElement("queryPatient", omNs);

    StringReader strReader = new StringReader(queryXML);
    XMLInputFactory xif = XMLInputFactory.newInstance();
    XMLStreamReader reader = xif.createXMLStreamReader(strReader);

    StAXOMBuilder builder = new StAXOMBuilder(reader);
    OMElement lineItem = builder.getDocumentElement();
    method.addChild(lineItem);/*w ww  .  jav  a 2 s  .co m*/
    return method;
}

From source file:edu.harvard.i2b2.pm.ws.PMServiceDriver.java

/**
 * Function to convert pm requestVdo to OMElement
 * //from   ww w . java 2s .com
 * @param requestPm   String request to send to pm web service
 * @return An OMElement containing the pm web service requestVdo
 */
public static OMElement getPmPayLoad(String requestPm) throws Exception {
    OMElement lineItem = null;
    try {
        StringReader strReader = new StringReader(requestPm);
        XMLInputFactory xif = XMLInputFactory.newInstance();
        XMLStreamReader reader = xif.createXMLStreamReader(strReader);

        StAXOMBuilder builder = new StAXOMBuilder(reader);
        lineItem = builder.getDocumentElement();
    } catch (FactoryConfigurationError e) {
        log.error(e.getMessage());
        throw new Exception(e);
    }
    return lineItem;
}

From source file:edu.harvard.i2b2.previousquery.QueryListNamesClient.java

public static OMElement getQueryPayLoad(String XMLstr) throws Exception {
    StringReader strReader = new StringReader(XMLstr);
    XMLInputFactory xif = XMLInputFactory.newInstance();
    XMLStreamReader reader = xif.createXMLStreamReader(strReader);

    StAXOMBuilder builder = new StAXOMBuilder(reader);
    OMElement lineItem = builder.getDocumentElement();
    //System.out.println("Line item string " + lineItem.toString());
    return lineItem;
}

From source file:edu.harvard.i2b2.eclipse.plugins.pft.ws.PFTServiceDriver2.java

/**
 * Function to convert PFT requestPdo to OMElement
 * //from  ww w . jav  a2  s . co m
 * @param requestPdo   String requestPdo to send to PFT web service
 * @return An OMElement containing the PFT web service requestPdo
 */
public static OMElement getPFTPayLoad(String requestPdo) throws Exception {
    OMElement method = null;
    try {
        OMFactory fac = OMAbstractFactory.getOMFactory();
        OMNamespace omNs = fac.createOMNamespace("http://i2b2.mgh.harvard.edu/message", "i2b2");
        method = fac.createOMElement("getPulmonaryData", omNs);

        StringReader strReader = new StringReader(requestPdo);
        XMLInputFactory xif = XMLInputFactory.newInstance();
        XMLStreamReader reader = xif.createXMLStreamReader(strReader);

        StAXOMBuilder builder = new StAXOMBuilder(reader);
        OMElement lineItem = builder.getDocumentElement();
        method.addChild(lineItem);
    } catch (FactoryConfigurationError e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        log.error(e.getMessage());
        throw new Exception(e);
    }
    return method;
}