Parse XML with DOM - getNodeValue() always null

Put your problem here if it does not fit any of the other categories.

Parse XML with DOM - getNodeValue() always null

Postby bstubbs » Wed Oct 08, 2008 7:37 pm

The problem is pretty simple: getNodeValue() is awlays coming up null. I can get the correct names in a list with
getNodeName(), but the value is always null. In other uses, I have done getTextContent(), but that doesn't seem to be an option. Has anyone else run into this? Here is my code just show an example.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2.  public void getSearch(String strURL) {
  3.  
  4.                 // Connect to WH API - SEARCH FUNCTION
  5.  
  6.         URL url;
  7.  
  8.         URLConnection urlConn = null;
  9.  
  10.         mainList = new ArrayList<Listy>();
  11.  
  12.          
  13.  
  14.         try {
  15.  
  16.         url = new URL(strURL);
  17.  
  18.         urlConn = url.openConnection();
  19.  
  20.         } catch (IOException ioe) {
  21.  
  22.                 contentdiv.setText("Could not Connect: "+ioe.getMessage());
  23.  
  24.         }
  25.  
  26.  
  27.  
  28.         Document doc = null;
  29.  
  30.         try {
  31.  
  32.            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
  33.  
  34.            DocumentBuilder db = dbf.newDocumentBuilder();
  35.  
  36.            doc = db.parse(urlConn.getInputStream());
  37.  
  38.            String sid="", spage="", sname="", syear="", sim="";
  39.  
  40.            Node thisNode, currentNode; NodeList nchild, nodeList;
  41.  
  42.            
  43.  
  44.            nodeList = doc.getElementsByTagName("item");
  45.  
  46.            int length = nodeList.getLength();
  47.  
  48.            for (int i = 0; i < length; i++)
  49.  
  50.              {
  51.  
  52.                    currentNode = nodeList.item(i);
  53.  
  54.                    nchild = currentNode.getChildNodes();
  55.  
  56.                    int clength = nchild.getLength();
  57.  
  58.                for (int j = 0; j < clength; j++)
  59.  
  60.                 {
  61.  
  62.                    thisNode = nchild.item(j);
  63.  
  64.                    String value = thisNode.getNodeValue();  
  65.  
  66.                    if("id".equals(thisNode.getNodeName())){sid = value;}
  67.  
  68.                    if("page".equals(thisNode.getNodeName())){spage = value;}
  69.  
  70.                    if("name".equals(thisNode.getNodeName())){sname = value;}
  71.  
  72.                    if("year".equals(thisNode.getNodeName())){syear = value;}
  73.  
  74.                    if("im".equals(thisNode.getNodeName())){sim = value;}
  75.  
  76.                 }  
  77.  
  78.                mainList.add(new Listy(sid,sname,spage,syear,sim));    
  79.  
  80.            }  
  81.  
  82.            mFavList.setAdapter(new ArrayAdapter<Listy>(this,android.R.layout.simple_list_item_1, mainList));                       
  83.  
  84.         }
  85.  
  86.           catch (ParserConfigurationException pce){contentdiv.setText("Could not Parse XML: "+pce.getMessage());}
  87.  
  88.                   catch (SAXException se) {contentdiv.setText("Could not Parse XML: "+se.getMessage());}
  89.  
  90.                   catch (IOException ioe) {contentdiv.setText("Invalid XML: "+ioe.getMessage());}
  91.  
  92.       }
  93.  
  94.  
Parsed in 0.180 seconds, using GeSHi 1.0.8.4


The XML is encoded in UTF-8, could that be causing the problem?

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1.  
  2.  <?xml version='1.0' encoding='UTF-8'?>
  3.  
  4.     <searchy>
  5.  
  6.          <item>
  7.  
  8.               <id>1</id>
  9.  
  10.               <page>person</page>
  11.  
  12.               <name>Guy</name>
  13.  
  14.               <year>1980</year>
  15.  
  16.               <im>None</im>
  17.  
  18.          </item>
  19.  
  20.          <item>
  21.  
  22.               <id>2</id>
  23.  
  24.               <page>person</page>
  25.  
  26.               <name>Other Guy</name>
  27.  
  28.               <year>1985</year>
  29.  
  30.               <im>None</im>
  31.  
  32.          </item>
  33.  
  34.    </searchy>
  35.  
  36.  
Parsed in 0.014 seconds, using GeSHi 1.0.8.4


If I change a line in the code to have value = thisNode.getNodeName();, it displays the Node Names correctly. But the Node value, is always null...

Any thoughts (other than just using SAX). Thanks in advance!
bstubbs
Freshman
Freshman
 
Posts: 3
Joined: Wed Oct 08, 2008 7:23 pm

Top

Postby ExxKA » Sat Nov 07, 2009 1:52 pm

Hey.

I have the exact same problem. I am scared to find that you haven't got an answer yet (in a year!!). Did you ever solve this issue yourself?

I have seen people on the official android group asking this question aswell. But it doesn't seem to be solved.
http://www.mail-archive.com/android-developers@googlegroups.com/msg19336.html

My code is like this,
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. NodeList nodes = xmlDoc.getElementsByTagName(TRIPID);
  2.                                 Node tripId = nodes.item(0);
  3.                                 Node tripIdContent = tripId.getFirstChild();
  4.                                 if(tripIdContent.getNodeType() == Node.TEXT_NODE ){
  5.                                         String value = tripId.getNodeValue();
  6.                                         String value2 = tripId.toString();
  7.                                         return Long.parseLong(value);          
  8.                                 }
  9.  
Parsed in 0.139 seconds, using GeSHi 1.0.8.4


So as you can see, I actually check wether it's a TEXT_NODE or not.. Just to be extra sure. In the debugger, I can see that the Node object tripIdContent has a string buffer which does contain the right String I am trying to fetch. But no luck.
value == null, and value2 == org.apache.harmoney...blablabla ElementID.


Has anyone solved this issue, or are you using some other parser which actually works? I am not afraid of external jar's for as long as they play well with Android.Google Groups PostWeb Page Name
ExxKA
Developer
Developer
 
Posts: 29
Joined: Tue Nov 03, 2009 7:16 pm
Location: Denmark

Postby qlimax » Sat Nov 07, 2009 6:53 pm

Hi, that seem to work for me...
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.   NodeList nodeList=d.getElementsByTagName("name");
  2.  
  3.                int len=nodeList.getLength();
  4.  
  5.                for(int i=0;i<len;i++){
  6.  
  7.                    Node node=nodeList.item(i).getFirstChild();
  8.  
  9.                    Element element=(Element)nodeList.item(i);
  10.  
  11.                    
  12.  
  13.                    String name=element.getNodeName();
  14.  
  15.                    String value=node.getNodeValue();
  16.  
  17.                    
  18.  
  19.                    Log.v("",name+":"+value);
  20.  
  21.  
  22.  
  23.                    }
Parsed in 0.152 seconds, using GeSHi 1.0.8.4


hope it helps

Bye
¯`·.¸¸.><((((º>¯`·.¸¸. ><((((º>
User avatar
qlimax
Master Developer
Master Developer
 
Posts: 270
Joined: Mon Aug 31, 2009 10:54 am
Location: Swiss

Postby ExxKA » Sun Nov 08, 2009 4:55 pm

Thanks champ. I'll try it out
ExxKA
Developer
Developer
 
Posts: 29
Joined: Tue Nov 03, 2009 7:16 pm
Location: Denmark

Postby ExxKA » Sun Nov 08, 2009 5:05 pm

It seems I had bugged myself out.

My problem was that I was trying to get the value of the wrong node(not the child, but the parent)

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. NodeList nodes = xmlDoc.getElementsByTagName(TRIPID);
  3.  
  4.                                 Node tripId = nodes.item(0);
  5.  
  6.                                 Node tripIdContent = tripId.getFirstChild();
  7.  
  8.                                 if(tripIdContent.getNodeType() == Node.TEXT_NODE ){
  9.  
  10.                                         String value = tripId.getNodeValue(); //SHOULD BE tripIdContent.getNodeValue();
  11.  
  12.                                         String value2 = tripId.toString();
  13.  
  14.                                         return Long.parseLong(value);          
  15.  
  16.                                 }
  17.  
  18.  
Parsed in 0.153 seconds, using GeSHi 1.0.8.4


Thanks for the help.
ExxKA
Developer
Developer
 
Posts: 29
Joined: Tue Nov 03, 2009 7:16 pm
Location: Denmark

Postby cupid062985 » Thu May 13, 2010 6:55 am

same here, i also experience this problem... any other solution? none of the solutions are working...
cupid062985
Junior Developer
Junior Developer
 
Posts: 19
Joined: Wed Jan 27, 2010 11:36 am

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: saravanan.palpandi and 3 guests