Using Dom Parser In Android
I'm using the DOM parser to retrive information from a XML file that looks like this: &l
Solution 1:
Pls take care whether your XML file is well formed or not,
You have to the notice three methods which i had shown below, they are
1. getElementsByTagName - Mention the tag which you want to parse
2.getChildNodes - retervies the child node
3.getNodeValue()- with the help of this method you can access the
value of particular tag
Step 1: Create a Method to parse _Information_Value ,inorder to parse the data of Information tag
String[] infoId=null;
publicvoidparse_Information_Value()throws UnknownHostException{
DocumentBuilderFactoryfactory= DocumentBuilderFactory.newInstance();
try {
DocumentBuilderbuilder= factory.newDocumentBuilder();
Documentdom= builder.parse(this.getInputStream());
org.w3c.dom.Elementroot= dom.getDocumentElement();
NodeListitems= root.getElementsByTagName("metData");
int a=items.getLength();
int k=0;
for (inti=0; i < items.getLength(); i++) {
Message_categorymessage=newMessage_category();
Nodeitem= items.item(i);
NodeListproperties= item.getChildNodes();
for (intj=0; j < properties.getLength(); j++) {
Nodeproperty= properties.item(j);
Stringname= property.getNodeName();
if (name.equalsIgnoreCase("wantedInformation")) {
message.setId(property.getFirstChild()
.getNodeValue());
infoId[k]=property.getFirstChild().getNodeValue();
k++;
}
}
}
} catch (Exception e) { }
}
Solution 2:
Depending on the size of your document, you may also want to use at a streaming oriented parser like SAX or Stax, which does not pull the whole document into memory and thus needs less memory than DOM.
Good thing is that SAX is already built into Android, so you can use it right away. See this link for a usage example.
Post a Comment for "Using Dom Parser In Android"