Example usage for javax.xml.stream XMLStreamReader hasNext

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

Introduction

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

Prototype

public boolean hasNext() throws XMLStreamException;

Source Link

Document

Returns true if there are more parsing events and false if there are no more events.

Usage

From source file:com.microsoft.windowsazure.storage.table.TableParser.java

/**
 * Reserved for internal use. Reads the properties of an entity from the stream into a map of property names to
 * typed values. Reads the entity data as an AtomPub Entry Resource from the specified {@link XMLStreamReader} into
 * a map of <code>String</code> property names to {@link EntityProperty} data typed values.
 * // w w w.  j  a v  a2s  .  c o  m
 * @param xmlr
 *            The <code>XMLStreamReader</code> to read the data from.
 * @param opContext
 *            An {@link OperationContext} object used to track the execution of the operation.
 * 
 * @return
 *         A <code>java.util.HashMap</code> containing a map of <code>String</code> property names to
 *         {@link EntityProperty} data typed values found in the entity data.
 * @throws XMLStreamException
 *             if an error occurs accessing the stream.
 * @throws ParseException
 *             if an error occurs converting the input to a particular data type.
 */
private static HashMap<String, EntityProperty> readAtomProperties(final XMLStreamReader xmlr,
        final OperationContext opContext) throws XMLStreamException, ParseException {
    int eventType = xmlr.getEventType();
    xmlr.require(XMLStreamConstants.START_ELEMENT, null, ODataConstants.PROPERTIES);
    final HashMap<String, EntityProperty> properties = new HashMap<String, EntityProperty>();

    while (xmlr.hasNext()) {
        eventType = xmlr.next();
        if (eventType == XMLStreamConstants.CHARACTERS) {
            xmlr.getText();
            continue;
        }

        if (eventType == XMLStreamConstants.START_ELEMENT
                && xmlr.getNamespaceURI().equals(ODataConstants.DATA_SERVICES_NS)) {
            final String key = xmlr.getLocalName();
            String val = Constants.EMPTY_STRING;
            String edmType = null;

            if (xmlr.getAttributeCount() > 0) {
                edmType = xmlr.getAttributeValue(ODataConstants.DATA_SERVICES_METADATA_NS, ODataConstants.TYPE);
            }

            // move to chars
            eventType = xmlr.next();

            if (eventType == XMLStreamConstants.CHARACTERS) {
                val = xmlr.getText();

                // end element
                eventType = xmlr.next();
            }

            xmlr.require(XMLStreamConstants.END_ELEMENT, null, key);

            final EntityProperty newProp = new EntityProperty(val, EdmType.parse(edmType));
            properties.put(key, newProp);
        } else if (eventType == XMLStreamConstants.END_ELEMENT && xmlr.getName().toString()
                .equals(ODataConstants.BRACKETED_DATA_SERVICES_METADATA_NS + ODataConstants.PROPERTIES)) {
            // End read properties
            break;
        }
    }

    xmlr.require(XMLStreamConstants.END_ELEMENT, null, ODataConstants.PROPERTIES);
    return properties;
}

From source file:com.prowidesoftware.swift.io.parser.MxParser.java

/**
 * Takes an xml with an MX message and detects the specific message type
 * parsing just the namespace from the Document element. If the Document
 * element is not present, or without the namespace or if the namespace url
 * contains invalid content it will return null.
 * <br><br>/*from w ww .j  a va 2s. c  o m*/
 * Example of a recognizable Document element:<br>
 * <Doc:Document xmlns:Doc="urn:swift:xsd:camt.003.001.04" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
 * <br>
 * The implementation is intended to be lightweight and efficient, based on {@link javax.xml.stream.XMLStreamReader} 
 *
 * @return id with the detected MX message type or null if it cannot be determined.
 * @since 7.7
 */
public MxId detectMessage() {
    if (StringUtils.isBlank(this.buffer)) {
        log.log(Level.SEVERE, "cannot detect message from null or empty content");
        return null;
    }
    final javax.xml.stream.XMLInputFactory xif = javax.xml.stream.XMLInputFactory.newInstance();
    try {
        final javax.xml.stream.XMLStreamReader reader = xif
                .createXMLStreamReader(new StringReader(this.buffer));
        while (reader.hasNext()) {
            int event = reader.next();
            if (javax.xml.stream.XMLStreamConstants.START_ELEMENT == event
                    && reader.getLocalName().equals(DOCUMENT_LOCALNAME)) {
                if (reader.getNamespaceCount() > 0) {
                    //log.finest("ELEMENT START: " + reader.getLocalName() + " , namespace count is: " + reader.getNamespaceCount());
                    for (int nsIndex = 0; nsIndex < reader.getNamespaceCount(); nsIndex++) {
                        final String nsPrefix = StringUtils.trimToNull(reader.getNamespacePrefix(nsIndex));
                        final String elementPrefix = StringUtils.trimToNull(reader.getPrefix());
                        if (StringUtils.equals(nsPrefix, elementPrefix)) {
                            String nsId = reader.getNamespaceURI(nsIndex);
                            //log.finest("\tNamepsace prefix: " + nsPrefix + " associated with URI " + nsId);
                            return new MxId(nsId);
                        }
                    }
                }
            }
        }
    } catch (final Exception e) {
        log.log(Level.SEVERE, "error while detecting message", e);
    }
    return null;
}

From source file:de.uzk.hki.da.sb.SIPFactoryTest.java

/**
 * @param premis/* w ww  .ja  v a 2s  .  c om*/
 * @param publicRights 
 */
private boolean checkPremisFilePubStartDate(File premis, ContractRights rights) {

    XMLInputFactory inputFactory = XMLInputFactory.newInstance();
    XMLStreamReader streamReader;
    try {
        streamReader = inputFactory.createXMLStreamReader(new FileInputStream(premis));
        while (streamReader.hasNext()) {
            int event = streamReader.next();
            switch (event) {
            case XMLStreamConstants.START_ELEMENT:
                if (streamReader.getLocalName().equals("startDate")) {

                    String startDate = streamReader.getElementText().substring(0, 10);
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                    String publicDate = sdf.format(rights.getPublicRights().getStartDate());
                    if (startDate.trim().equals(publicDate.trim())) {
                        return true;
                    }
                }
            default:
                break;
            }
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (XMLStreamException e) {
        e.printStackTrace();
    }
    return false;
}

From source file:com.microsoft.windowsazure.storage.table.TableParser.java

/**
 * Reserved for internal use. Parses the operation response as a collection of entities. Reads entity data from the
 * specified input stream using the specified class type and optionally projects each entity result with the
 * specified resolver into an {@link ODataPayload} containing a collection of {@link TableResult} objects.
 * /*from w ww.ja v  a 2s .co m*/
 * @param inStream
 *            The <code>InputStream</code> to read the data to parse from.
 * @param clazzType
 *            The class type <code>T</code> implementing {@link TableEntity} for the entities returned. Set to
 *            <code>null</code> to ignore the returned entities and copy only response properties into the
 *            {@link TableResult} objects.
 * @param resolver
 *            An {@link EntityResolver} instance to project the entities into instances of type <code>R</code>. Set
 *            to <code>null</code> to return the entities as instances of the class type <code>T</code>.
 * @param opContext
 *            An {@link OperationContext} object used to track the execution of the operation.
 * @return
 *         An {@link ODataPayload} containing a collection of {@link TableResult} objects with the parsed operation
 *         response.
 * 
 * @throws XMLStreamException
 *             if an error occurs while accessing the stream.
 * @throws ParseException
 *             if an error occurs while parsing the stream.
 * @throws InstantiationException
 *             if an error occurs while constructing the result.
 * @throws IllegalAccessException
 *             if an error occurs in reflection while parsing the result.
 * @throws StorageException
 *             if a storage service error occurs.
 */
@SuppressWarnings("unchecked")
private static <T extends TableEntity, R> ODataPayload<?> parseAtomQueryResponse(final InputStream inStream,
        final Class<T> clazzType, final EntityResolver<R> resolver, final OperationContext opContext)
        throws XMLStreamException, ParseException, InstantiationException, IllegalAccessException,
        StorageException {
    ODataPayload<T> corePayload = null;
    ODataPayload<R> resolvedPayload = null;
    ODataPayload<?> commonPayload = null;

    if (resolver != null) {
        resolvedPayload = new ODataPayload<R>();
        commonPayload = resolvedPayload;
    } else {
        corePayload = new ODataPayload<T>();
        commonPayload = corePayload;
    }

    final XMLStreamReader xmlr = Utility.createXMLStreamReaderFromStream(inStream);
    int eventType = xmlr.getEventType();
    xmlr.require(XMLStreamConstants.START_DOCUMENT, null, null);
    eventType = xmlr.next();

    xmlr.require(XMLStreamConstants.START_ELEMENT, null, ODataConstants.FEED);
    // skip feed chars
    eventType = xmlr.next();

    while (xmlr.hasNext()) {
        eventType = xmlr.next();

        if (eventType == XMLStreamConstants.CHARACTERS) {
            xmlr.getText();
            continue;
        }

        final String name = xmlr.getName().toString();

        if (eventType == XMLStreamConstants.START_ELEMENT) {
            if (name.equals(ODataConstants.BRACKETED_ATOM_NS + ODataConstants.ENTRY)) {
                final TableResult res = parseAtomEntity(xmlr, clazzType, resolver, opContext);
                if (corePayload != null) {
                    corePayload.tableResults.add(res);
                }

                if (resolver != null) {
                    resolvedPayload.results.add((R) res.getResult());
                } else {
                    corePayload.results.add((T) res.getResult());
                }
            }
        } else if (eventType == XMLStreamConstants.END_ELEMENT
                && name.equals(ODataConstants.BRACKETED_ATOM_NS + ODataConstants.FEED)) {
            break;
        }
    }

    xmlr.require(XMLStreamConstants.END_ELEMENT, null, ODataConstants.FEED);
    return commonPayload;
}

From source file:com.prowidesoftware.swift.io.parser.MxParser.java

/**
 * Convenient API to get structure information from an MX message.
 * <br ><br>//from   w w  w . j  a  va 2s.  co  m
 * This can be helpful when the actual content of an XML is unknown and 
 * some preprocessing of the XML must be done in order to parse or
 * validate its content properly.
 * <br >
 * The implementation is intended to be lightweight and efficient, based on {@link javax.xml.stream.XMLStreamReader}
 *  
 * @since 7.8.4
 */
public MxStructureInfo analizeMessage() {
    if (this.info != null) {
        return this.info;
    }
    this.info = new MxStructureInfo();
    if (StringUtils.isBlank(this.buffer)) {
        log.log(Level.WARNING, "cannot analize message from null or empty content");
        return this.info;
    }
    final javax.xml.stream.XMLInputFactory xif = javax.xml.stream.XMLInputFactory.newInstance();
    try {
        final javax.xml.stream.XMLStreamReader reader = xif
                .createXMLStreamReader(new StringReader(this.buffer));
        boolean first = true;
        while (reader.hasNext()) {
            int event = reader.next();
            if (javax.xml.stream.XMLStreamConstants.START_ELEMENT == event) {
                if (reader.getLocalName().equals(DOCUMENT_LOCALNAME)) {
                    this.info.containsDocument = true;
                    this.info.documentNamespace = readNamespace(reader);
                    this.info.documentPrefix = StringUtils.trimToNull(reader.getPrefix());
                } else if (reader.getLocalName().equals(HEADER_LOCALNAME)) {
                    this.info.containsHeader = true;
                    this.info.headerNamespace = readNamespace(reader);
                    this.info.headerPrefix = StringUtils.trimToNull(reader.getPrefix());
                } else if (first) {
                    this.info.containsWrapper = true;
                }
                first = false;
            }
        }
    } catch (final Exception e) {
        log.log(Level.SEVERE, "error while analizing message: " + e.getMessage());
        info.exception = e;
    }
    return this.info;
}

From source file:com.conx.logistics.kernel.bpm.impl.jbpm.core.mock.BPMGuvnorUtil.java

public boolean templateExistsInRepo(String templateName) throws Exception {
    List<String> allPackages = getPackageNames();
    try {/* w w w. j  a va 2  s  . c  om*/
        for (String pkg : allPackages) {
            String templateURL = getGuvnorProtocol() + "://" + getGuvnorHost() + "/" + getGuvnorSubdomain()
                    + "/rest/packages/" + pkg + "/assets/" + URLEncoder.encode(templateName, "UTF-8");

            URL checkURL = new URL(templateURL);
            HttpURLConnection checkConnection = (HttpURLConnection) checkURL.openConnection();
            checkConnection.setRequestMethod("GET");
            checkConnection.setRequestProperty("Accept", "application/atom+xml");
            checkConnection.setConnectTimeout(Integer.parseInt(getGuvnorConnectTimeout()));
            checkConnection.setReadTimeout(Integer.parseInt(getGuvnorReadTimeout()));
            applyAuth(checkConnection);
            checkConnection.connect();
            if (checkConnection.getResponseCode() == 200) {

                XMLInputFactory factory = XMLInputFactory.newInstance();
                XMLStreamReader reader = factory.createXMLStreamReader(checkConnection.getInputStream());

                boolean foundFormFormat = false;
                while (reader.hasNext()) {
                    if (reader.next() == XMLStreamReader.START_ELEMENT) {
                        if ("format".equals(reader.getLocalName())) {
                            reader.next();
                            String pname = reader.getElementText();
                            if ("flt".equalsIgnoreCase(pname)) {
                                foundFormFormat = true;
                                break;
                            }
                        }
                    }
                }
                return foundFormFormat;
            }
        }
    } catch (Exception e) {
        logger.error("Exception checking template url : " + e.getMessage());
        return false;
    }
    logger.info("Could not find process template for: " + templateName);
    return false;
}

From source file:com.conx.logistics.kernel.bpm.impl.jbpm.core.mock.BPMGuvnorUtil.java

public List<String> getAllProcessesInPackage(String pkgName) {
    List<String> processes = new ArrayList<String>();
    String assetsURL = getGuvnorProtocol() + "://" + getGuvnorHost() + "/" + getGuvnorSubdomain()
            + "/rest/packages/" + pkgName + "/assets/";

    try {/*from   ww w  .ja  v  a2s  . c  om*/
        XMLInputFactory factory = XMLInputFactory.newInstance();
        XMLStreamReader reader = factory.createXMLStreamReader(getInputStreamForURL(assetsURL, "GET"));

        String format = "";
        String title = "";
        while (reader.hasNext()) {
            int next = reader.next();
            if (next == XMLStreamReader.START_ELEMENT) {
                if ("format".equals(reader.getLocalName())) {
                    format = reader.getElementText();
                }
                if ("title".equals(reader.getLocalName())) {
                    title = reader.getElementText();
                }
                if ("asset".equals(reader.getLocalName())) {
                    if (format.equals(EXT_BPMN) || format.equals(EXT_BPMN2)) {
                        processes.add(title);
                        title = "";
                        format = "";
                    }
                }
            }
        }
        // last one
        if (format.equals(EXT_BPMN) || format.equals(EXT_BPMN2)) {
            processes.add(title);
        }
    } catch (Exception e) {
        logger.error("Error finding processes in package: " + e.getMessage());
    }
    return processes;
}

From source file:net.cloudkit.toolkit.DownloadTest.java

@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)
@Transactional/*  w  ww .  ja v a2  s  .c  o m*/
@Test
public void test() throws Exception {

    Map<String, Object> cdl = new HashMap<>();
    Map<String, String> cdlBillHead = null;
    Map<String, String> cdlBillList = null;
    List<Map<String, String>> cdlBillLists = new ArrayList<>();
    Map<String, String> cdlBillDoc = null;
    List<Map<String, String>> cdlBillDocs = new ArrayList<>();

    // MESSAGE_CONFIG
    // InputStream
    InputStream in = null;
    try {

        // StAX?
        XMLInputFactory xif = XMLInputFactory.newInstance();
        // ?
        XMLStreamReader reader = xif
                .createXMLStreamReader(new FileInputStream(new File("D:\\temp\\bill\\CDL100000006531646.xml")));

        // 
        while (reader.hasNext()) {
            // ?
            int event = reader.next();

            // 
            // if (reader.getEventType() == XMLStreamConstants.START_ELEMENT) {
            if (event == XMLStreamReader.START_ELEMENT) {
                // CdlBill/CdlBillHead
                if ("CdlBillHead".equals(reader.getLocalName())) {
                    cdlBillHead = new LinkedHashMap<>();
                }
                {
                    // BillSeq
                    if ("BillSeq".equals(reader.getLocalName())) {
                        cdlBillHead.put("BillSeq", reader.getElementText());
                    }
                    // ListNo
                    if ("ListNo".equals(reader.getLocalName())) {
                        cdlBillHead.put("ListNo", reader.getElementText());
                    }
                    // IEFlag
                    if ("IEFlag".equals(reader.getLocalName())) {
                        cdlBillHead.put("IEFlag", reader.getElementText());
                    }
                    // IEPort
                    if ("IEPort".equals(reader.getLocalName())) {
                        cdlBillHead.put("IEPort", reader.getElementText());
                    }
                    // IEDate
                    if ("IEDate".equals(reader.getLocalName())) {
                        cdlBillHead.put("IEDate", reader.getElementText());
                    }
                    // RecordsNo
                    if ("RecordsNo".equals(reader.getLocalName())) {
                        cdlBillHead.put("RecordsNo", reader.getElementText());
                    }
                    // TradeCode
                    if ("TradeCode".equals(reader.getLocalName())) {
                        cdlBillHead.put("TradeCode", reader.getElementText());
                    }
                    // TradeName
                    if ("TradeName".equals(reader.getLocalName())) {
                        cdlBillHead.put("TradeName", reader.getElementText());
                    }
                    // OwnerCode
                    if ("OwnerCode".equals(reader.getLocalName())) {
                        cdlBillHead.put("OwnerCode", reader.getElementText());
                    }
                    // OwnerName
                    if ("OwnerName".equals(reader.getLocalName())) {
                        cdlBillHead.put("OwnerName", reader.getElementText());
                    }
                    // TrafMode
                    if ("TrafMode".equals(reader.getLocalName())) {
                        cdlBillHead.put("TrafMode", reader.getElementText());
                    }
                    // ShipName
                    if ("ShipName".equals(reader.getLocalName())) {
                        cdlBillHead.put("ShipName", reader.getElementText());
                    }
                    // VoyageNo
                    if ("VoyageNo".equals(reader.getLocalName())) {
                        cdlBillHead.put("VoyageNo", reader.getElementText());
                    }
                    // BillNo
                    if ("BillNo".equals(reader.getLocalName())) {
                        cdlBillHead.put("BillNo", reader.getElementText());
                    }
                    // TradeMode
                    if ("TradeMode".equals(reader.getLocalName())) {
                        cdlBillHead.put("TradeMode", reader.getElementText());
                    }
                    // TradeCountry
                    if ("TradeCountry".equals(reader.getLocalName())) {
                        cdlBillHead.put("TradeCountry", reader.getElementText());
                    }
                    // DestinationPort
                    if ("DestinationPort".equals(reader.getLocalName())) {
                        cdlBillHead.put("DestinationPort", reader.getElementText());
                    }
                    // DistrictCode
                    if ("DistrictCode".equals(reader.getLocalName())) {
                        cdlBillHead.put("DistrictCode", reader.getElementText());
                    }
                    // PackNum
                    if ("PackNum".equals(reader.getLocalName())) {
                        cdlBillHead.put("PackNum", reader.getElementText());
                    }
                    // WrapType
                    if ("WrapType".equals(reader.getLocalName())) {
                        cdlBillHead.put("WrapType", reader.getElementText());
                    }
                    // GrossWt
                    if ("GrossWt".equals(reader.getLocalName())) {
                        cdlBillHead.put("GrossWt", reader.getElementText());
                    }
                    // NetWt
                    if ("NetWt".equals(reader.getLocalName())) {
                        cdlBillHead.put("NetWt", reader.getElementText());
                    }
                    // ListType
                    if ("ListType".equals(reader.getLocalName())) {
                        cdlBillHead.put("ListType", reader.getElementText());
                    }
                    // ListStat
                    if ("ListStat".equals(reader.getLocalName())) {
                        cdlBillHead.put("ListStat", reader.getElementText());
                    }
                    // DocuCodeCom
                    if ("DocuCodeCom".equals(reader.getLocalName())) {
                        cdlBillHead.put("DocuCodeCom", reader.getElementText());
                    }
                    // AgentCode
                    if ("AgentCode".equals(reader.getLocalName())) {
                        cdlBillHead.put("AgentCode", reader.getElementText());
                    }
                    // AgentName
                    if ("AgentName".equals(reader.getLocalName())) {
                        cdlBillHead.put("AgentName", reader.getElementText());
                    }
                    // DeclCustom
                    if ("DeclCustom".equals(reader.getLocalName())) {
                        cdlBillHead.put("DeclCustom", reader.getElementText());
                    }
                    // DeclDate
                    if ("DeclDate".equals(reader.getLocalName())) {
                        cdlBillHead.put("DeclDate", reader.getElementText());
                    }
                    // GType
                    if ("GType".equals(reader.getLocalName())) {
                        cdlBillHead.put("GType", reader.getElementText());
                    }

                }

                // CdlBill/CdlBillLists/CdlBillList
                if ("CdlBillList".equals(reader.getLocalName())) {
                    cdlBillList = new LinkedHashMap<>();
                }
                {
                    // GNo
                    if ("GNo".equals(reader.getLocalName())) {
                        cdlBillList.put("GNo", reader.getElementText());
                    }
                    // ItemNo
                    if ("ItemNo".equals(reader.getLocalName())) {
                        cdlBillList.put("ItemNo", reader.getElementText());
                    }
                    // CodeTs
                    if ("CodeTs".equals(reader.getLocalName())) {
                        cdlBillList.put("CodeTs", reader.getElementText());
                    }
                    // GName
                    if ("GName".equals(reader.getLocalName())) {
                        cdlBillList.put("GName", reader.getElementText());
                    }
                    // GModel
                    if ("GModel".equals(reader.getLocalName())) {
                        cdlBillList.put("GModel", reader.getElementText());
                    }
                    // GQty
                    if ("GQty".equals(reader.getLocalName())) {
                        cdlBillList.put("GQty", reader.getElementText());
                    }
                    // GUnit
                    if ("GUnit".equals(reader.getLocalName())) {
                        cdlBillList.put("GUnit", reader.getElementText());
                    }
                    // DeclPrice
                    if ("DeclPrice".equals(reader.getLocalName())) {
                        cdlBillList.put("DeclPrice", reader.getElementText());
                    }
                    // DeclTotal
                    if ("DeclTotal".equals(reader.getLocalName())) {
                        cdlBillList.put("DeclTotal", reader.getElementText());
                    }
                    // TradeCurr
                    if ("TradeCurr".equals(reader.getLocalName())) {
                        cdlBillList.put("TradeCurr", reader.getElementText());
                    }
                    // OriginalalCountry
                    if ("OriginalalCountry".equals(reader.getLocalName())) {
                        cdlBillList.put("OriginalalCountry", reader.getElementText());
                    }
                    // DutyMode
                    if ("DutyMode".equals(reader.getLocalName())) {
                        cdlBillList.put("DutyMode", reader.getElementText());
                    }
                    // Unit1
                    if ("Unit1".equals(reader.getLocalName())) {
                        cdlBillList.put("Unit1", reader.getElementText());
                    }
                    // Unit2
                    if ("Unit2".equals(reader.getLocalName())) {
                        cdlBillList.put("Unit2", reader.getElementText());
                    }
                }

                // CdlBill/CdlBillDocs/CdlBillDoc
                if ("CdlBillDoc".equals(reader.getLocalName())) {
                    cdlBillDoc = new LinkedHashMap<>();
                }
                {
                    // DocCode
                    if ("DocCode".equals(reader.getLocalName())) {
                        cdlBillDoc.put("DocCode", reader.getElementText());
                    }
                    // CertNo
                    if ("CertNo".equals(reader.getLocalName())) {
                        cdlBillDoc.put("CertNo", reader.getElementText());
                    }
                }
            }

            if (event == XMLStreamReader.END_ELEMENT) {
                // CdlBill/CdlBillHead
                if ("CdlBillHead".equals(reader.getLocalName())) {

                }

                // CdlBill/CdlBillLists/CdlBillList
                if ("CdlBillList".equals(reader.getLocalName())) {
                    cdlBillLists.add(cdlBillList);
                }

                // CdlBill/CdlBillDocs/CdlBillDoc
                if ("CdlBillDoc".equals(reader.getLocalName())) {
                    cdlBillDocs.add(cdlBillDoc);
                }
            }
        }
    } catch (XMLStreamException e) {
        e.printStackTrace();
    } finally {
        if (in != null) {
            try {
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    cdl.put("CdlBillHead", cdlBillHead);
    cdl.put("CdlBillLists", cdlBillLists);
    cdl.put("CdlBillDocs", cdlBillDocs);
    System.out.println(cdlBillLists.size() + " " + cdlBillDocs.size());
    persistentRepositoryService.save(cdl);

}

From source file:ca.efendi.datafeeds.messaging.FtpSubscriptionMessageListener.java

private void parse(FtpSubscription ftpSubscription, final InputStream is) throws XMLStreamException {
    final XMLInputFactory factory = XMLInputFactory.newInstance();
    factory.setProperty(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES, true);
    factory.setProperty(XMLInputFactory.IS_COALESCING, true);
    final XMLStreamReader reader = factory.createXMLStreamReader(is, "UTF-8");
    CJProduct product = null;/*from ww w . ja  v a2s  . c o m*/
    String tagContent = null;
    //final ServiceContext serviceContext = new ServiceContext();

    //ServiceContext serviceContext = ServiceContextFactory.getInstance(
    //        BlogsEntry.class.getName(), actionRequest);

    //serviceContext.setScopeGroupId(20159);
    while (reader.hasNext()) {
        final int event = reader.next();
        switch (event) {
        case XMLStreamConstants.START_ELEMENT:
            //tagContent = "";
            if ("product".equals(reader.getLocalName())) {
                product = _cjProductLocalService.createCJProduct(0);
            }
            break;
        case XMLStreamConstants.CHARACTERS:
            //tagContent += reader.getText().trim();
            tagContent = reader.getText().trim();
            break;
        case XMLStreamConstants.END_ELEMENT:
            switch (reader.getLocalName()) {
            case "product":
                try {

                    _log.warn("refreshing document...");
                    _cjProductLocalService.refresh(ftpSubscription, product);
                } catch (final SystemException e) {
                    _log.error(e);
                } catch (final PortalException e) {
                    _log.error(e);
                }
                break;
            case "programname":
                product.setProgramName(tagContent);
                break;
            case "programurl":
                product.setProgramUrl(tagContent);
                break;
            case "catalogname":
                product.setCatalogName(tagContent);
                break;
            case "lastupdated":
                product.setLastUpdated(tagContent);
                break;
            case "name":
                product.setName(tagContent);
                break;
            case "keywords":
                product.setKeywords(tagContent);
                break;
            case "description":
                product.setDescription(tagContent);
                break;
            case "sku":
                product.setSku(tagContent);
                break;
            case "manufacturer":
                product.setManufacturer(tagContent);
                break;
            case "manufacturerid":
                product.setManufacturerId(tagContent);
                break;
            case "currency":
                product.setCurrency(tagContent);
                break;
            case "price":
                product.setPrice(tagContent);
                break;
            case "buyurl":
                product.setBuyUrl(tagContent);
                break;
            case "impressionurl":
                product.setImpressionUrl(tagContent);
                break;
            case "imageurl":
                product.setImageUrl(tagContent);
                break;
            case "instock":
                product.setInStock(tagContent);
                break;
            }
            break;
        case XMLStreamConstants.START_DOCUMENT:
            break;
        }
    }
}

From source file:net.bulletin.pdi.xero.step.support.XMLChunkerImpl.java

private String pullNextXmlChunkFromTopElementOnStack(XMLChunkerState data) throws KettleException {
    Stack<String> elementStack = data.getElementStack();
    XMLStreamReader xmlStreamReader = data.getXmlStreamReader();

    int elementStackDepthOnEntry = elementStack.size();
    StringWriter stringWriter = new StringWriter();

    try {//from   w  w w.j  av  a2s.com
        XMLStreamWriter xmlStreamWriter = data.getXmlOutputFactory().createXMLStreamWriter(stringWriter);

        xmlStreamWriter.writeStartDocument(CharEncoding.UTF_8, "1.0");

        // put the current element on because presumably it's the open element for the one
        // that is being looked for.

        XmlReaderToWriter.write(xmlStreamReader, xmlStreamWriter);

        while (xmlStreamReader.hasNext() & elementStack.size() >= elementStackDepthOnEntry) {

            switch (xmlStreamReader.next()) {

            case XMLStreamConstants.END_DOCUMENT:
                break; // handled below explicitly.

            case XMLStreamConstants.END_ELEMENT:
                elementStack.pop();
                XmlReaderToWriter.write(xmlStreamReader, xmlStreamWriter);
                break;

            case XMLStreamConstants.START_ELEMENT:
                elementStack.push(xmlStreamReader.getLocalName());
                XmlReaderToWriter.write(xmlStreamReader, xmlStreamWriter);
                break;

            default:
                XmlReaderToWriter.write(xmlStreamReader, xmlStreamWriter);
                break;

            }

        }

        xmlStreamWriter.writeEndDocument();
        xmlStreamWriter.close();
    } catch (Exception e) {
        throw new KettleException("unable to process a chunk of the xero xml stream", e);
    }

    return stringWriter.toString();
}