Example usage for javax.xml.stream XMLStreamReader getElementText

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

Introduction

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

Prototype

public String getElementText() throws XMLStreamException;

Source Link

Document

Reads the content of a text-only element, an exception is thrown if this is not a text-only element.

Usage

From source file:edu.indiana.d2i.htrc.portal.HTRCAgentClient.java

private JobDetailsBean parseJobSubmit(InputStream stream) throws XMLStreamException {
    JobDetailsBean res = new JobDetailsBean();
    XMLStreamReader parser = factory.createXMLStreamReader(stream);
    while (parser.hasNext()) {
        int event = parser.next();
        if (event == XMLStreamConstants.START_ELEMENT) {
            if (parser.hasName()) {
                // only parse several attributes
                if (parser.getLocalName().equals(JobDetailsBean.STATUS)) {
                    res.setJobStatus(parser.getAttributeValue(0));
                } else if (parser.getLocalName().equals(JobDetailsBean.JOBID)) {
                    res.setJobId(parser.getElementText());
                } else if (parser.getLocalName().equals(JobDetailsBean.JOBNAME)) {
                    res.setJobTitle(parser.getElementText());
                } else if (parser.getLocalName().equals(JobDetailsBean.DATE)) {
                    res.setLastUpdatedDate(parser.getElementText());
                } else if (parser.getLocalName().equals(JobDetailsBean.USER)) {
                    res.setUserName(parser.getElementText());
                }/*from w w w  .ja  va  2  s  .c  om*/
            }
        }
    }
    return res;
}

From source file:iTests.framework.utils.WebuiTestUtils.java

/**
 * retrieves the license key from gigaspaces installation license key
 * @throws javax.xml.stream.FactoryConfigurationError
 * @throws javax.xml.stream.XMLStreamException
 * @throws java.io.IOException/*  ww  w .j a v a2  s  .c om*/
 */
public String getLicenseKey() throws XMLStreamException, FactoryConfigurationError, IOException {

    String licensekey = LICENSE_PATH.replace("lib/required/../../", "");
    InputStream is = new FileInputStream(new File(licensekey));
    XMLStreamReader parser = XMLInputFactory.newInstance().createXMLStreamReader(is);
    int element;
    while (true) {
        element = parser.next();
        if (element == XMLStreamReader.START_ELEMENT) {
            if (parser.getName().toString().equals("licensekey")) {
                return parser.getElementText();
            }
        }
        if (element == XMLStreamReader.END_DOCUMENT) {
            break;
        }
    }
    return null;

}

From source file:edu.indiana.d2i.htrc.io.index.solr.SolrClient.java

private Vector createVector(XMLStreamReader parser) throws XMLStreamException {
    Vector vector = new RandomAccessSparseVector(dictionary.size());
    while (parser.hasNext()) {
        int event = parser.next();
        if (event == XMLStreamConstants.START_ELEMENT) {
            String attributeValue = parser.getAttributeValue(null, "name");
            if (attributeValue != null) {
                //               if (dictionary.containsKey(attributeValue)) {
                //                  parser.next();
                //                  int tf = Integer.valueOf(parser.getElementText());
                //                  vector.setQuick(dictionary.get(attributeValue), tf);
                //               }

                parser.next();//from   w ww  .ja  v  a2s .  com
                int freq = Integer.valueOf(parser.getElementText());
                if (filter.accept(attributeValue, freq)) {
                    vector.setQuick(dictionary.get(attributeValue), freq);
                }
            }
        }
    }
    return vector;
}

From source file:com.flexive.chemistry.webdav.TextDocumentResource.java

/**
 * Set the value of a property, stream points to the start of the property tag.
 *
 * @param parser    the XML parser/* w ww  .  ja  v  a 2s .  c  om*/
 * @throws XMLStreamException   on parsing errors
 */
protected void processProperty(XMLStreamReader parser) throws XMLStreamException {
    int level = 0;
    String name = null;
    for (int i = 0; i < parser.getAttributeCount(); i++) {
        if ("name".equals(parser.getAttributeName(i).getLocalPart())) {
            name = parser.getAttributeValue(i);
            break;
        }
    }
    if (name == null) {
        if (LOG.isTraceEnabled()) {
            LOG.trace("property without name attribute encountered");
        }
        return;
    }

    String value = null;
    for (int event = parser.nextTag(); event != XMLStreamConstants.END_DOCUMENT
            && level >= 0; event = parser.nextTag()) {
        switch (event) {
        case XMLStreamConstants.START_ELEMENT:
            if ("value".equals(parser.getLocalName())) {
                value = parser.getElementText().trim();
            } else if ("name".equals(parser.getLocalName())) {
                name = parser.getElementText();
            } else {
                level++;
            }
            break;
        case XMLStreamConstants.END_ELEMENT:
            level--;
            break;
        }
    }

    if (value != null) {
        if (LOG.isTraceEnabled()) {
            LOG.trace("Setting field " + name + " to " + value);
        }
        try {
            object.setValue(name, value);
        } catch (Exception e) {
            if (LOG.isDebugEnabled()) {
                LOG.debug("Failed to set field " + name + " (ignored): " + e.getMessage());
            }
        }
    }
}

From source file:edu.indiana.d2i.htrc.portal.HTRCPersistenceAPIClient.java

private AlgorithmDetailsBean parseAlgorithmDetailBean(InputStream stream) throws XMLStreamException {
    AlgorithmDetailsBean res = new AlgorithmDetailsBean();
    XMLStreamReader parser = factory.createXMLStreamReader(stream);

    List<AlgorithmDetailsBean.Parameter> parameters = new ArrayList<AlgorithmDetailsBean.Parameter>();
    List<String> authors = new ArrayList<String>();
    while (parser.hasNext()) {
        int event = parser.next();
        if (event == XMLStreamConstants.START_ELEMENT) {
            if (parser.hasName()) {
                // only parse the info tag!
                if (parser.getLocalName().equals(AlgorithmDetailsBean.NAME)) {
                    res.setName(parser.getElementText());
                } else if (parser.getLocalName().equals(AlgorithmDetailsBean.VERSION)) {
                    res.setVersion(parser.getElementText());
                } else if (parser.getLocalName().equals(AlgorithmDetailsBean.DESCRIPTION)) {
                    res.setDescription(parser.getElementText());
                } else if (parser.getLocalName().equals(AlgorithmDetailsBean.SUPPORTURL)) {
                    res.setSupportUrl(parser.getElementText());
                } else if (parser.getLocalName().equals(AlgorithmDetailsBean.PARAMETER)) {
                    AlgorithmDetailsBean.Parameter parameter = new AlgorithmDetailsBean.Parameter();
                    int count = parser.getAttributeCount();
                    for (int i = 0; i < count; i++) {
                        if (parser.getAttributeLocalName(i).equals("required"))
                            parameter.setRequired(Boolean.valueOf(parser.getAttributeValue(i)));
                        if (parser.getAttributeLocalName(i).equals("type"))
                            parameter.setType(parser.getAttributeValue(i));
                        if (parser.getAttributeLocalName(i).equals("name"))
                            parameter.setName(parser.getAttributeValue(i));
                        if (parser.getAttributeLocalName(i).equals("defaultValue"))
                            parameter.setDefaultValue(parser.getAttributeValue(i));
                        if (parser.getAttributeLocalName(i).equals("validation"))
                            parameter.setValidation(parser.getAttributeValue(i));
                        if (parser.getAttributeLocalName(i).equals("validationError"))
                            parameter.setValidationError(parser.getAttributeValue(i));
                        if (parser.getAttributeLocalName(i).equals("readOnly"))
                            parameter.setReadOnly(Boolean.parseBoolean(parser.getAttributeValue(i)));
                    }//  www.j  a v a 2 s .  co  m
                    parser.nextTag();
                    if (parser.getLocalName().equals("label"))
                        parameter.setLabel(parser.getElementText());
                    parser.nextTag();
                    if (parser.getLocalName().equals("description"))
                        parameter.setDescription(parser.getElementText());
                    parameters.add(parameter);
                } else if (parser.getLocalName().equals(AlgorithmDetailsBean.AUTHOR)) {
                    int count = parser.getAttributeCount();
                    for (int i = 0; i < count; i++) {
                        if (parser.getAttributeLocalName(i).equals("name"))
                            authors.add(parser.getAttributeValue(i));
                    }
                }
            }
        }
    }
    res.setParameters(parameters);
    res.setAuthors(authors);
    return res;
}

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

@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)
@Transactional/* w  ww .ja  v  a 2 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:davmail.exchange.ews.EWSMethod.java

protected void handleMimeContent(XMLStreamReader reader, Item responseItem) throws XMLStreamException {
    if (reader instanceof TypedXMLStreamReader) {
        // Stax2 parser: use enhanced base64 conversion
        responseItem.mimeContent = ((TypedXMLStreamReader) reader).getElementAsBinary();
    } else {/*ww  w  . j av a 2 s . c  om*/
        // failover: slow and memory consuming conversion
        byte[] base64MimeContent = reader.getElementText().getBytes();
        responseItem.mimeContent = Base64.decodeBase64(base64MimeContent);
    }
}

From source file:babel.content.pages.PageVersion.java

public void unpersist(XMLStreamReader reader) throws XMLStreamException {
    String elemTag;/*from w  ww . ja  va  2 s.c  o m*/
    String elemAttrib;

    m_verProps.clear();
    ;
    m_contentMeta.clear();
    m_parseMeta.clear();
    m_outLinks = null;
    m_content = new String();

    while (true) {
        int event = reader.next();

        if (event == XMLStreamConstants.END_ELEMENT
                && XML_TAG_PAGEVERSION.equals(reader.getName().toString())) {
            break;
        }

        if (event == XMLStreamConstants.START_ELEMENT) {
            elemTag = reader.getName().toString();
            elemAttrib = reader.getAttributeValue(0);

            if ("MetaData".equals(elemTag)) {
                if ("VersionProperties".equals(elemAttrib)) {
                    m_verProps.unpersist(reader);
                } else if ("ContentMetadata".equals(elemAttrib)) {
                    m_contentMeta.unpersist(reader);
                } else if ("ParseMetadata".equals(elemAttrib)) {
                    m_parseMeta.unpersist(reader);
                }
            } else if (XML_TAG_CONTENT.equals(elemTag)) {
                //m_content = new String(Base64.decodeBase64(reader.getElementText().getBytes()));
                m_content = reader.getElementText();
            }

            //TODO: Not reading the out links
        }
    }
}

From source file:edu.indiana.d2i.htrc.portal.HTRCAgentClient.java

private Map<String, JobDetailsBean> parseJobDetailBeans(InputStream stream) throws XMLStreamException {
    Map<String, JobDetailsBean> res = new TreeMap<String, JobDetailsBean>();
    XMLStreamReader parser = factory.createXMLStreamReader(stream);
    while (parser.hasNext()) {
        int event = parser.next();
        if (event == XMLStreamConstants.START_ELEMENT) {
            if (parser.hasName()) {
                if (parser.getLocalName().equals("job_status")) {
                    // one job status
                    JobDetailsBean detail = new JobDetailsBean();
                    Map<String, String> jobParams = new HashMap<String, String>();
                    Map<String, String> results = new HashMap<String, String>();
                    int innerEvent = parser.next();
                    while (true) {
                        if (innerEvent == XMLStreamConstants.END_ELEMENT
                                && parser.getLocalName().equals("job_status")) {
                            break;
                        }/*from  w w  w.  ja v  a2s.  c om*/

                        if (innerEvent == XMLStreamConstants.START_ELEMENT && parser.hasName()) {
                            // single tag
                            if (parser.getLocalName().equals("job_name")) {
                                detail.setJobTitle(parser.getElementText());
                            } else if (parser.getLocalName().equals("user")) {
                                detail.setUserName(parser.getElementText());
                            } else if (parser.getLocalName().equals("algorithm")) {
                                detail.setAlgorithmName(parser.getElementText());
                            } else if (parser.getLocalName().equals("job_id")) {
                                detail.setJobId(parser.getElementText());
                            } else if (parser.getLocalName().equals("date")) {
                                detail.setLastUpdatedDate(parser.getElementText());
                            }

                            // parameters
                            if (parser.hasName() && parser.getLocalName().equals(JobDetailsBean.ONEPARAM)) {
                                String name, value;
                                name = value = "";
                                for (int i = 0; i < 3; i++) {
                                    if (parser.getAttributeName(i).toString().equals("name"))
                                        name = parser.getAttributeValue(i);
                                    if (parser.getAttributeName(i).toString().equals("value"))
                                        value = parser.getAttributeValue(i);
                                }
                                jobParams.put(name, value);
                            }

                            // status
                            if (parser.hasName() && parser.getLocalName().equals(JobDetailsBean.STATUS)) {
                                String status = parser.getAttributeValue(0);
                                detail.setJobStatus(status);
                            }

                            // results
                            if (parser.hasName() && parser.getLocalName().equals(JobDetailsBean.RESULT)) {
                                String name = parser.getAttributeValue(0);
                                String value = parser.getElementText();
                                results.put(name, value);
                            }

                            // message
                            if (parser.hasName() && parser.getLocalName().equals(JobDetailsBean.MESSAGE)) {
                                detail.setMessage(parser.getElementText());
                            }

                            // saved or unsaved
                            if (parser.hasName() && parser.getLocalName().equals(JobDetailsBean.SAVEDORNOT)) {
                                detail.setJobSavedStr(parser.getElementText());
                            }
                        }
                        innerEvent = parser.next();
                    }
                    detail.setJobParams(jobParams);
                    detail.setResults(results);
                    res.put(detail.getJobId(), detail);
                }
            }
        }
    }
    return res;
}

From source file:net.sf.jabref.importer.fileformat.FreeCiteImporter.java

public ParserResult importEntries(String text) {
    // URLencode the string for transmission
    String urlencodedCitation = null;
    try {/*ww  w . j a  v a  2 s.c om*/
        urlencodedCitation = URLEncoder.encode(text, StandardCharsets.UTF_8.name());
    } catch (UnsupportedEncodingException e) {
        LOGGER.warn("Unsupported encoding", e);
    }

    // Send the request
    URL url;
    URLConnection conn;
    try {
        url = new URL("http://freecite.library.brown.edu/citations/create");
        conn = url.openConnection();
    } catch (MalformedURLException e) {
        LOGGER.warn("Bad URL", e);
        return new ParserResult();
    } catch (IOException e) {
        LOGGER.warn("Could not download", e);
        return new ParserResult();
    }
    try {
        conn.setRequestProperty("accept", "text/xml");
        conn.setDoOutput(true);
        OutputStreamWriter writer = new OutputStreamWriter(conn.getOutputStream());

        String data = "citation=" + urlencodedCitation;
        // write parameters
        writer.write(data);
        writer.flush();
    } catch (IllegalStateException e) {
        LOGGER.warn("Already connected.", e);
    } catch (IOException e) {
        LOGGER.warn("Unable to connect to FreeCite online service.", e);
        return ParserResult
                .fromErrorMessage(Localization.lang("Unable to connect to FreeCite online service."));
    }
    // output is in conn.getInputStream();
    // new InputStreamReader(conn.getInputStream())
    List<BibEntry> res = new ArrayList<>();

    XMLInputFactory factory = XMLInputFactory.newInstance();
    try {
        XMLStreamReader parser = factory.createXMLStreamReader(conn.getInputStream());
        while (parser.hasNext()) {
            if ((parser.getEventType() == XMLStreamConstants.START_ELEMENT)
                    && "citation".equals(parser.getLocalName())) {
                parser.nextTag();

                StringBuilder noteSB = new StringBuilder();

                BibEntry e = new BibEntry();
                // fallback type
                EntryType type = BibtexEntryTypes.INPROCEEDINGS;

                while (!((parser.getEventType() == XMLStreamConstants.END_ELEMENT)
                        && "citation".equals(parser.getLocalName()))) {
                    if (parser.getEventType() == XMLStreamConstants.START_ELEMENT) {
                        String ln = parser.getLocalName();
                        if ("authors".equals(ln)) {
                            StringBuilder sb = new StringBuilder();
                            parser.nextTag();

                            while (parser.getEventType() == XMLStreamConstants.START_ELEMENT) {
                                // author is directly nested below authors
                                assert "author".equals(parser.getLocalName());

                                String author = parser.getElementText();
                                if (sb.length() == 0) {
                                    // first author
                                    sb.append(author);
                                } else {
                                    sb.append(" and ");
                                    sb.append(author);
                                }
                                assert parser.getEventType() == XMLStreamConstants.END_ELEMENT;
                                assert "author".equals(parser.getLocalName());
                                parser.nextTag();
                                // current tag is either begin:author or
                                // end:authors
                            }
                            e.setField(FieldName.AUTHOR, sb.toString());
                        } else if (FieldName.JOURNAL.equals(ln)) {
                            // we guess that the entry is a journal
                            // the alternative way is to parse
                            // ctx:context-objects / ctx:context-object / ctx:referent / ctx:metadata-by-val / ctx:metadata / journal / rft:genre
                            // the drawback is that ctx:context-objects is NOT nested in citation, but a separate element
                            // we would have to change the whole parser to parse that format.
                            type = BibtexEntryTypes.ARTICLE;
                            e.setField(ln, parser.getElementText());
                        } else if ("tech".equals(ln)) {
                            type = BibtexEntryTypes.TECHREPORT;
                            // the content of the "tech" field seems to contain the number of the technical report
                            e.setField(FieldName.NUMBER, parser.getElementText());
                        } else if (FieldName.DOI.equals(ln) || "institution".equals(ln) || "location".equals(ln)
                                || FieldName.NUMBER.equals(ln) || "note".equals(ln)
                                || FieldName.TITLE.equals(ln) || FieldName.PAGES.equals(ln)
                                || FieldName.PUBLISHER.equals(ln) || FieldName.VOLUME.equals(ln)
                                || FieldName.YEAR.equals(ln)) {
                            e.setField(ln, parser.getElementText());
                        } else if ("booktitle".equals(ln)) {
                            String booktitle = parser.getElementText();
                            if (booktitle.startsWith("In ")) {
                                // special treatment for parsing of
                                // "In proceedings of..." references
                                booktitle = booktitle.substring(3);
                            }
                            e.setField("booktitle", booktitle);
                        } else if ("raw_string".equals(ln)) {
                            // raw input string is ignored
                        } else {
                            // all other tags are stored as note
                            noteSB.append(ln);
                            noteSB.append(':');
                            noteSB.append(parser.getElementText());
                            noteSB.append(Globals.NEWLINE);
                        }
                    }
                    parser.next();
                }

                if (noteSB.length() > 0) {
                    String note;
                    if (e.hasField("note")) {
                        // "note" could have been set during the parsing as FreeCite also returns "note"
                        note = e.getFieldOptional("note").get().concat(Globals.NEWLINE)
                                .concat(noteSB.toString());
                    } else {
                        note = noteSB.toString();
                    }
                    e.setField("note", note);
                }

                // type has been derived from "genre"
                // has to be done before label generation as label generation is dependent on entry type
                e.setType(type);

                // autogenerate label (BibTeX key)
                LabelPatternUtil.makeLabel(
                        JabRefGUI.getMainFrame().getCurrentBasePanel().getBibDatabaseContext().getMetaData(),
                        JabRefGUI.getMainFrame().getCurrentBasePanel().getDatabase(), e, Globals.prefs);

                res.add(e);
            }
            parser.next();
        }
        parser.close();
    } catch (IOException | XMLStreamException ex) {
        LOGGER.warn("Could not parse", ex);
        return new ParserResult();
    }

    return new ParserResult(res);
}