...

View Full Version : problem in getting node value



amitye
12-02-2009, 10:50 AM
can someone please help me to find out wahy I cant access the text value within the "author> tag using the following code? I get that "var y" is null.

Thanks!



Content-type: text/html\n\n
<html>
<body>

<book>
<author>Moris</author>
</book>

<script type="text/javascript">
var x =document.getElementsByTagName("author")[0]
var y = x.childNodes[0];

document.write(y.value)
</script>


</body>
</html>

Philip M
12-02-2009, 11:30 AM
<book> and <author> are not HTML tags.


<p>Morris</p>
<p>Smith</p>
<p>Robinson</p>

<script type="text/javascript">
var allParas = document.getElementsByTagName("p");
var num = allParas.length;
alert("There are " + num + " <p> elements in this document");
var y = allParas[2].innerHTML
alert (y)
</script>

All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.

amitye
12-02-2009, 11:45 AM
so why is it working here?
http://www.w3schools.com/Dom/prop_element_tagname.asp


Because <book> and <author> are not valid HTML tags.


<p>Morris</p>
<p>Smith</p>
<p>Robinson</p>

<script type="text/javascript">
var allParas = document.getElementsByTagName("p");
var num = allParas.length;
alert("There are " + num + " <p> elements in this document");
var y = allParas[2].innerHTML
alert (y)
</script>

All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.

Kor
12-02-2009, 12:02 PM
so why is it working here?
http://www.w3schools.com/Dom/prop_element_tagname.asp
in that example the code writes the tagName attribute, not the innerHTML. You must sense the difference between XML DOM and HTML(XHTML) DOM.

amitye
12-02-2009, 01:04 PM
ok. I'm starting to get the difference.
now lets say that I want to manipulate some XML DOM structure using javascript.

the origin of this XML is a file which conatin configuration in XML format.
I dont want to reffer the user directly to open the file, so I want to add it to the html page as a hidden attribute/text.

what is the best way to do it? and How do I access it after that?


in that example the code writes the tagName attribute, not the innerHTML. You must sense the difference between XML DOM and HTML(XHTML) DOM.

Kor
12-02-2009, 01:57 PM
AJAX
http://en.wikipedia.org/wiki/Ajax_(programming)
http://www.w3schools.com/Ajax/Default.Asp

amitye
12-06-2009, 08:19 PM
thanks, kor.
I did my study and now I start using ajax . however I have some problem which I was unable to solve.

The ajax return the xml in the "xmlhttp.responseXML.documentElement"
where I can parse it and everything is ok.

The problem start when I want to cop this XML DOM to another variable.
so I defined a global var "new_dom" and in the "onResponse" (which is being called when the ajax was served) I copy the xml dom from the responseXML to my variable.

now, when I want to access this variable from other function it is allways null!

what am i doing wrong??




var new_dom;

function loadXMLDoc(url)
{
xmlhttp=null;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE5, IE6
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}

xmlhttp.onreadystatechange=onResponse;
xmlhttp.open("GET",url,true);
xmlhttp.send(null);


}

function onResponse()
{

if(xmlhttp.readyState!=4) return;
if(xmlhttp.status!=200)
{
alert("Problem retrieving XML data");
return;
}


new_dom=xmlhttp.responseXML.documentElement.cloneNode(true)

}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum