List of usage examples for javax.xml.stream XMLStreamConstants START_ELEMENT
int START_ELEMENT
To view the source code for javax.xml.stream XMLStreamConstants START_ELEMENT.
Click Source Link
From source file:edu.indiana.d2i.htrc.io.index.solr.SolrClient.java
public List<String> getIDList(String queryStr) { List<String> idlist = new ArrayList<String>(); try {/*from w w w . j a v a 2 s . c om*/ // get num of hits String url = mainURL + QUERY_PREFIX + queryStr; System.out.println(url); HttpGet getRequest = new HttpGet(url); HttpResponse response = httpClient.execute(getRequest); InputStream content = response.getEntity().getContent(); int numFound = 0; XMLStreamReader parser = factory.createXMLStreamReader(content); while (parser.hasNext()) { int event = parser.next(); if (event == XMLStreamConstants.START_ELEMENT) { String attributeValue = parser.getAttributeValue(null, NUM_FOUND); if (attributeValue != null) { numFound = Integer.valueOf(attributeValue); break; } } } content.close(); // String idurl = mainURL + QUERY_PREFIX + queryStr + "&start=0&rows=" + numFound; // String idurl = mainURL + QUERY_PREFIX + queryStr + "&start=0&rows=" + 10; HttpResponse idresponse = httpClient.execute(new HttpGet(idurl)); InputStream idcontent = idresponse.getEntity().getContent(); XMLStreamReader idparser = factory.createXMLStreamReader(idcontent); while (idparser.hasNext()) { int event = idparser.next(); if (event == XMLStreamConstants.START_ELEMENT) { String attributeValue = idparser.getAttributeValue(null, "name"); if (attributeValue != null) { if (attributeValue.equals(SOLR_QUERY_ID)) { String volumeId = idparser.getElementText(); idlist.add(volumeId); } } } } idcontent.close(); } catch (ClientProtocolException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (XMLStreamException e) { // TODO Auto-generated catch block e.printStackTrace(); } return idlist; }
From source file:de.ii.xtraplatform.ogc.api.gml.parser.GMLParser.java
private void parseException(SMInputCursor cursor) throws XMLStreamException { SMFlatteningCursor body = (SMFlatteningCursor) cursor.descendantElementCursor().advance(); String exceptionCode = ""; String exceptionText = ""; while (body.readerAccessible()) { if (body.getCurrEventCode() == XMLStreamConstants.START_ELEMENT) { if (body.getLocalName().equals(WFS.getWord(WFS.VOCABULARY.EXCEPTION))) { exceptionCode = body.getAttrValue(WFS.getWord(WFS.VOCABULARY.EXCEPTION_CODE)); }// ww w. j a va 2s. c o m if (body.getLocalName().equals(WFS.getWord(WFS.VOCABULARY.EXCEPTION_TEXT))) { exceptionText = body.collectDescendantText(); } } body = (SMFlatteningCursor) body.advance(); } LOGGER.error("Exception coming from WFS: '{} {}'.", exceptionCode, exceptionText); WFSException wfse = new WFSException("Exception coming from WFS: '{}'.", exceptionCode); wfse.addDetail(exceptionText); throw wfse; }
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;/*www .j a va 2s . com*/ 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:com.revolsys.record.io.format.xml.XmlProcessor.java
@SuppressWarnings("unchecked") public <T> T parseObject(final StaxReader parser, final Class<? extends T> objectClass) throws IOException { try {//from ww w . j a v a 2s .c om if (objectClass == null) { Object object = null; while (parser.nextTag() == XMLStreamConstants.START_ELEMENT) { if (object != null) { throw new IllegalArgumentException( "Expecting a single child element " + parser.getLocation()); } object = process(parser); } return (T) object; } else { final T object = objectClass.newInstance(); if (object instanceof Collection) { final Collection<Object> collection = (Collection<Object>) object; while (parser.nextTag() == XMLStreamConstants.START_ELEMENT) { final Object value = process(parser); collection.add(value); } } else { while (parser.nextTag() == XMLStreamConstants.START_ELEMENT) { final String tagName = parser.getName().getLocalPart(); final Object value = process(parser); try { String propertyName; if (tagName.length() > 1 && Character.isLowerCase(tagName.charAt(1))) { propertyName = CaseConverter.toLowerFirstChar(tagName); } else { propertyName = tagName; } BeanUtils.setProperty(object, propertyName, value); } catch (final Throwable e) { e.printStackTrace(); } } } return object; } } catch (final InstantiationException e) { throw new IllegalArgumentException(e); } catch (final IllegalAccessException e) { throw new IllegalArgumentException(e); } }
From source file:ca.uhn.fhir.parser.XmlParser.java
private <T> T doXmlLoop(XMLEventReader streamReader, ParserState<T> parserState) { ourLog.trace("Entering XML parsing loop with state: {}", parserState); try {//from w w w .j a v a 2 s . co m List<String> heldComments = new ArrayList<String>(1); while (streamReader.hasNext()) { XMLEvent nextEvent = streamReader.nextEvent(); try { switch (nextEvent.getEventType()) { case XMLStreamConstants.START_ELEMENT: { StartElement elem = nextEvent.asStartElement(); String namespaceURI = elem.getName().getNamespaceURI(); if ("extension".equals(elem.getName().getLocalPart())) { Attribute urlAttr = elem.getAttributeByName(new QName("url")); String url; if (urlAttr == null || isBlank(urlAttr.getValue())) { getErrorHandler().missingRequiredElement(new ParseLocation("extension"), "url"); url = null; } else { url = urlAttr.getValue(); } parserState.enteringNewElementExtension(elem, url, false); } else if ("modifierExtension".equals(elem.getName().getLocalPart())) { Attribute urlAttr = elem.getAttributeByName(new QName("url")); String url; if (urlAttr == null || isBlank(urlAttr.getValue())) { getErrorHandler().missingRequiredElement(new ParseLocation("modifierExtension"), "url"); url = null; } else { url = urlAttr.getValue(); } parserState.enteringNewElementExtension(elem, url, true); } else { String elementName = elem.getName().getLocalPart(); parserState.enteringNewElement(namespaceURI, elementName); } if (!heldComments.isEmpty()) { for (String next : heldComments) { parserState.commentPre(next); } heldComments.clear(); } @SuppressWarnings("unchecked") Iterator<Attribute> attributes = elem.getAttributes(); for (Iterator<Attribute> iter = attributes; iter.hasNext();) { Attribute next = iter.next(); parserState.attributeValue(next.getName().getLocalPart(), next.getValue()); } break; } case XMLStreamConstants.END_DOCUMENT: case XMLStreamConstants.END_ELEMENT: { if (!heldComments.isEmpty()) { for (String next : heldComments) { parserState.commentPost(next); } heldComments.clear(); } parserState.endingElement(); // if (parserState.isComplete()) { // return parserState.getObject(); // } break; } case XMLStreamConstants.CHARACTERS: { parserState.string(nextEvent.asCharacters().getData()); break; } case XMLStreamConstants.COMMENT: { Comment comment = (Comment) nextEvent; String commentText = comment.getText(); heldComments.add(commentText); break; } } parserState.xmlEvent(nextEvent); } catch (DataFormatException e) { throw new DataFormatException("DataFormatException at [" + nextEvent.getLocation().toString() + "]: " + e.getMessage(), e); } } return parserState.getObject(); } catch (XMLStreamException e) { throw new DataFormatException(e); } }
From source file:com.microsoft.alm.plugin.context.soap.CatalogServiceImpl.java
private void readResponse(final HttpResponse httpResponse, final ElementDeserializable readFromElement) { InputStream responseStream = null; try {/*from www . ja va 2 s .co m*/ final Header encoding = httpResponse.getFirstHeader("Content-Encoding"); //$NON-NLS-1$ if (encoding != null && encoding.getValue().equalsIgnoreCase("gzip")) //$NON-NLS-1$ { responseStream = new GZIPInputStream(httpResponse.getEntity().getContent()); } else { responseStream = httpResponse.getEntity().getContent(); } XMLStreamReader reader = null; try { reader = XML_INPUT_FACTORY.createXMLStreamReader(responseStream); final QName envelopeQName = new QName(SOAP, "Envelope", "soap"); //$NON-NLS-1$ //$NON-NLS-2$ final QName headerQName = new QName(SOAP, "Header", "soap"); //$NON-NLS-1$ //$NON-NLS-2$ final QName bodyQName = new QName(SOAP, "Body", "soap"); //$NON-NLS-1$ //$NON-NLS-2$ // Read the envelope. if (reader.nextTag() == XMLStreamConstants.START_ELEMENT && reader.getName().equals(envelopeQName)) { while (reader.nextTag() == XMLStreamConstants.START_ELEMENT) { if (reader.getName().equals(headerQName)) { // Ignore headers for now. readUntilElementEnd(reader); } else if (reader.getName().equals(bodyQName)) { if (reader.nextTag() == XMLStreamConstants.START_ELEMENT && reader.getName().getLocalPart().equals(QUERY_NODE_RESPONSE)) { readFromElement.readFromElement(reader); return; } } } } } catch (final XMLStreamException e) { logger.warn("readResponse", e); throw new RuntimeException(e); } finally { if (reader != null) { try { reader.close(); } catch (final XMLStreamException e) { // Ignore and continue } } } } catch (IOException e) { logger.warn("readResponse", e); throw new RuntimeException(e); } finally { if (responseStream != null) { try { responseStream.close(); } catch (IOException e) { // Ignore and continue } } } }
From source file:com.microsoft.windowsazure.services.table.client.AtomPubParser.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 ww w. jav a2s. c o 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") protected static <T extends TableEntity, R> ODataPayload<?> parseResponse(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 = parseEntity(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:org.javelin.sws.ext.bind.internal.model.ComplexTypePattern.java
@Override public T consumeValue(XMLEventReader eventReader, UnmarshallingContext context) throws XMLStreamException { // first create an object to be filled (using PropertyAccessors - direct or bean) according to the content model T object = BeanUtils.instantiate(this.getJavaType()); // the order is dictated by incoming events, not by the mode // TODO: create a property to enable strict unmarshalling - dictated by content model // only this (ContentModel) pattern iterates over XML Events XMLEvent event = null;//from w w w. j a va2 s .c o m PropertyMetadataValue<T, ?> pmv = null; // this loop will only handle first level of start elements and only single end element // deeper levels will be handled by nested patterns while (true) { boolean end = false; event = eventReader.peek(); pmv = null; switch (event.getEventType()) { case XMLStreamConstants.ATTRIBUTE: pmv = this.consumeNestedAttribute(eventReader, context); break; case XMLStreamConstants.CDATA: case XMLStreamConstants.CHARACTERS: // TODO: XMLEvent.ENTITY_REFERENCE? if (this.simpleContent != null) { pmv = this.consumeSimpleContent(eventReader, context); break; } case XMLStreamConstants.COMMENT: case XMLStreamConstants.DTD: case XMLStreamConstants.SPACE: case XMLStreamConstants.ENTITY_DECLARATION: case XMLStreamConstants.NOTATION_DECLARATION: case XMLStreamConstants.PROCESSING_INSTRUCTION: eventReader.nextEvent(); break; case XMLStreamConstants.ENTITY_REFERENCE: // TODO: XMLEvent.ENTITY_REFERENCE? eventReader.nextEvent(); break; case XMLStreamConstants.START_DOCUMENT: // strange break; case XMLStreamConstants.START_ELEMENT: pmv = this.consumeNestedElement(eventReader, context); break; case XMLStreamConstants.END_ELEMENT: // TODO: in mixed content there will be more than one end element it this content model's level case XMLStreamConstants.END_DOCUMENT: end = true; break; } if (end) break; if (pmv != null) pmv.getMetadata().setValue(object, pmv.getValue()); } return (T) object; }
From source file:net.cloudkit.enterprises.ws.SuperPassQueryTest.java
public static String parsingReceiptStatus(String responseContext) throws XMLStreamException { String serviceResponseCode = "-1"; XMLStreamReader reader = factory.createXMLStreamReader(new StringReader(responseContext)); try {/*w w w . j a va 2 s. com*/ int event = reader.getEventType(); while (true) { switch (event) { case XMLStreamConstants.START_ELEMENT: // System.out.println(reader.getName()); if (reader.getName().toString().equals("ServiceResponseCode")) { // System.out.println(reader.getElementText()); serviceResponseCode = reader.getElementText(); } break; case XMLStreamConstants.END_ELEMENT: // System.out.println("End Element:" + r.getName()); break; } if (!reader.hasNext()) break; event = reader.next(); } } finally { reader.close(); } return serviceResponseCode; }
From source file:babel.content.pages.PageVersion.java
public void unpersist(XMLStreamReader reader) throws XMLStreamException { String elemTag;/*from www. j a v a2 s.c om*/ 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 } } }