I just have this question around the Stax XMLStreamWriter. Best explained by example:
Say I need to produce a document somehow like this:
<buddies>
<buddies name="tim"/>
<buddies name="toady"/>
</buddies>
, where the tim and toady guys are created by some callback that receives the XMLStreamWriter as an argument.
writer.writeElement("buddies");
callback1.writeBuddies(writer);
callback2.writeBuddies(writer);
write.writeEndElement();
Now the thing is, the whole document must conform to a schema that states: If there's a <buddies>
element, there must be at least one <buddy>
inside, so if none of my callbacks write anything on the stream I'd have an empty element which is invalid. Question is: Can I delay the writeElement("buddies")
somehow like this:
// Pseudocode
Mark mark = writer.getPos()
boolean written = callback1.writeBuddies(writer)
written |= callback2.writeBuddies(writer)
if (written){
writer.writeStartElement(mark, "buddies") // write at mark
writer.writeStopElement() // write at the end of stream
}
Or am I completely off the track?
StAX is a forward only streaming API. To achieve what you are asking is a potential requirement for DOM parser where you construct the node "buddies" separately and attach it to the main document if it is not empty.