XML Parser in Android
I'm trying to extract n0Y7ezLlIYA8R0K54rEmHaTOraBQVSPDjQaGlQxlGso4jdVN1kRxtcfskEs=
using w3c dom
<html>
<div id='token' style='display:none;'>
n0Y7ezLlIYA8R0K54rEmHaTOraBQVSPDjQaGlQxlGso4jdVN1kRxtcfskEs=
</div>
</html>
but I seem to be stuck
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document doc = builder.parse(con.getInputStream());
NodeList list = doc.getElementsByTagName("div");
Edit:
I got it to work but it seems a little bit clunky:String token;
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document doc = builder.parse(con.getInputStream());
Element root = doc.getDocumentElement();
NodeList items = root.getElementsByTagName("html");
for(int i = 0; i < items.getLength(); i++) {
Message message = new Message();
Node item = items.item(i);
NodeList properties = item.getChildNodes();
for(in开发者_如何学Got j = 0; j < properties.getLength(); j++) {
Node property = properties.item(j);
String name = property.getNodeName();
if(name.equalsIgnoreCase("div")) {
token = property.getFirstChild().getNodeValue());
}
}
}
Is there a prettier way to get the token?
VTDGen vg= new VTDGen();
if (vg.parseFile("input.xml",false)){
VTDNav vn = vg.getNav();
vn.toElement(VTDNav.FIRST_CHILD);
int i = vn.getText();
if (i!=-1)
System.out.println(" text node is "+vn.toString(i));
}
In my case VTD-XML parser served the best although my cml documents are not that huge, some sample code using VTD-XML are given below. You can refer the below links which explain that VTD-XML parser is better than SAX, DOM, etc parsers as they have given performance benchmarks as well. http://www.codeproject.com/Articles/28237/Programming-XPath-with-VTD-XML http://www.codeproject.com/Articles/24354/VTD-XML-XML-Processing-for-the-Future-Part-II
// For reading xpath values
public String readXpathValue(String dir, String file, String xpath) {
String value = null;
try{
VTDGen vg = new VTDGen();
int i;
AutoPilot ap = new AutoPilot();
ap.selectXPath(xpath);
if (vg.parseFile(dir+file, true))
{
VTDNav vn = vg.getNav();
ap.bind(vn);
//XPath eval returns one node at a time
while ((i = ap.evalXPath()) != -1)
{
value = vn.toString(i);
}
// ap.resetXPath();
}
}
catch (Exception e){
System.out.println("Exception Occurred in reading Xpath Value : "+e);
}
return value;
}
// For modifying xml file at runtime
public void formCreateXMLRequest(MAMData mamData,Map<String, String> strTobeModified) throws DatatypeConfigurationException, PayPalUserCreationFailedException, ModifyException, TranscodeException, IOException, XPathEvalException, NavException, XPathParseException
{
VTDGen vg = new VTDGen();
if (!vg.parseFile(mamData.getDirectory() + mamData.getBatchRequest(), true))
return;
VTDNav vn = vg.getNav();
XMLModifier xm = new XMLModifier(vn);
AutoPilot ap = new AutoPilot(vn);
Set<String> xpathkeys= strTobeModified.keySet();
for(String xpath : xpathkeys) {
ap.selectXPath(xpath);
while((ap.evalXPath()) != -1)
{
int p = vn.getText();
xm.updateToken(p, strTobeModified.get(xpath));
}
xm.output(mamData.getDirectory()+mamData.getBatchRequest());
}
}
精彩评论