use of javax.xml.stream.events.ProcessingInstruction in project sirix by sirixdb.
the class XMLShredder method insertNewContent.
/**
* Insert new content based on a StAX parser {@link XMLStreamReader}.
*
* @throws SirixException if something went wrong while inserting
*/
protected final void insertNewContent() throws SirixException {
try {
boolean firstElement = true;
int level = 0;
QName rootElement = null;
boolean endElemReached = false;
final StringBuilder sBuilder = new StringBuilder();
long insertedRootNodeKey = -1;
// Iterate over all nodes.
while (mReader.hasNext() && !endElemReached) {
final XMLEvent event = mReader.nextEvent();
switch(event.getEventType()) {
case XMLStreamConstants.START_ELEMENT:
level++;
addNewElement(event.asStartElement());
if (firstElement) {
firstElement = false;
insertedRootNodeKey = mWtx.getNodeKey();
rootElement = event.asStartElement().getName();
}
break;
case XMLStreamConstants.END_ELEMENT:
level--;
if (level == 0 && rootElement != null && rootElement.equals(event.asEndElement().getName())) {
endElemReached = true;
}
final QName name = event.asEndElement().getName();
processEndTag(new QNm(name.getNamespaceURI(), name.getPrefix(), name.getLocalPart()));
break;
case XMLStreamConstants.CHARACTERS:
if (mReader.peek().getEventType() == XMLStreamConstants.CHARACTERS) {
sBuilder.append(event.asCharacters().getData().trim());
} else {
sBuilder.append(event.asCharacters().getData().trim());
processText(sBuilder.toString());
sBuilder.setLength(0);
}
break;
case XMLStreamConstants.COMMENT:
if (mIncludeComments) {
processComment(((Comment) event).getText());
}
break;
case XMLStreamConstants.PROCESSING_INSTRUCTION:
if (mIncludePIs) {
final ProcessingInstruction pi = (ProcessingInstruction) event;
processPI(pi.getData(), pi.getTarget());
}
break;
default:
}
}
mWtx.moveTo(insertedRootNodeKey);
} catch (final XMLStreamException e) {
throw new SirixIOException(e);
}
}
Aggregations