0

I've an Android app that gets an xml response from a webservice call. I am trying to parse the xml using DOM parser. I'm having trouble getting the values of the StartDate tags as well as the other tags in the call tag. Any ideas where i'm going wrong. When i log the value out from the startDate, it's empty. Thanks.

public class DomParser {

    private static final String TAG = DomParser.class.getSimpleName();
    DocumentBuilderFactory builderFactory;
    DocumentBuilder builder;
     Document document;

    public DomParser() {
        super();
        Log.e(TAG, "inside domparser constructor");
        builderFactory = DocumentBuilderFactory.newInstance();
        document = null;
        try {
            builder = builderFactory.newDocumentBuilder();
            Log.e(TAG, "built the dom factory");
        } catch (ParserConfigurationException e) {
            e.printStackTrace();  
        }
    }// end of constructor


    public void parseXmlString(String str){

        try {
            String s = str.replace("&lt;", "<");
            String t = s.replace("&gt;", ">");
            document = builder.parse( new InputSource(new StringReader(t))); 
            Log.e(TAG, "document = " + document);
            Log.e(TAG, "str = " + t);
        } catch (SAXException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        Log.e(TAG, "about to parse the rootElement");
        Element rootElement = document.getDocumentElement();
        NodeList nodes = rootElement.getChildNodes();

        Node rota = nodes.item(0);
        NodeList callList = rota.getChildNodes();

        for(int i = 0; i < callList.getLength(); i++){

            Node call = callList.item(i);
            NodeList callChildrenList = call.getChildNodes();

            for(int j = 0; j < callChildrenList.getLength(); j++){
                Node callChild = callChildrenList.item(j);
                if(callChild instanceof Element){
                    //a child element to process
                    Element child = (Element) callChild;
                    String startDate = child.getAttribute("StartDate"); 
                    Log.e(TAG, "element StartDate = " + startDate);
                  }
            }

        }



    }//end of parseXmlString

}

.

  <?xml version="1.0" encoding="utf-8"?>
    10-05 15:49:17.680: E/DomParser(31495): <string xmlns="http://tempuri.org/"><Rota><Call><StartDate>2012-10-05T07:00:00+01:00</StartDate><EndDate>2012-10-05T07:25:00+01:00</EndDate><Duration>25</Duration><CallStatusID>1</CallStatusID><CallStatusName>Completed</CallStatusName><ClientSurname>Ebbs</ClientSurname><ClientForename>Vera</ClientForename><NeedName>Assist With Dressing, Laundry, Leave A Drink, Leave A Sandwich For Later, Make A Hot Drink, Prepare Breakfast, Shower, TIME BAND - Early Breakfast</NeedName><CarerAwayReason /><CallID>47418572-73a1-4dab-bc24-739c122b2b25</CallID><ActualTimeIn>07:30</ActualTimeIn><ActualTimeOut>07:49</ActualTimeOut></Call><Call><StartDate>2012-10-05T07:30:00+01:00</StartDate><EndDate>2012-10-05T08:00:00+01:00</EndDate><Duration>30</Duration><CallStatusID>1</CallStatusID><CallStatusName>Completed</CallStatusName><ClientSurname>Brown</ClientSurname><ClientForename>Margaret</ClientForename><NeedName>Careline, Diabetic Type 2, Empty Commode, KEY SAFE, Leave A Drink, Make A Hot Drink, Meds - Level II Administer, Prepare Snack, TIME BAND - Early Bed</NeedName><CarerAwayReason /><CallID>14f4f796-c7a6-4942-9d91-e9e7a40394f5</CallID><ActualTimeIn>08:00</ActualTimeIn><ActualTimeOut>08:21</ActualTimeOut></Call><Call><StartDate>2012-10-05T08:00:00+01:00</StartDate><EndDate>2012-10-05T08:40:00+01:00</EndDate><Duration>40</Duration><CallStatusID>1</CallStatusID><CallStatusName>Completed</CallStatusName><ClientSurname>Weaver</ClientSurname><ClientForename>Margaret</ClientForename><NeedName>KEY SAFE, Morning Call - Get Up And Breakfast, Personal Care</NeedName><CarerAwayReason /><CallID>2a7e6c70-ee2c-4657-a313-a18b87a766ed</CallID><ActualTimeIn>08:35</ActualTimeIn><ActualTimeOut>09:13</ActualTimeOut></Call><Call><StartDate>2012-10-05T09:00:00+01:00</StartDate><EndDate>2012-10-05T09:20:00+01:00</EndDate><Duration>20</Duration><CallStatusID>3</CallStatusID><CallStatusName>NCR</CallStatusName><ClientSurname>Howarth</ClientSurname><ClientForename>Edith</ClientForename><NeedName>Change Incontinence Pads, KEY SAFE, Make Bed / Change As Neccessary, Meds - Level II Administer, Personal Care, Prepare Breakfast, TIME BAND - Late Breakfast</NeedName><CarerAwayReason /><CallID>383c9be6-4cbe-459a-92ed-5dc309a0bffb</CallID></Call><Call><StartDate>2012-10-05T09:00:00+01:00</StartDate><EndDate>2012-10-05T09:30:00+01:00</EndDate><Duration>30</Duration><CallStatusID>1</CallStatusID><CallStatusName>Completed</CallStatusName><ClientSurname>Kastik</ClientSurname><ClientForename>Lydia</ClientForename><NeedName>Keys Needed, Leave A Drink, Leave A Sandwich For Later, Make A Hot Drink, Make Bed / Change As Neccessary, Medication Prompt, Meds - Level II Administer, Personal Care, TIME BAND - Early Breakfast</NeedName><CarerAwayReason /><CallID>f4cc1f96-ef65-4231-9e03-0b8263b04d27</CallID><ActualTimeIn>09:28</ActualTimeIn><ActualTimeOut>09:57</ActualTimeOut></Call><Call><StartDate>2012-10-05T10:30:00+01:00</StartDate><EndDate>2012-10-05T11:00:00+01:00</EndDate><Duration>30</Duration><CallStatusID>1</CallStatusID><CallStatusName>Completed</CallStatusName><ClientSurname>Terett</ClientSurname><ClientForename>Mamie</ClientForename><NeedName>Assist With Dressing, Meds - Self Medicating, Morning Call, Personal Care, Strip Wash, TIME BAND - Late Breakfast</NeedName><CarerAwayReason /><CallID>03f52182-b9ff-4fa9-9b71-1a20b8bbeeaa</CallID><ActualTimeIn>10:10</ActualTimeIn><ActualTimeOut>10:57</ActualTimeOut></Call><Call><StartDate>2012-10-05T12:00:00+01:00</StartDate><EndDate>2012-10-05T12:15:00+01:00</EndDate><Duration>15</Duration><CallStatusID>1</CallStatusID><CallStatusName>Completed</CallStatusName><ClientSurname>Garland</ClientSurname><ClientForename>Mavis</ClientForename><NeedName>Empty Catheter Bag, Medication Prompt, Meds - Level II Administer, TIME BAND - Early Lunch, Toiletting / Commode</NeedName><CarerAwayReason /><CallID>802d8adf-3a80-40bd-be91-f29db7c1291e</CallID><ActualTimeIn>11:16</ActualTimeIn><ActualTimeOut>11:38</ActualTimeOut></Call><Call><StartDate>2012-10-05T12:30:00+01:00</StartD

[update]

Element rootElement = document.getDocumentElement();
        NodeList nodes = rootElement.getChildNodes();

        Node rota = nodes.item(0);
        NodeList callList = rota.getChildNodes();

        for(int i = 0; i < callList.getLength(); i++){

            Node call = callList.item(i);
            NodeList callChildrenList = call.getChildNodes();


            for(int j = 0; j < callChildrenList.getLength(); j++){
                Node callChild = callChildrenList.item(j);

                Log.e(TAG, "callChild name = " + callChild.getNodeName());


            }

2 Answers 2

2

Something like this based on your new example code

// HERE LOOP TO FIND THE NODE LIKE YOU HAVE DONE IN YOUR EXAMPLE
// WHERE YOU HAVE callChild.getNodeName() AND LOOK FOR THE ONE THAT IS "StartDate"
// ASSUMING startDateNode HERE IS THE NODE YOU WANT

    // GET THE CHILD NODES AND LOOK FOR THE TEXT NODE  
    NodeList nList = startDateNode.getChildNodes();
    for (int i=0; i < nList.getLength(); i++) {
       Node tmpNode = nList.item(i);
       if (tmpNode.getNodeType() == Node.TEXT_NODE) {
          String dateStr = ((CharacterData)tmpNode).getData();
           // NOW YOU HAVE THE CHARACTER DATA
          // DO SOMETHING WITH THE VALUE
       }
    }
Sign up to request clarification or add additional context in comments.

4 Comments

Hi, There is one rota tag. inside the rota tag there are serveral call tags. Inside each call tag there are various tags that i need to get the values out. Once i'm inside the call tag each of the subsequent values must be populated into an array. Eventually there is an array that is an array of arrays. eg in this case the parent array will hold 19 arrays. Each of those 19 arrays will hold the data within the call tag. So does your code extract all the startdates first?
I've updated my post and got a little closer. I can now log out all the tag names but how would i get their values? Thanks
I had added some code to that example in error, from something else, which i just corrected. It gets loopy, walking the DOM like this but that should help. You need to get the child nodes of the node you have now, and look for the one of type Node.TEXT_NODE and then get the data from that.
hi, i just changed one line in your code and it works fine thanks. String nodeData = callChild.getTextContent();
1

check hereto get the proper idea i have implemented in one of my code like this.

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.