List of usage examples for javax.xml.stream XMLStreamReader getText
public String getText();
From source file:Main.java
public static String parseValue(XMLStreamReader xmlRdr, String elementName) throws XMLStreamException { String val = null; while (true) { xmlRdr.next();//from w w w . j av a 2 s . com int event2 = xmlRdr.getEventType(); if (event2 == XMLStreamConstants.CHARACTERS) { val = xmlRdr.getText(); } else if (event2 == XMLStreamConstants.END_ELEMENT && xmlRdr.getLocalName().equals(elementName)) { return val; } } }
From source file:gdv.xport.util.AbstractFormatterTest.java
/** * We use the XMLStreams to validate the XML. * * @param xmlString XML-String/* w w w. j a va 2s .c om*/ * @throws XMLStreamException * the given XML string is not a valid XML */ protected static void checkXML(final String xmlString) throws XMLStreamException { XMLStreamReader xmlr = xmlInputFactory.createXMLStreamReader(new StringReader(xmlString)); int n = 0; while (xmlr.hasNext()) { int eventType = xmlr.next(); if (eventType == XMLStreamConstants.CHARACTERS) { n += xmlr.getText().length(); } } log.info(n + " bytes text in " + xmlString.length() + " bytes XML"); }
From source file:Main.java
public static void consumeStart(XMLStreamReader xmlRdr, String elementName) throws XMLStreamException { while (xmlRdr.hasNext()) { if (xmlRdr.isStartElement() && xmlRdr.getLocalName().equals(elementName)) { xmlRdr.next();/*from w w w . ja v a2 s . c om*/ return; } xmlRdr.next(); } throw new IllegalStateException( "expected start tag <" + elementName + ">, found '" + xmlRdr.getText() + "'"); }
From source file:Main.java
public static void consumeEnd(XMLStreamReader xmlRdr, String elementName) throws XMLStreamException { while (xmlRdr.hasNext()) { if (xmlRdr.isEndElement() && xmlRdr.getLocalName().equals(elementName)) { xmlRdr.next();//from w ww . jav a2 s . c o m return; } xmlRdr.next(); } throw new IllegalStateException( "expected start tag <" + elementName + ">, found '" + xmlRdr.getText() + "'"); }
From source file:Main.java
public static String nextString(XMLStreamReader reader) throws XMLStreamException { int type;/*ww w . ja va2 s .com*/ StringBuilder result = new StringBuilder(); while ((type = reader.next()) != XMLStreamReader.END_ELEMENT) { switch (type) { case XMLStreamReader.START_ELEMENT: throw new IllegalStateException("Found start element parsing text."); case XMLStreamReader.CDATA: case XMLStreamReader.SPACE: case XMLStreamReader.CHARACTERS: result.append(reader.getText()); } } return result.toString(); }
From source file:net.di2e.ecdr.libs.result.relevance.TextParser.java
/** * Given xml as a string, this method will parse out element text and CDATA text. It separates * each by one space character./*w w w . j av a 2 s . com*/ * * @param xmlData * XML as a {@code String} * @return parsed CDATA and element text */ protected static String parseTextFrom(String xmlData) { StringBuilder builder = new StringBuilder(); XMLStreamReader xmlStreamReader; try { // xml parser does not handle leading whitespace xmlStreamReader = xmlInputFactory.createXMLStreamReader(new StringReader(xmlData)); while (xmlStreamReader.hasNext()) { int event = xmlStreamReader.next(); if (event == XMLStreamConstants.CHARACTERS || event == XMLStreamConstants.CDATA) { String text = xmlStreamReader.getText(); if (StringUtils.isNotBlank(text)) { builder.append(" " + text.trim()); } } if (event == XMLStreamConstants.START_ELEMENT) { for (int i = 0; i < xmlStreamReader.getAttributeCount(); i++) { String text = xmlStreamReader.getAttributeValue(i); if (StringUtils.isNotBlank(text)) { builder.append(" " + text.trim()); } } } } } catch (XMLStreamException e1) { LOGGER.warn("Failure occurred in parsing the xml data (" + xmlData + "). No data has been stored or indexed.", e1); } return builder.toString(); }
From source file:Main.java
private static void process(XMLStreamReader reader) { int eventType = reader.getEventType(); switch (eventType) { case XMLStreamConstants.START_ELEMENT: System.out.println("Start element: " + reader.getLocalName()); int count = reader.getAttributeCount(); for (int i = 0; i < count; i++) { String name = reader.getAttributeLocalName(i); String value = reader.getAttributeValue(i); System.out.println("\tAttribute name/value: " + name + "/" + value); }//w w w. ja va 2s . co m break; case XMLStreamConstants.END_ELEMENT: System.out.println("End element: " + reader.getLocalName()); break; case XMLStreamConstants.CHARACTERS: System.out.println("Text: " + reader.getText()); break; default: break; } }
From source file:XMLStreamReaderDemo.java
private static void process(XMLStreamReader reader) { int eventType = reader.getEventType(); switch (eventType) { case XMLStreamConstants.START_ELEMENT: System.out.println("Start element: " + reader.getLocalName()); int count = reader.getAttributeCount(); for (int i = 0; i < count; i++) { String name = reader.getAttributeLocalName(i); String value = reader.getAttributeValue(i); System.out.println("\tAttribute name/value: " + name + "/" + value); }//from w w w . ja v a2s . c o m break; case XMLStreamConstants.END_ELEMENT: System.out.println("End element: " + reader.getLocalName()); break; case XMLStreamConstants.CHARACTERS: System.out.println("Text: " + reader.getText()); break; default: break; } }
From source file:com.microsoft.windowsazure.services.table.client.AtomPubParser.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 a2 s .c om * @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. */ protected static HashMap<String, EntityProperty> readProperties(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:edu.utah.further.core.api.xml.XmlUtil.java
/** * Basic StAX element printout. For more sophisticated functionality, use * {@link XmlStreamPrinter}.//from ww w . j a v a 2s . co m * * @param reader * reader * @return reader's next element textual representation */ public static String getEventSimpleString(final XMLStreamReader reader) { switch (reader.getEventType()) { case XMLStreamConstants.START_ELEMENT: return "START_ELEMENT:\t\"" + reader.getLocalName() + "\""; case XMLStreamConstants.END_ELEMENT: return "END_ELEMENT:\t\"" + reader.getLocalName() + "\""; case XMLStreamConstants.START_DOCUMENT: return "START_DOCUMENT"; case XMLStreamConstants.END_DOCUMENT: return "END_DOCUMENT"; case XMLStreamConstants.CHARACTERS: return "CHARACTERS:\t\"" + reader.getText() + "\"" + " blank? " + StringUtils.isWhitespace(reader.getText()); case XMLStreamConstants.SPACE: return "SPACE:\t\"" + reader.getText() + "\""; default: return "EVENT:\t" + reader.getEventType(); } }