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.062 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.003 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.033 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.035 seconds, using GeSHi 1.0.8.4


hope it helps

Bye
¯`·.¸¸.><((((º>¯`·.¸¸. ><((((º>
User avatar
qlimax
Master Developer
Master Developer
 
Posts: 271
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.036 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

Re: Parse XML with DOM - getNodeValue() always null

Postby theSun » Wed Aug 25, 2010 9:53 pm

No need to cast to element or anything like that, just simply call ".getFirstChild().getNodeValue()", here's a common example. Why is ".getFirstChild()" required when its really the text of the current node I want? Not sure but it works.
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. NoneList nodes = root.getElementsByTagName("node");
  2.  
  3. for( int i = 0; i < nodes.getLength(); i++){
  4.     String innerText = nodes[i].getFirstChild().getNodeValue();
  5. }
  6.  
  7.  
Parsed in 0.037 seconds, using GeSHi 1.0.8.4
theSun
Once Poster
Once Poster
 
Posts: 1
Joined: Wed Aug 25, 2010 9:44 pm

Re: Parse XML with DOM - getNodeValue() always null

Postby helios_47 » Wed Apr 27, 2011 4:11 pm

hey theSun,

I am also having a similar problem. in the post u mention root.getElementsByTagName ... what is root and what is it defined as?

here is a snippet of my code

Im using async task to grab the string value from the xml file and passing that as a result String variable

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
readStream = new ByteArrayInputStream(result.getBytes());
Document doc = db.parse(readStream);
doc.getDocumentElement().normalize();

NodeList nodelist = doc.getElementsByTagName("result");


id = new TextView[nodelist.getLength()]; //Textviews defined in my xml file
name = new TextView[nodelist.getLength()];
score = new TextView[nodelist.getLength()];

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

Node node = nodelist.item(i);

id = new TextView[nodelist.getLength()];
id[i] = (TextView) findViewById(R.id.textID);

name = new TextView[nodelist.getLength()];
name[i] = (TextView) findViewById(R.id.textName);

score = new TextView[nodelist.getLength()];
score[i] = (TextView) findViewById(R.id.textScore);

Element fstElmnt = (Element) node;

Element fstElmt = (Element)nodelist.item(0);

NodeList idList = fstElmt .getElementsByTagName("id");
Element idElement = (Element) idList.item(0);
idList = idElement.getChildNodes();
id[i].setText(idElement.getTextContent());

I have tried debugging it and it says XML parsing error:null pointer exception. tried looking elsewhere on the net but no luck..

any help would be great
helios_47
Freshman
Freshman
 
Posts: 2
Joined: Fri Apr 15, 2011 12:06 pm

Re: Parse XML with DOM - getNodeValue() always null

Postby imbroken » Wed Apr 27, 2011 6:01 pm

Have a look at the table below "Class Overview", there you can see which node types gives you values or null
http://developer.android.com/reference/ ... /Node.html
imbroken
Senior Developer
Senior Developer
 
Posts: 131
Joined: Thu Jan 06, 2011 6:57 pm
Location: Germnay

Re: Parse XML with DOM - getNodeValue() always null

Postby royrjs » Thu Sep 15, 2011 9:11 am

theSun is right: it's nonsense, but it works, hehe!!

Thank you!
royrjs
Once Poster
Once Poster
 
Posts: 1
Joined: Thu Sep 15, 2011 9:08 am

Re: Parse XML with DOM - getNodeValue() always null

Postby jouyang3 » Tue Jul 24, 2012 9:59 am

We need firstchild because the existence of TextNode. There isn't really any values from elements.
jouyang3
Once Poster
Once Poster
 
Posts: 1
Joined: Tue Jul 24, 2012 9:56 am

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 15 guests