getElementsByTagName() returns null
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Sachin_Patil
Posted On:   Sunday, February 3, 2002 06:42 PM

Heres my my small xml String and a chunk of code, where I want the element query as a whole but it returns null dont know why? Could anyone help? Interestingly, it returns proper child elements. String xml=" <?xml version = '1.0'?> j8lrzp5sg1 1615 "; System.out.println(document.getElementsByTagName("QUERY").item(0)); This statement returns a null as [QUERY] & System.out.println(document.getElementsByTagName("QUERY_ID").item(0).getFirstChild().getNodeValue()); or System.out.println(document.getElementsByTagName("RESULT_ID").item(0).getFirstChild().getNodeValue()); retu   More>>

Heres my my small xml String and a chunk of code, where I want the element query as a whole but it returns null dont know why? Could anyone help? Interestingly, it returns proper child elements.

String xml=" <?xml version = '1.0'?>



j8lrzp5sg1
1615

";

System.out.println(document.getElementsByTagName("QUERY").item(0));

This statement returns a null as [QUERY]
&


System.out.println(document.getElementsByTagName("QUERY_ID").item(0).getFirstChild().getNodeValue());

or

System.out.println(document.getElementsByTagName("RESULT_ID").item(0).getFirstChild().getNodeValue());

return the correct value of resp elements.

   <<Less

Re: getElementsByTagName() returns null

Posted By:   Noel_Prevost  
Posted On:   Friday, May 3, 2002 12:36 PM

Whenever you are trying to print the node out it is printing null becuase you do not have anything to print. itself does not contain a value. However, it does contain all of its child nodes which do contain values. Thus you need to parse the XML. contains j8lrzp5sg1 and contains 1615.






j8lrzp5sg1

1615





Try this


NodeList nodelist = document.getElementsByTagName("QUERY");

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

Node node = nodelist.item(i);

NodeList childList = node.getChildNodes();

//now you have and in childList



Node queryId = childList.item(0);

Node resultId = childList.item(0);


System.out.println("QueryId-->" + queryID.getFirstChild().getNodeValue());


System.out.println("ResultId-->" + resultId.getFirstChild().getNodeValue();

}//end for loop





If you are wanting to return the node based on your cnt attribute, you should use the
getElementById() where you put the value you are
wanting to look for such as getElementById("1").
The above
for loop will loop through all of the nodes.



also, on your next post, please enclose your message in the < pre > < /pre > html tags. It will make it a lot
easier to read.
also use the < br > to
break a line so that it does not scroll so long.

About | Sitemap | Contact