Java Code Examples for javax.xml.stream.XMLEventReader#getElementText()

The following examples show how to use javax.xml.stream.XMLEventReader#getElementText() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Bug6586466Test.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void test() {
    String xmlData = "<?xml version=\"1.0\"?><Test>Hello</Test>";
    try {
        XMLEventReader xmlReader = XMLInputFactory.newInstance().createXMLEventReader(new ByteArrayInputStream(xmlData.getBytes()));

        XMLEvent event = xmlReader.nextEvent();
        System.out.println(event.getClass());

        // xmlReader.peek(); // error in both cases with/without peek()
        event = xmlReader.nextTag(); // nextEvent() would work fine
        // nextTag() forgets to set fLastEvent
        System.out.println(event.getClass());

        String text = xmlReader.getElementText();
        System.out.println(text);
    } catch (XMLStreamException e) {
        Assert.fail(e.getMessage());
    }
}
 
Example 2
Source File: XSSFPullParser.java    From hadoopoffice with Apache License 2.0 6 votes vote down vote up
/**
 * Parses an inline string from cell in XML format
 * 
 * @param xer XMLEventReader from which to read the inline string content
 * @throws XMLStreamException           in case the string item cannot be
 *                                      correctly read from the XML file
 * @throws FormatNotUnderstoodException in case a string cannot be identified in
 *                                      cell
 */
private String parseCellInlineStringText(XMLEventReader xer)
		throws XMLStreamException, FormatNotUnderstoodException {
	String result = "";
	XMLEvent xe;
	while ((xe = xer.nextTag()).isStartElement()) {
		String elementName = xe.asStartElement().getName().getLocalPart().toUpperCase();
		switch (elementName) {
		case "T": // normal text
			result = xer.getElementText();
			break;
		case "R": // rich text (returned as normal text)
			result = this.parseCellInlineStringRichText(xer);
			break;
		case "RPH": // phonetic (ignored)
		case "PHONETICPR": // phonetic properties (ignored)
			this.skipXMLElementHierarchy(xer);
			break;
		default:
			LOG.error("Unknown inline string tag: " + elementName);
			throw new FormatNotUnderstoodException("Unknown inline string tag: " + elementName);

		}
	}
	return result;
}
 
Example 3
Source File: XSSFPullParser.java    From hadoopoffice with Apache License 2.0 6 votes vote down vote up
/**
 * Parses a rich text item of a shared string table and returns the unformatted
 * text
 * 
 * @param xer
 * @return unformatted text of rich text item
 * @throws FormatNotUnderstoodException
 * @throws XMLStreamException
 */
private String parseCellInlineStringRichText(XMLEventReader xer)
		throws XMLStreamException, FormatNotUnderstoodException {
	String result = "";
	XMLEvent xe;
	while ((xe = xer.nextTag()).isStartElement()) {
		String elementName = xe.asStartElement().getName().getLocalPart().toUpperCase();
		switch (elementName) {
		case "T": // normal text
			result = xer.getElementText();
			break;
		case "RPR": // run properties (ignored)
		default:
			LOG.error("Unknown rich text inline string tag: " + elementName);
			throw new FormatNotUnderstoodException("Unknown rich text inline string tag: " + elementName);

		}
	}

	return result;
}
 
Example 4
Source File: EncryptedCachedDiskStringsTable.java    From hadoopoffice with Apache License 2.0 6 votes vote down vote up
/**
 * Parses a string item from a SST in XML format
 * 
 * @param xer XMLEventReader from which to read the next string item
 * @throws XMLStreamException           in case the string item cannot be
 *                                      correctly read from the XML file
 * @throws FormatNotUnderstoodException in case a string item cannot be
 *                                      identified in the shared string table
 *                                      (e.g. unknown type)
 */
private String parseSIText(XMLEventReader xer) throws XMLStreamException, FormatNotUnderstoodException {
	String result = "";
	XMLEvent xe;
	while ((xe = xer.nextTag()).isStartElement()) {
		String elementName = xe.asStartElement().getName().getLocalPart().toUpperCase();
		switch (elementName) {
		case "T": // normal text
			result = xer.getElementText();
			break;
		case "R": // rich text (returned as normal text)
			result = this.parseSIRichText(xer);
			break;
		case "RPH": // phonetic (ignored)
		case "PHONETICPR": // phonetic properties (ignored)
			this.skipXMLElementHierarchy(xer);
			break;
		default:
			LOG.error("Unknown string item in shared string table: " + elementName);
			throw new FormatNotUnderstoodException("Unknown string item in shared string table: " + elementName);

		}
	}
	return result;
}
 
Example 5
Source File: EncryptedCachedDiskStringsTable.java    From hadoopoffice with Apache License 2.0 6 votes vote down vote up
/**
 * Parses a rich text item of a shared string table and returns the unformatted
 * text
 * 
 * @param xer
 * @return unformatted text of rich text item
 * @throws FormatNotUnderstoodException
 * @throws XMLStreamException
 */
private String parseSIRichText(XMLEventReader xer) throws XMLStreamException, FormatNotUnderstoodException {
	String result = "";
	XMLEvent xe;
	while ((xe = xer.nextTag()).isStartElement()) {
		String elementName = xe.asStartElement().getName().getLocalPart().toUpperCase();
		switch (elementName) {
		case "T": // normal text
			result = xer.getElementText();
			break;
		case "RPR": // run properties (ignored)
		default:
			LOG.error("Unknown rich text string item in shared string table: " + elementName);
			throw new FormatNotUnderstoodException(
					"Unknown rich text string item in shared string table: " + elementName);

		}
	}

	return result;
}
 
Example 6
Source File: ModuleOptionParser.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Parse the module-option element
 * @param xmlEventReader
 * @return
 * @throws XMLStreamException
 */
public Map<String, Object> parse(XMLEventReader xmlEventReader) throws XMLStreamException
{
   Map<String, Object> options = new HashMap<String,Object>();
   
   //See if there are options
   while(true)
   {
      XMLEvent xmlEvent = xmlEventReader.peek();
      if(xmlEvent instanceof EndElement) break;
      StartElement peekedStartElement = (StartElement) xmlEvent;
      if(xmlEvent == null)
         break; //no module options
      
      String peekedStartElementName = StaxParserUtil.getStartElementName(peekedStartElement);
      
      if("module-option".equals(peekedStartElementName))
      {
         xmlEvent = xmlEventReader.nextEvent();
         Attribute attribute = (Attribute) peekedStartElement.getAttributes().next();
         
         //Sometime, there may be embedded xml in the option. We cannot use peek
         //next event here because the event reader jumps to the next module option
         //in the presence of a text (and not embedded xml). Since embedded xml is rare,
         //we are going to rely on exceptions as a mode of control. The issue is that
         //we have used an event filter on the XMLEventReader for convenience
         Object val = null;
         try
         {
            val = xmlEventReader.getElementText();
         }
         catch(XMLStreamException xse)
         {
            //Look for embedded xml
            XMLEvent embeddedOrText = xmlEventReader.peek();
            if(embeddedOrText.getEventType() == XMLStreamConstants.START_ELEMENT)
            { 
               val = embeddedXMLParsing(xmlEventReader); 
            }   
         } 
         options.put(attribute.getValue(), val );
      }
      else break; 
   }
   return options;
}
 
Example 7
Source File: SearchHandle.java    From java-client-api with Apache License 2.0 4 votes vote down vote up
private void handleMetrics(XMLEventReader reader, StartElement element)
    throws XMLStreamException
  {
    DatatypeFactory dtFactory;
    try {
      dtFactory = DatatypeFactory.newInstance();
    } catch (DatatypeConfigurationException dce) {
      throw new MarkLogicIOException("Cannot instantiate datatypeFactory", dce);
    }

    Calendar now = Calendar.getInstance();

    QName queryName    = new QName(SEARCH_NS, "query-resolution-time");
    QName facetName    = new QName(SEARCH_NS, "facet-resolution-time");
    QName snippetName  = new QName(SEARCH_NS, "snippet-resolution-time");
    QName metadataName = new QName(SEARCH_NS, "metadata-resolution-time");
    QName extractName  = new QName(SEARCH_NS, "extract-resolution-time");
    QName totalName    = new QName(SEARCH_NS, "total-time");

    long qrTime = -1;
    long frTime = -1;
    long srTime = -1;
    long mrTime = -1;
    long erTime  = -1;
    long tTime  = -1;

    QName metricsName = element.getName();
    events: while (reader.hasNext()) {
      XMLEvent event = reader.nextEvent();

      int eventType = event.getEventType();
      switch (eventType) {
        case XMLStreamConstants.START_ELEMENT:
          StartElement startElement = event.asStartElement();
          QName startName = startElement.getName();
          String readerValue = reader.getElementText();
   if (readerValue != null && readerValue.length() > 0) {
if (queryName.equals(startName)) {
	qrTime = parseTime(dtFactory, now, readerValue);
} else if (facetName.equals(startName)) {
	frTime = parseTime(dtFactory, now, readerValue);
} else if (snippetName.equals(startName)) {
	srTime = parseTime(dtFactory, now, readerValue);
} else if (metadataName.equals(startName)) {
	mrTime = parseTime(dtFactory, now, readerValue);
} else if (extractName.equals(startName)) {
	erTime = parseTime(dtFactory, now, readerValue);
} else if (totalName.equals(startName)) {
	tTime = parseTime(dtFactory, now, readerValue);
} else {
	logger.warn("Unexpected metrics element " + startName.toString());
}
   }
          break;
        case XMLStreamConstants.END_ELEMENT:
          if (metricsName.equals(event.asEndElement().getName())) {
            break events;
          }
          break;
      }
    }

    tempMetrics = new SearchMetricsImpl(qrTime, frTime, srTime, mrTime, erTime, tTime);
  }