Search code examples
javaxmlstax

Stax processing instruction


I'm trying to parse a processing instruction like this using StAX:

<?item something="<some_element></some_element>"?>

StAX doesn't appear to recognize this as a processing instruction. It finds these events:

< - CharacterEvent
?item something=" - CharacterEvent
<some_element> - StartEvent
</some_element> - EndEvent
"?> - CharacterEvent

Shouldn't the whole thing be considered a single ProcessingInstruction event?


Solution

  • Works as expected for me. This code:

    String xml = "<?item something=\"<some_element></some_element>\"?><foo></foo>";
    XMLEventReader reader = XMLInputFactory.newInstance().createXMLEventReader(new StringReader(xml));
    while (reader.hasNext()) {
        final XMLEvent event = reader.nextEvent();
        System.out.println(event + " - " + event.getClass().getSimpleName());
    }
    

    Prints this:

    <?xml version="1.0" encoding='null' standalone='no'?> - StartDocumentEvent
    <?itemsomething="<some_element></some_element>"?> - ProcessingInstructionEvent
    <foo> - StartElementEvent
    </foo> - EndElementEvent
    ENDDOCUMENT - EndDocumentEvent
    

    That is with Java 6. What Java version are you using?