0

I have an XML like this

<reg>
  <user>
       <Name> abc</Name>
       <Email> [email protected]</Email>
       <picture> sdCard/imges.145.jpg </Picture>
       <Date> 12/12/2012</Date>
  </user>
  <user>
       <Name> abc dfg</Name>
       <Email> [email protected]</Email>
       <picture> sdCard/imges.145.jpg </Picture>
       <Date> 23/12/2013</Date>
  </user>
</reg>

i want to get values of specific nodes and add them to ArrayList to show the in ListView I have tried this code but it only gives values for Last Record

Users setUser = new Users();
final ArrayList<Users> uData = new ArrayList<Users>();
Document xmlDocument = builder.parse(file);

                XPath xPath =  XPathFactory.newInstance().newXPath();


                String expression = "/reg/user/Name";
                System.out.println(expression);

                NodeList nodeList = (NodeList) xPath.compile(expression).evaluate(xmlDocument, XPathConstants.NODESET);
                for (int i = 0; i < nodeList.getLength(); i++) {
                    System.out.println(nodeList.item(i).getTextContent());
                   setUser.setName(nodeList.item(i).getTextContent());

               }
                String expression1 = "/reg/user/Email";
                System.out.println(expression1);

              NodeList nodeList1 = (NodeList) xPath.compile(expression1).evaluate(xmlDocument, XPathConstants.NODESET);
                for (int i = 0; i < nodeList1.getLength(); i++) {
                    System.out.println(nodeList1.item(i).getTextContent());
                   setUser.setEmail(nodeList1.item(i).getTextContent());
               }
                String expression2 = "/reg/user/Picture";
                System.out.println(expression2);

                NodeList nodeList2 = (NodeList) xPath.compile(expression2).evaluate(xmlDocument, XPathConstants.NODESET);
                for (int i = 0; i < nodeList2.getLength(); i++) {
                    System.out.println(nodeList2.item(i).getTextContent());
                   setUser.setimage(nodeList2.item(i).getTextContent());
               }
                String expression3 = "/reg/user/LastEdited";
                System.out.println(expression3);

                NodeList nodeList3 = (NodeList) xPath.compile(expression3).evaluate(xmlDocument, XPathConstants.NODESET);
                for (int i = 0; i < nodeList3.getLength(); i++) {
                    System.out.println(nodeList3.item(i).getTextContent());
                   setUser.setDate(nodeList3.item(i).getTextContent()); 


                }  uData.add(setUser);

How can i make this to show all records ?

8
  • how do u identify the specific node? Commented Mar 26, 2014 at 9:58
  • here in my code i am identifying the node by XPATH expression . . . Commented Mar 26, 2014 at 10:08
  • Your setUser object replaces when the for loop iterates.And your adding setUser to uData at end of forloop. So only the last object will be added to the array list.Thats why you are getting the last record only Commented Mar 26, 2014 at 10:15
  • i know this reason the question is that how can i make this happen in 1 (outer loop) so i can get values n all 4 nodes and add it to ArrayList, the way i am doing it by Xpath i have to run a saperate loop for each nod to get its values and put it in NodeList... Commented Mar 26, 2014 at 10:20
  • 1
    oh.Instead of adding to the object u can keep temperory list for name,image,date,email.Now you have all elements in seperate array. Since all list has same no of elements, You can start another loop. For(int i=0;i<name.size()>;i++){ uData= new UData(); uData.setImage=name.get(i).getname(); uData.setDate=date(i).getDate; etc etc .Now the object is ready and add to uData.add(uData); }. But this will not be the best solution, this would cause serious performance issue. Try simplexml if u can, its pretty easy to handle Commented Mar 26, 2014 at 10:55

1 Answer 1

1

The reason why your code only gives values for Last Record already explained by @playmaker420 in comment. To fix it, you can try to loop through <user> elements instead of looping through each <name>, <email>, <picture>, and <date> separately :

XPath xPath =  XPathFactory.newInstance().newXPath();
String expression = "/reg/user";

NodeList nodeList = (NodeList) xPath.compile(expression).evaluate(xmlDocument, XPathConstants.NODESET);
for (int i = 0; i < nodeList.getLength(); i++) {
    Users setUser = new Users();
    Element el = (Element) nodeList.item(i);

    String name = el.getElementsByTagName("Name").item(0).getTextContent();
    setUser.setName(name);

    String email = el.getElementsByTagName("Email").item(0).getTextContent();
    setUser.setEmail(email);

    String picture = el.getElementsByTagName("picture").item(0).getTextContent();
    setUser.setimage(picture);

    String date = el.getElementsByTagName("Date").item(0).getTextContent();
    setUser.setDate(date);

    uData.add(setUser);
}
Sign up to request clarification or add additional context in comments.

2 Comments

its giving last records for 2 times . .
hmm.. I suspect you still have another uData.add(setUser); outside for loop? That will cause the last record added to list for 2nd time.

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.