Thursday, September 23, 2010

Generic Xml Parsing in Android

Here's the code

public void genericXmlParsing(String raw) throws Exception{
DocumentBuilder builder = DocumentBuilderFactory
.newInstance()
.newDocumentBuilder();
Document doc = builder.parse(new InputSource(new StringReader(raw)));
NodeList elementNames = doc.getElementsByTagName("ElementName");
NodeList elementValues = doc.getElementsByTagName("ElementValue");
for(int i=0;i Element elementName = (Element)elementNames.item(i);
ParsedXmlData parsedXmlData = new ParsedXmlData();
parsedXmlData.setElementName(elementName.getFirstChild().getNodeValue());
parsedXmlDataList.add(parsedXmlData);
}
for(int i=0;i Element elementValue = (Element)elementValues.item(i);
ParsedXmlData parsedXmlData = parsedXmlDataList.get(i);
parsedXmlData.setElementValue(elementValue.getFirstChild().getNodeValue());
}
}

2 comments:

  1. Hi thanks for reply,
    I have done this bt not able to display the List of attributes in XML file
    How it will code in oncreate() method.
    pls help me regard this..

    ReplyDelete
  2. How to parse such a file?? please any help??




    www.codemobiles.com, since



    www.codemobiles.com1, since

    ReplyDelete