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.

ErstwhileIII
  • 4,829
  • 2
  • 23
  • 37
KyleCrowley
  • 900
  • 6
  • 14
  • 31

3 Answers3

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;
}
Shahinoor Shahin
  • 685
  • 3
  • 14
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

Community
  • 1
  • 1
Vikas Rathod
  • 374
  • 2
  • 14
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.

Community
  • 1
  • 1
Solution
  • 604
  • 4
  • 10