tags:

views:

64

answers:

1

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?

+1  A: 

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?

Joachim Sauer
interesting - i'm using Java 5...
Drew Johnson
@Drew: as far as I know that must mean that you're using a third-party StAX implementation because it only became part of the JDK in Java 6. So I'd see if there is a update for that library or if it is a known bug.
Joachim Sauer
@Joachim: Thanks for the help and putting me on the right path. The problem was not related to StAX, as you pointed out. I was manipulating the xml prior to sending it to StAX (escaping the first "<"), which was causing the issue.FYI...it looks like StAX was included in Java 5 (http://java.sun.com/javaee/5/docs/api/javax/xml/stream/package-summary.html)
Drew Johnson
@Drew: you linked to the documentation of Java EE 5. I'm talking about Java SE: http://java.sun.com/javase/6/docs/api/javax/xml/stream/XMLInputFactory.html mentions "Since: 1.6"
Joachim Sauer