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

public void buildTree(DefaultTreeModel treeModel, DefaultMutableTreeNode current, File file)
        throws XMLStreamException, FileNotFoundException {
    FileInputStream inputStream = new FileInputStream(file);
    XMLInputFactory inputFactory = XMLInputFactory.newInstance();
    XMLStreamReader reader = inputFactory.createXMLStreamReader(inputStream);

    addStartDocumentNodes(reader, current);

    parseRestOfDocument(reader, current);
}

From source file:it.iit.genomics.cru.structures.bridges.eppic.client.EppicJaxbClient.java

private EppicAnalysisList getPdbInterfaces(String fileName) {
    try {//  w  w w .j a va2s . com

        XMLInputFactory xmlif = XMLInputFactory.newInstance();
        XMLStreamReader xmler = xmlif.createXMLStreamReader(new FileReader(fileName));

        JAXBContext jaxbContext = JAXBContext.newInstance(EppicAnalysisList.class);
        Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();

        EppicAnalysisList result;
        result = (EppicAnalysisList) jaxbUnmarshaller.unmarshal(xmler);
        return result;
    } catch (JAXBException | IOException | XMLStreamException e) {
        logger.error(null, e);
    }
    return null;
}

From source file:au.org.ala.bhl.DocumentPaginator.java

public void paginateText(String text, PageHandler handler) {
    XMLInputFactory factory = XMLInputFactory.newInstance();
    try {/*  w ww  .jav  a  2  s  .c o  m*/
        XMLStreamReader parser = factory.createXMLStreamReader(new StringReader(text));
        paginateImpl(parser, handler);
    } catch (Exception ex) {
        throw new RuntimeException("Failed to paginate text", ex);
    }
}

From source file:au.org.ala.bhl.DocumentPaginator.java

public void paginate(Reader reader, PageHandler handler) {
    XMLInputFactory factory = XMLInputFactory.newInstance();
    try {//  w  w w.  j a  v  a  2 s.c  om
        XMLStreamReader parser = factory.createXMLStreamReader(reader);
        paginateImpl(parser, handler);
    } catch (Exception ex) {
        throw new RuntimeException("Failed to paginate from reader", ex);
    }

}

From source file:com.predic8.membrane.core.interceptor.acl.AccessControlInterceptor.java

protected AccessControl parse(String fileName, Router router) throws Exception {
    try {//from www .j  av  a 2s .  co  m
        XMLInputFactory factory = XMLInputFactory.newInstance();
        XMLStreamReader reader = new FixedStreamReader(factory.createXMLStreamReader(router.getResolverMap()
                .resolve(ResolverMap.combine(router == null ? null : router.getBaseLocation(), fileName))));
        AccessControl res = (AccessControl) new AccessControl(router).parse(reader);
        res.init(router);
        return res;
    } catch (Exception e) {
        log.error("Error initializing accessControl.", e);
        System.err.println("Error initializing accessControl: terminating.");
        throw new RuntimeException(e);
    }
}

From source file:wiki.link.LinkImporter.java

public boolean readXML(String filename) {

    //System.exit(1);
    //MAKE BLOODY SURE YOU HAVE SOME HOURS.
    DbConnector dbc = new DbConnector("localhost");
    dbc.jdbcTemplate.update("TRUNCATE links;");
    try {/*from ww  w  .  ja va  2 s .  c o  m*/
        XMLInputFactory xif = XMLInputFactory.newInstance();
        XMLStreamReader xsr = xif.createXMLStreamReader(new FileInputStream(filename));

        long n = 0;
        List<Doc> toSave = new ArrayList<>();
        while (xsr.hasNext()) {
            xsr.next();
            if (xsr.getEventType() == XMLStreamReader.START_ELEMENT) {
                if (xsr.getLocalName().equals("page")) {
                    long id = -1;
                    String title = null;
                    String text = null;
                    while (xsr.hasNext()) {
                        xsr.next();
                        if (xsr.getEventType() == XMLStreamReader.START_ELEMENT) {
                            if (xsr.getLocalName().equals("id") && id == -1) {
                                id = Long.parseLong(xsr.getElementText());
                            }
                            if (xsr.getLocalName().equals("title")) {
                                title = xsr.getElementText();
                            }
                            if (xsr.getLocalName().equals("text")) {
                                text = xsr.getElementText();
                            }
                        } else if (xsr.getEventType() == XMLStreamReader.END_ELEMENT
                                && xsr.getLocalName().equals("page")) {
                            break;
                        }
                    }
                    if (id != -1 && title != null && text != null) {
                        Doc wd = new Doc(id, title, text);
                        toSave.add(wd);
                        n++;
                        if (n % 1000 == 0) {
                            insertLinks(toSave, dbc);
                            //                                WikiDoc.insertAll(toSave, dbc);
                            System.out.println(n);
                            toSave.clear();
                        }
                    }
                }
            }
        }
    } catch (IOException | XMLStreamException e) {
        e.printStackTrace();
    }
    return true;
}

From source file:edu.harvard.i2b2.fr.ws.FileRepositoryService.java

private OMElement buildOMElementFromString(String xmlString, String contentID) throws XMLStreamException {
    OMElement returnElement = null;/*w w w  . j a  v a2 s  . c  om*/

    try {
        StringReader strReader = new StringReader(xmlString);
        XMLInputFactory xif = XMLInputFactory.newInstance();
        XMLStreamReader reader = xif.createXMLStreamReader(strReader);

        StAXOMBuilder builder = new StAXOMBuilder(reader);
        returnElement = builder.getDocumentElement();

        OMFactory factory = OMAbstractFactory.getOMFactory();
        OMNamespace omNs = factory.createOMNamespace("http://www.i2b2.org/xsd", "swa");

        OMElement fileElement = factory.createOMElement("file", omNs, returnElement);
        fileElement.addAttribute("href", contentID, null);
    } catch (XMLStreamException ex) {
        log.error("Error while converting FR response PDO to OMElement", ex);
        throw ex;
    }

    return returnElement;
}

From source file:com.sazneo.export.file.FileProcessor.java

/**
 * Process the XML file//from   w  w w.j  a  v a2  s  .c  o  m
 */
public void process() {
    try {
        Reader reader = new FileReader(exportFile);
        XMLInputFactory xmlFactory = XMLInputFactory.newInstance();
        XMLStreamReader xmlStreamReader = xmlFactory.createXMLStreamReader(reader);
        boolean fileTag = false;
        String fileName = null;
        Integer fileSize = null;
        while (xmlStreamReader.hasNext()) {
            xmlStreamReader.next();

            if (xmlStreamReader.getEventType() == XMLStreamReader.START_ELEMENT) {
                if ("file".equals(xmlStreamReader.getLocalName())) {
                    fileTag = true;
                    fileName = MessageFormat.format("{0}_{1}", xmlStreamReader.getAttributeValue(null, "id"),
                            xmlStreamReader.getAttributeValue(null, "filename"));
                    fileSize = Integer.parseInt(xmlStreamReader.getAttributeValue(null, "size"));
                    logger.debug(MessageFormat.format("XML contains file with name: {0} and size {1} bytes",
                            fileName, fileSize));
                }
            }

            if (fileTag) {
                if (xmlStreamReader.getEventType() == XMLStreamReader.CHARACTERS) {
                    exportFile(xmlStreamReader.getText(), fileName, fileSize);
                }

                if (xmlStreamReader.getEventType() == XMLStreamReader.END_ELEMENT) {
                    fileSize = null;
                    fileName = null;
                    fileTag = false;
                }
            }
        }
    } catch (FileNotFoundException e) {
        logger.error(MessageFormat.format("Could not find export file:-\n{0}", e));
    } catch (XMLStreamException e) {
        logger.error(MessageFormat.format("Could not parse xml export file:-\n{0}", e));
    }
}

From source file:edu.harvard.i2b2.fr.delegate.pm.PMServiceDriver.java

/**
 * Function to convert pm requestVdo to OMElement
 * /*from w ww . ja v  a 2s  . com*/
 * @param requestPm   String request to send to pm web service
 * @return An OMElement containing the pm web service requestVdo
 */
public OMElement getPmPayLoad(String requestPm) throws I2B2Exception {
    OMElement method = null;
    try {
        OMFactory fac = OMAbstractFactory.getOMFactory();
        //OMNamespace omNs = fac.createOMNamespace("http://www.i2b2.org/xsd/hive/msg",
        //"i2b2");
        //method = fac.createOMElement("request", omNs);
        StringReader strReader = new StringReader(requestPm);
        XMLInputFactory xif = XMLInputFactory.newInstance();
        XMLStreamReader reader = xif.createXMLStreamReader(strReader);
        StAXOMBuilder builder = new StAXOMBuilder(reader);
        method = builder.getDocumentElement();

    } catch (FactoryConfigurationError e) {
        log.error(e.getMessage());
        throw new I2B2Exception("", e.getException());
    } catch (XMLStreamException e) {
        log.error(e.getMessage());
        throw new I2B2Exception("", e);
    }
    return method;
}

From source file:edu.harvard.i2b2.crc.loader.delegate.pm.PMServiceDriver.java

/**
 * Function to convert pm requestVdo to OMElement
 * /*ww w .j ava2 s .c  o m*/
 * @param requestPm
 *            String request to send to pm web service
 * @return An OMElement containing the pm web service requestVdo
 */
public OMElement getPmPayLoad(String requestPm) throws I2B2Exception {
    OMElement method = null;
    try {
        OMFactory fac = OMAbstractFactory.getOMFactory();
        // OMNamespace omNs =
        // fac.createOMNamespace("http://www.i2b2.org/xsd/hive/msg",
        // "i2b2");
        // method = fac.createOMElement("request", omNs);
        StringReader strReader = new StringReader(requestPm);
        XMLInputFactory xif = XMLInputFactory.newInstance();
        XMLStreamReader reader = xif.createXMLStreamReader(strReader);
        StAXOMBuilder builder = new StAXOMBuilder(reader);
        method = builder.getDocumentElement();

    } catch (FactoryConfigurationError e) {
        log.error(e.getMessage());
        throw new I2B2Exception("", e.getException());
    } catch (XMLStreamException e) {
        log.error(e.getMessage());
        throw new I2B2Exception("", e);
    }
    return method;
}