Inefficient XML parsing in Java

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

Answer:1

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

Answer:2

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

Answer:3

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.

Answer:4

I have a string that is being set to a default when the input is outside of the specified values but it keeps returning null instead of the default value. what code am I missing to have the correct ...

I'm developing a download-everything app for Android devices, and just a few URLs are giving me problems by throwing FileNotFoundException for some reason. I print out the URL and copy it to my ...

I need help. If userID is right. JSONObject is { "info": { "name": "Guest", "age": "18" } } If userID is wrong. JSONObject is { "info":"wrong" } My code: if (!(resultObject....

I have an assignment to be done in Java, and I can't figure this out for the life of me. I'm supposed to use Graphics2D and Java.AWT. to mirror an image across both the x-axis and y-axis. Current ...