1

I need some advice on how to parse XML with Java where there are multiple nodes that have the same tag. For example, if I have an XML file that looks like this:

<?xml version="1.0"?>
<TrackResponse>
  <TrackInfo ID="EJ958083578US">
    <TrackSummary>Your item was delivered at 8:10 am on June 1 in Wilmington DE 19801.</TrackSummary>
    <TrackDetail>May 30 11:07 am NOTICE LEFT WILMINGTON DE 19801.</TrackDetail>
    <TrackDetail>May 30 10:08 am ARRIVAL AT UNIT WILMINGTON DE 19850.</TrackDetail>
    <TrackDetail>May 29 9:55 am ACCEPT OR PICKUP EDGEWATER NJ 07020.</TrackDetail>
  </TrackInfo>
</TrackResponse> 

I am able to get the "TrackSummary" but I do not know how to handle the "TrackDetail", since there is more than 1. There could be more than the 3 on that sample XML so I need a way to handle that.

So far I have this code:

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
InputSource is = new InputSource(new StringReader(xmlResponse));
Document dom = builder.parse(is);

//Get the ROOT: "TrackResponse"
Element docEle = dom.getDocumentElement();

//Get the CHILD: "TrackInfo"
NodeList nl = docEle.getElementsByTagName("TrackInfo");

String summary = "";

//Make sure we found the child node okay
if (nl != null && nl.getLength() > 0) 
{
    //In the event that there is more then one node, loop
    for (int i = 0 ; i < nl.getLength(); i++) 
    {
        summary = getTextValue(docEle,"TrackSummary");
        Log.d("SUMMARY", summary);
    }

    return summary;
}

How would I handle the whole 'multiple TrackDetail nodes' ordeal? I'm new to XML parsing so I am a bit unfamiliar on how to tackle things like this.

2
  • You can create a java arraylist to manage the TruckDetail records ... add to that for each one you recognize. Commented Mar 7, 2014 at 6:05
  • Yes, this post needs an answer. Commented May 22, 2015 at 7:36

3 Answers 3

1

You can try like this :

public Map getValue(Element element, String str) {
    NodeList n = element.getElementsByTagName(str);
    for (int i = 0; i < n.getLength(); i++) {
        System.out.println(getElementValue(n.item(i)));
    }
    return list/MapHere;
}
Sign up to request clarification or add additional context in comments.

Comments

0

If you are free to change your implementation then i would suggest you to use implementation given here.

you can collect the trackdetail in string array and when you are in XmlPullParser.END_TAG check for trackinfo tag end and then stop

Comments

0

You can refer below code for that.

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(f);
Element root = doc.getDocumentElement();
NodeList nodeList = doc.getElementsByTagName("TrackInfo");
for (int i = 0; i < nodeList.getLength(); i++) {
  Node node = nodeList.item(i); // this is node under track info
  // do your stuff
}

for more information you can go through below link.

How to parse same name tag in xml using dom parser java?

It may help.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.