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

The following examples show how to use javax.xml.stream.XMLEventReader#nextTag() . 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: Bug6668115Test.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * The reason the following call sequence is a problem is that with a
 * peekevent, getElementText calls nextEvent which does properly update the
 * lastEvent
 */
@Test
public void testNextTag() {
    try {
        XMLEventReader er = getReader();
        er.nextTag();
        er.nextTag();

        System.out.println(er.getElementText());
        er.nextTag();
        System.out.println(er.getElementText());

    } catch (Exception e) {
        System.out.println(e.getMessage());
        e.printStackTrace();
        Assert.fail(e.getMessage());
    }
}
 
Example 2
Source File: Bug6668115Test.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testNextTagWPeek() {
    try {
        XMLEventReader er = getReader();
        er.nextTag();
        er.nextTag();

        er.peek();
        System.out.println(er.getElementText());
        er.nextTag();
        System.out.println(er.getElementText());

    } catch (Exception e) {
        System.out.println(e.getMessage());
        e.printStackTrace();
        Assert.fail(e.getMessage());
    }
}
 
Example 3
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 4
Source File: StaxEventXMLReaderTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void partial() throws Exception {
	XMLInputFactory inputFactory = XMLInputFactory.newInstance();
	XMLEventReader eventReader = inputFactory.createXMLEventReader(new StringReader(CONTENT));
	eventReader.nextTag();  // skip to root
	StaxEventXMLReader xmlReader = new StaxEventXMLReader(eventReader);
	ContentHandler contentHandler = mock(ContentHandler.class);
	xmlReader.setContentHandler(contentHandler);
	xmlReader.parse(new InputSource());
	verify(contentHandler).startDocument();
	verify(contentHandler).startElement(eq("http://springframework.org/spring-ws"), eq("child"), eq("child"), any(Attributes.class));
	verify(contentHandler).endElement("http://springframework.org/spring-ws", "child", "child");
	verify(contentHandler).endDocument();
}
 
Example 5
Source File: StaxEventXMLReaderTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void partial() throws Exception {
	XMLInputFactory inputFactory = XMLInputFactory.newInstance();
	XMLEventReader eventReader = inputFactory.createXMLEventReader(new StringReader(CONTENT));
	eventReader.nextTag();  // skip to root
	StaxEventXMLReader xmlReader = new StaxEventXMLReader(eventReader);
	ContentHandler contentHandler = mock(ContentHandler.class);
	xmlReader.setContentHandler(contentHandler);
	xmlReader.parse(new InputSource());
	verify(contentHandler).startDocument();
	verify(contentHandler).startElement(eq("http://springframework.org/spring-ws"), eq("child"), eq("child"), any(Attributes.class));
	verify(contentHandler).endElement("http://springframework.org/spring-ws", "child", "child");
	verify(contentHandler).endDocument();
}
 
Example 6
Source File: XmlJavaxFormat.java    From jigsaw-payment with Apache License 2.0 5 votes vote down vote up
private Object handleObject(XMLEventReader parser,
        XMLEvent startEvent,
        ExtensionRegistry extensionRegistry, 
        Message.Builder builder,
        FieldDescriptor field, 
        ExtensionRegistry.ExtensionInfo extension) throws XMLStreamException {

    Message.Builder subBuilder = createSubBuilder(builder, field, extension);

    XMLEvent event = startEvent;
    int depth = 0; // initialize to 0
    do {
        if (event.isStartElement()) {
            depth++; // we're inside the element
            mergeField(parser, event, 
                    extensionRegistry, subBuilder);
            XMLEvent nextEvent = parser.nextTag();
            
            if (nextEvent.isEndElement()) {
                depth--;
                // if we're back under the top level obj, and there is another close, we're done.
                if (depth <= 0 && parser.peek().isEndElement()) {
                    break;
                }
            } else if (nextEvent.isStartElement()) {
                depth++;
            }
        } else {
            // something is potentially wrong..
            break;
        }
    } while (parser.hasNext() && (event = parser.nextTag()) != null);
    
    return subBuilder.build();
}
 
Example 7
Source File: EventFilterSupportTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
private static void testNextTag(int max)
        throws IOException, XMLStreamException {
    System.out.println("\nTest nextTag (" + max + ")...");
    XMLEventReader reader = createXmlReader(max);
    XMLEvent event;
    do {
        event = reader.nextTag();
        System.out.println(event);
        if (event.getEventType() == XMLEvent.END_ELEMENT
            && event.asEndElement().getName().getLocalPart().equals(ROOT)) {
            break;
        }
    } while (true);
    System.out.println("nextTag passed\n");
}
 
Example 8
Source File: StaxEventXMLReaderTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void partial() throws Exception {
	XMLInputFactory inputFactory = XMLInputFactory.newInstance();
	XMLEventReader eventReader = inputFactory.createXMLEventReader(new StringReader(CONTENT));
	eventReader.nextTag(); // skip to root
	StaxEventXMLReader xmlReader = new StaxEventXMLReader(eventReader);
	ContentHandler contentHandler = mock(ContentHandler.class);
	xmlReader.setContentHandler(contentHandler);
	xmlReader.parse(new InputSource());
	verify(contentHandler).startDocument();
	verify(contentHandler).startElement(eq("http://springframework.org/spring-ws"), eq("child"), eq("child"), any(Attributes.class));
	verify(contentHandler).endElement("http://springframework.org/spring-ws", "child", "child");
	verify(contentHandler).endDocument();
}
 
Example 9
Source File: AttrXmlResourceValue.java    From bazel with Apache License 2.0 4 votes vote down vote up
private static void endAttrElement(XMLEventReader reader) throws XMLStreamException {
  XMLEvent endTag = reader.nextTag();
  if (!endTag.isEndElement() || !QName.valueOf("attr").equals(endTag.asEndElement().getName())) {
    throw new XMLStreamException("Unexpected ParentTag:" + endTag, endTag.getLocation());
  }
}