Hi I am learning xml through w3schools website and i have a doubt in XML DOM(Document object model) Tutorial.
Under DOM load Function topic.
It is mentioned that
This function can be stored in the header of an HTML page and called from a script in the page.
=====================================
Part 1
========================
Part 2
Basically in the Part 2 were declaring a static text which will hold textual information i.e a piece of concatenated code which has parent and child nodes.
Then the function loadXMLString(text) is bing called to which the text is being passed not in the function in part 1
it will take the text and create a new XMLDoc
xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async="false";
xmlDOc.loadXML(text);
Then this will load the text to the Parser which will load it in memory and return what?????
DOes it return text i mean it is a function so it should return something does it return text
if it returns text it is being passed to
xmlDoc=loadXMLString(text);
xmlDoc is xmlDoc of string type?, probably not.
Are we calling the function xmlDocString so that the parser will be called, which will convert the xml document into a javascript
and is the function returning javascript to xmlDoc which is calling the function
/*
If were using the xmlDoc to hold/act as a buffer for the text which is static i.e we are supplying it in the file itself in declaration
then
what is the purpose of the line.
what is the purpouse of loadxmlstring.js
what does it load additionally?
isnt text which is static here supposed to be in the file called loadxmlstring.js
I am confused as the text to be loaded into the parser is being statistically supplied in the code itself as
text = "<bookstore>"+"<books>"...."<books>"+"<bookstore>".
and
then also mentioning to call the function loadxmlstring.js which would call the function loadXMLString(txt) i mean which will get loaded into the parser the dynamic text supplied in the 2nd part or the text which is being held in the function in first piece of code.
loadxmlstring.js which then would be unwrapped to extract the elements/nodes in the xml tree and document.write the node contents.
*/
Doubt 2:
The above code shows that the author node consists of a child node called pages.
Can we print both of them out by saying
instead of document.write(xmlDoc.getElementsByTagName("author"[0].childNodes[0].nodevalue);
document.write(xmlDoc.getElementsByTagName("pages"[0].childNodes[0].nodevalue);
as
document.write(xmlDoc.getElementsByTagName("author")[0].childNodes[1].nodeValue);
I mean here specifying from the 0 position of author node to 0 position of child node print out.
But can we say from the 0 position of author node, i.e parent node to the end of child node which is pages node instead ofwriting 2 lines of code.
Thank you.
Under DOM load Function topic.
It is mentioned that
This function can be stored in the header of an HTML page and called from a script in the page.
=====================================
Part 1
Code:
function loadXMLString(txt)
{
try //Internet Explorer
{
xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async="false";
xmlDoc.loadXML(txt);
return(xmlDoc);
}
catch(e)
{
try //Firefox, Mozilla, Opera, etc.
{
parser=new DOMParser();
xmlDoc=parser.parseFromString(txt,"text/xml");
return(xmlDoc);
}
catch(e) {alert(e.message)}
}
return(null);
}
Part 2
Code:
<html>
<head>
<script type="text/javascript" src="loadxmlstring.js"></script>
</head>
<body>
<script type="text/javascript">
text="<bookstore>"
text=text+"<book>";
text=text+"<title>Everyday Italian</title>";
text=text+"<author>Giada De Laurentiis";
text=text+"<pages>100</pages> </author>";
text=text+"<year>2005</year>";
text=text+"</book> </bookstore>";
text=text+"";
xmlDoc=loadXMLString(text);
document.write(xmlDoc.getElementsByTagName("title")[0].childNodes[0].nodeValue);
document.write("<br />");
document.write(xmlDoc.getElementsByTagName("author")[0].childNodes[0].nodeValue);
document.write("<br />");
document.write(xmlDoc.getElementsByTagName("pages")
[0].childNodes[0].nodeValue);
document.write("<br />");
document.write(xmlDoc.getElementsByTagName("year")[0].childNodes[0].nodeValue);
</script>
</body>
</html>
Basically in the Part 2 were declaring a static text which will hold textual information i.e a piece of concatenated code which has parent and child nodes.
Then the function loadXMLString(text) is bing called to which the text is being passed not in the function in part 1
it will take the text and create a new XMLDoc
xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async="false";
xmlDOc.loadXML(text);
Then this will load the text to the Parser which will load it in memory and return what?????
DOes it return text i mean it is a function so it should return something does it return text
if it returns text it is being passed to
xmlDoc=loadXMLString(text);
xmlDoc is xmlDoc of string type?, probably not.
Are we calling the function xmlDocString so that the parser will be called, which will convert the xml document into a javascript
and is the function returning javascript to xmlDoc which is calling the function
/*
If were using the xmlDoc to hold/act as a buffer for the text which is static i.e we are supplying it in the file itself in declaration
then
what is the purpose of the line.
Code:
<script type = "text/javascript" src="loadxmlstring.js">
</script>
what does it load additionally?
isnt text which is static here supposed to be in the file called loadxmlstring.js
I am confused as the text to be loaded into the parser is being statistically supplied in the code itself as
text = "<bookstore>"+"<books>"...."<books>"+"<bookstore>".
and
then also mentioning to call the function loadxmlstring.js which would call the function loadXMLString(txt) i mean which will get loaded into the parser the dynamic text supplied in the 2nd part or the text which is being held in the function in first piece of code.
loadxmlstring.js which then would be unwrapped to extract the elements/nodes in the xml tree and document.write the node contents.
*/
Doubt 2:
The above code shows that the author node consists of a child node called pages.
Can we print both of them out by saying
instead of document.write(xmlDoc.getElementsByTagName("author"[0].childNodes[0].nodevalue);
document.write(xmlDoc.getElementsByTagName("pages"[0].childNodes[0].nodevalue);
as
document.write(xmlDoc.getElementsByTagName("author")[0].childNodes[1].nodeValue);
I mean here specifying from the 0 position of author node to 0 position of child node print out.
But can we say from the 0 position of author node, i.e parent node to the end of child node which is pages node instead ofwriting 2 lines of code.
Thank you.