1

I have a XML like this:

<thoughts>
    <thought>
        <id>1</id>
        <category>Leadership</category>
        <what>sometext</what>            
        <who>sometext</who>
    </thought>
    <thought>
        <id>2</id>
        <category>Leadership</category>
        <what>sometext</what>            
        <who>sometext</who>
    </thought>
    ... 100's of with category Leadership
    <thought>
        <id>1</id>
        <category>Love</category>
        <what>sometext</what>            
        <who>sometext</who>
    </thought>
    <thought>
        <id>2</id>
        <category>Love</category>
        <what>sometext</what>            
        <who>sometext</who>
    </thought>
    ... 100's of with category Love

    ... and so on up to about ten categories
</thoughts>

I am parsing this xml in java for a particular category and id in android. Here is my code:

String id= "100",category="Love";// for example
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(getResources().openRawResource(R.raw.thoughts));
doc.getDocumentElement().normalize();

// Log.d(LOG,"root: " + doc.getDocumentElement().getNodeName());
NodeList nList = doc.getElementsByTagName("thought");

for (int temp = 0; temp < nList.getLength(); temp++) {
    org.w3c.dom.Node nNode = nList.item(temp);
    Element eElement = (Element) nNode;
    if (nNode.getNodeType() == Node.ELEMENT_NODE && getTagValue("id", eElement).contains(id) && getTagValue("category", eElement).contains(category)) {
        Log.d("THOUGHTSERVICE", "getTagValue(\"what\", eElement):"+getTagValue("what", eElement));
        what = getTagValue("what", eElement);
        who = getTagValue("who", eElement);
    }
}

The problem is: this is kind of brute force. Can you suggest some other method? May be give an example.

Thanks in advance

harshit
  • 3,788
  • 3
  • 31
  • 54
  • 1
    Why not use JAXB. You may want to look at http://stackoverflow.com/questions/373833/best-xml-parser-for-java for more details – Chris Sep 15 '12 at 10:41
  • You should consider using a SAX parser instead as they are faster than DOM parsers. – Peter Lawrey Sep 15 '12 at 10:43

4 Answers4

1

Why not use JAXB. You may want to look at Best XML parser for Java for more details

Community
  • 1
  • 1
Chris
  • 5,584
  • 9
  • 40
  • 58
1

XPath based querying seems more apt to this situation as you know the nodes you are interested in.

Vikdor
  • 23,934
  • 10
  • 61
  • 84
  • Can you please link me to how to use it. I am completely blank on this (xml parsing). – harshit Sep 15 '12 at 10:46
  • This tutorial is a good one to start with: http://www.ibm.com/developerworks/library/x-javaxpathapi/index.html – Vikdor Sep 15 '12 at 10:47
  • what should i put as expression. I tried "/thought[id='100']//thought[category='Love']/what/text()" I cant figure out how to select multiple attributes (what and who) and give two criteria – harshit Sep 15 '12 at 12:38
0

Better use xmlPullParser , it reads sequentially and you can stop reading as soon as you find and read the tag you are interested in. It will decrease RAM usage, plus if the required tag is present in beginning of file, you don't need to read rest of the file.

You can also do some skipping, like if <id> not match, skip to next <id> tag.

S.D.
  • 29,290
  • 3
  • 79
  • 130
0

// Using the DOM Parser Procedure to parse the xml responds string

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder db = factory.newDocumentBuilder();
Document doc = db.parse(inStream);
NodeList nodeList = doc.getElementsByTagName("thoughts");
for (int index = 0; index < nodeList.getLength(); index++)
{
    Node node = nodeList.item(index);
    if (node.getNodeType() == Node.ELEMENT_NODE)
    {
        Element element = (Element) node;
        NodeList PAYMENT_DATENode = element.getElementsByTagName("id");
        for (int iIndex = 0; iIndex < PAYMENT_DATENode.getLength(); iIndex++) 
        {
            if (PAYMENT_DATENode.item(iIndex).getNodeType() == Node.ELEMENT_NODE) 
            {
                Element nameElement = (Element) PAYMENT_DATENode.item(iIndex);
                String s = (nameElement.getFirstChild().getNodeValue();
            }
        }
    }
}
Vikdor
  • 23,934
  • 10
  • 61
  • 84
Rajesh Rajaram
  • 3,271
  • 4
  • 29
  • 48