??????????????XML??С?????????
XMLInputFactory xif = XMLInputFactory.newInstance();
XMLStreamReader xsr = xif.createXMLStreamReader(new FileReader("input.xml")); // ??Stax???XML
xsr.nextTag(); // Advance to statements element
TransformerFactory tf = TransformerFactory.newInstance();
Transformer t = tf.newTransformer();
t.setParameter(OutputKeys.OMIT_XML_DECLARATION?? "no");
t.setParameter(OutputKeys.STANDALONE?? "yes");
long i = 0;
String action = null;
while (xsr.hasNext()) {
if (xsr.next() == XMLStreamConstants.START_ELEMENT) {
if ("ContentItem".equals(xsr.getLocalName())) {
action = getAttributeValue(xsr?? "action");
} else if ("Data".equals(xsr.getLocalName())) {
File file = new File("out/" + action + i++ + ".xml");
t.transform(new StAXSource(xsr)?? new StreamResult(file)); // ????任??????~
// DOMResult dr = new DOMResult(); // ??????Dom??????releaseMe
// t.transform(new StAXSource(xsr)?? dr);
}
}
}
?????????????????????????XML?????????String???棬???????????????????XML???????????????ouputStream?String??????
ByteArrayOutputStream baos = new ByteArrayOutputStream();
t.transform(new StAXSource(xsr)?? new StreamResult(baos));
String subXMLStr = baos.toString();
?????????з??????з???????????????????