上周,我开始学习StaX,但如果你仍在寻求解决你的问题,这可能有助于你:
XMLInputFactory xmlif = XMLInputFactory.newInstance();
xmlif.setProperty(XMLInputFactory.IS_COALESCING, true);
XMLStreamReader xmlsr;
String resultat="";
boolean isGetme=false;
try {
xmlsr = xmlif.createXMLStreamReader(new FileReader("lib/toto.xml"));
int eventType;
while (xmlsr.hasNext()) {
eventType = xmlsr.next();
switch (eventType) {
case XMLStreamConstants.START_ELEMENT:
if(xmlsr.getLocalName().equals("getMe")){
isGetme=true;
}
if(isGetme){
resultat+="<"+xmlsr.getLocalName()+">";
}
break;
case XMLStreamConstants.CHARACTERS:
if(isGetme){
resultat+=xmlsr.getText();
}
break;
case XMLStreamConstants.END_ELEMENT:
if(xmlsr.getLocalName().equals("getMe")){
resultat+="</"+xmlsr.getLocalName()+">";
isGetme=false;
}
if(isGetme && !xmlsr.getLocalName().equals("getMe")){
resultat+="</"+xmlsr.getLocalName()+">";
}
break;
default:
break;
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (XMLStreamException e) {
e.printStackTrace();
}
System.out.println(resultat);
This code will get you all the inner nodes and text contents, and store it into a String.
I.E:
<getMe>
<data>test</data>
</getMe>
然而,这部法律“格外”而且肯定不是你所期望的最佳解决办法,因此,联索行动可能更适合你们的需要。