Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Kindly Help with a XML DOM doubt in xml 1

Status
Not open for further replies.

Zten

Programmer
Apr 11, 2009
4
GB
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

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 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.
 
[1] Doubt #1
There is quite a bit of confusion indeed that leads to confusion on the exposition of the doubt itself. Hence, it is hard to follow. In any case, these selections might clear things up.
[1.1] (restricted to the ActiveXObject part, hence, iexplore)
>Then this will load the text to the Parser which will load it in memory and return what?????
The tutorial material does not control the return. It returns something. What? It returns a boolean true (-1) and false (0). If the loading of loadXML() is successful, it returns true (-1); it it falis, it returns false (0). The tutorial does not control that part because they construct a well-formed xml serialized string, hence, there is no need to control that so as not to load up too much material to absorb for the novice.
[1.2]
>xmlDoc=loadXMLString(text);
>xmlDoc is xmlDoc of string type?, probably not.
>xmlDoc is xmlDoc of string type?, probably not.
No, xmlDoc being return is a DOMDocument object, absolutely not a string.
[1.3] Now, this is a tougher part. What happens if the loadXML() actually failed. The tutorial shown does not treat that. For ActiveXObject iexplore part, it is stored in the xmlDoc.parseError object. For native DOMParser ff etc, it is stored in the xmlDoc document itself which will then have a root named <parsererror>...</parsererror>. With the peak into them, you know whether you would go ahead to do getElementsByTagName() etc dom operation.
[1.4] I think it is sufficient for the doubt #1. Questions on the construction of html/script etc, I am not going to answer as you have to have learned html itself before doing xml tutorial like that.

[2] Doubt #2
>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.
Sure, it would be of the following form. The line:
[tt] document.write(xmlDoc.getElementsByTagName("pages")[0].childNodes[0].nodeValue);[/tt]
would have the same effect as this.
[tt] document.write(xmlDoc.getElementsByTagName("author")[0].childNodes[[red]1[/red]].[red]childNodes[0].[/red]nodeValue);[/tt]
 
Thanks tsuji for that valuable post,

However i have some more doubts if you would be kind enough to clear them.
==========================================================
1:
---
In part 2 text is a XML document,then
xmlDoc=loadXMLString(text);
will call the function loadXMLString(txt) in part 1.


The function takes in text
and returns xmlDoc (You replied that )which is DOM Document object.

As far as i know DTD is a collection of guidelines for a well formed xml document.

So when we say in the function in part 1
xmlDoc=new ActiveXObject(text)
i.e xmlDoc of ActiveXObject type which is taking a xml document i.e text.
then xmlDoc.async="false"; will prevent the DOM parser to execute the document before it is fully loaded

==========================================================
2:
---
xmlDoc.load(text);
OR
parser=new DOMParser();
xmlDoc=parser.parseFromString,text)
here the xml document is being converted into a javascript which is what the parser doess loads the xml document into memory and converts it into a javascript.js.

so now after converting the xml document into a javascript in the function it is returning xmlDoc as a javascript to the calling xmlDoc

which is what your saying that a DOM document object will be returned back i.e a javascript object i mean the dom basically converts the Xml document into a javascript.

Maybe i am wrong, kindly correct me if i am

Doubt 1:

In the 2nd part
====================================================
<html>
<head>
<script type=
"text/javascript" src="loadxmldoc.js"></script>
</head>
====================================================

Is this the declaration part where were specifying that the script is of either text or javascript and source file is called loadxmldoc.js

====================================
<body>
<script type="text/javascript">
xmlDoc=loadXMLDoc(text);
====================================

Here the actual body of the html resides which interacts with the declaration to check for additional information

In here were calling the function and passing text


Doubt 2:
========================================================
<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>";
text=text+"</author>";
text=text+"</book>";
text=text+"</bookstore>";

xmlDoc=loadXMLString(text);

document.write(xmlDoc.getElementsByTagName("title")[0].childNodes[0].nodeValue);
document.write("<br />");
document.write(xmlDoc.getElementsByTagName("author")[0].childNodes[1].childNodes[0].nodeValue);
document.write("<br />");
</script>
</body>
</html>
==========================================================
when we say xmlDoc.getElementsByTagName is xmlDoc here acting as javascript which maybe acts as a buffer/cursor holding the different nodes because what happens when we pass a xml document through a DOM(document object model) is it converts the xml document into a tree format with root nodes, parent nodes, child Nodes, siblings, etc.

so that the document is now not dependent upon the data present in it but it sort of acts as a skeleton with a format that validates againist the well formed xml document format.
Is this what is happening

Doubt 3:
And it does not print out the author and pages in this piece

document.write(xmlDoc.getElementByTagName("author")[0]
.childNodes[1].childNodes[0].nodevalue);


It should print
Giada di ...
100
...
But

It just prints the pages twice.
100
100

well it is being mentioned to write to the document the 0th position of author node to the 1st position of its child which is pages and from the 1st position of child i.e pages to 0 position of the child of pages.

Is my understanding until here right or if not kindly correct me, thank you for helping me learn.

 
[3] >1:
[3.1] >As far as i know DTD is a collection of guidelines for a well formed xml document.
What have it got to do with the tutorial there?
[3.2]
>xmlDoc=new ActiveXObject(text)
Read the line properly, not approximately. That is not what the tutorial is telling you.
>i.e xmlDoc of ActiveXObject type which is taking a xml document i.e text.
Disregard of the wrong reading above, xmlDoc is itself already an DOMDocument object. It is taking (with loadXML() method) a text string to fill up its content. The text itself is not an xml document.
>then xmlDoc.async="false"; will prevent the DOM parser to execute the document before it is fully loaded
"DOM parser to execute the document" does not have the right semantic. The parser loads the text string . The text string is not the (xml) document. The document is a technical term in the xml spec. The .async is to make the loading process synchronous (with double negative asynchroneity false) so that xmlDoc is fully loaded its content. This is again with very specific meaning and is flagged by its readystate property being 4. Until then, the control is returned and the line "return xmlDoc" can be executed with xmlDoc which is now in one piece and not in the way of doing thing.

[4] >2:
[4.1]
>xmlDoc.load(text);
>xmlDoc=parser.parseFromString,text)
You have to more exact. Read careful what it is written in the tutorial.
[4.2]
>i mean the dom basically converts the Xml document into a javascript.
This is not correct. xmlDoc is in a sense the xml document parsed into the memory according to dom (document object model). It is an instance of an implementation of xml dom. xmlDoc instantiated with the new ActiveXObject("Microsoft.XMLDOM") and with the native new DOMParser() are very different. They are different in many aspects. But they all implement some core functionality as specified in the xml dom. That is how they are in common. But there is nothing that can be taken for granted. The "standard" is subject to interpretation and implementation is subject to bugs etc etc.
[4.3]
><script type="text/javascript" src="loadxmldoc.js"></script>
>Is this the declaration part where were specifying that the script is of either text or javascript and source file is called loadxmldoc.js
It is not either text or javascript, text/javascript is read as a whole. It is what html4.01 adapted as MIME type for type attribute to appear in the script tag for javascript language.
[4.3]
>so that the document is now not dependent upon the data present in it but it sort of acts as a skeleton with a format that validates againist the well formed xml document format.
I wouldn't say not dependent upon the data present in it. It depends on the data an awful lot. "validate" is again very much out of place. It has a very technical meaning. But I wouldn't get carried away too far in words. The object not only represents the xml document, free of "irrelevant" info as far as the xml document is concerned, (tecnically it works on the infoset of it which is the fruit of the work done by what called parsing), it also has implements properties and methods ready to work on the document.
[4.4]
>document.write(xmlDoc.getElementByTagName("author")[0].childNodes[1].childNodes[0].nodevalue);
The line I posted meant to replace direct reference to "pages" tag. I see no reason one would expect to print what you seem to suggest. I suggest you may look at this line as well so that both pieces of info would appear:
[tt]document.write(xmlDoc.getElementByTagName("author")[0].text;[/tt]
but not in the format I hope for: maybe that's the reason why one uses two lines of script.

In any case, do not anchor what you discover with pseudo technical terms by pure imagination too soon and it has a very perverse effect that it would be hard to unlearn. To unlearn, that is the hard part: I see too often. I'm afraid I am done with this thread.
 
Kindly help me out, sorry to frustrate you i can understand that it might be hard for you a experienced person to deal with a novice, but if you can find it in your heart to help me learn it would be of great help.

xmlDoc is itself already an DOMDocument object. It is taking (with loadXML() method) a text string to fill up its content.

This part is unclear to me,
You say text is not an xml file/document but doesnt text has all the features of a xml file format, i am not saying it is well formed xml document as it does not have a corresponding DTD with it but looking at it text corresponds to a xml format but when we write

text="<bookStore>"
text=text+"<books>";
text=text+"<author>Beth</author>";
text=text+"</books> </bookStore>";

Doesnt text above represent a xml file, what i mean to say is a xml file is normally in the form above, maybe i am completely wrong, i am not trying to defend my statement, i am just trying to clear my confusion, flamed or no flamed you know my goal is to learn and whether you flame, i am sorry for your frustration but thankful for your time and cooperation.

You said that xmlDOM is already an DOM Document object do u say this because in the function loadXmlDoc(txt) in part 1its stated

xmlDoc=new ActiveXObject("Microsoft.XMLDOM");

Thank you for clearing the concept where

xmlDoc.async="false"; (with double negative asynchronicity false)so that xmlDoc is fully loaded its content.
=======
What does this mean "flagged by its ready state property being 4??"
=======

I understand the rest

"until then the control is returned and the line "return xmlDoc" can be executed with xmlDoc which is now in one piece(because it is fully loaded) and not in the way of doing thing."

===========

[4.2]
>i mean the dom basically converts the Xml document into a javascript.
This is not correct. xmlDoc is in a sense the xml document parsed into the memory according to dom (document object model). It is an instance of an implementation of xml dom. xmlDoc instantiated with the new ActiveXObject("Microsoft.XMLDOM")

==========
Do you mean to say that xmlDoc is an instance of an object of the xml dom, it is not alloted space in the memory (because it is just instantiation).

I mean xmlDoc=new ActiveXObject(text)
so here xmlDoc is an instantiation of a DOM document object which has a memory location and xmlDOc because its an instance of the new ActiveXObject(text) which is an DOM document object also points to the same memory location of the DOM document object.

------------------>

I have another doubt if you would be kind enough to clear it

====================
<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>";
text=text+"</bookstore>";

xmlDoc=loadXMLString(text);

document.write(xmlDoc.getElementsByTagName("pages")[0].nodeType);
document.write("<br />");
document.write(xmlDoc.getElementsByTagName("year")[0].nodeType);
====================

This returns
1
1
====================

document.write(xmlDoc.getElementsByTagName("pages")[0].childNodes[0].nodeType);

returns 3

Why is this when we include childNodes[0] it returns different nodeType.

Is it because it refers to the tress structure which might be of this form

bookstore
|
books
----------------------
| |
title(1) author(1)
|
pages(3)

Why is that the output changes when childNodes[0] is included

Thank you.
 
[5] First, I can criticize the tutorial for not being senisitive to scope of variables. xmlDoc inside the function and xmlDoc pretented to be what returned from the function loadXMLString(text) should live in a different scope of the application. That may contribute to some confusion is you do not have conscience on the scoping of variables in javascript. But, things can drag along no end.

[6] I am going to quote the specific lines on the usage of xml document as it will be dragging to far away and academic for the "novices", abstractly not necessary meaning yourself. Sure, I have not much of an objection against the text file of that construction of the string, or to the string variable of the same or even the text stream reading from file or being output of some process... at a condition that you've to understand xml document itself is multiple facets of an abstraction and an implements in the real. Just like a rational number, 5/7 say, being fraction of 5 and 7, 5/7 represents something which lives in a quotient space with element being the whole class of number (5/7, 10/14, 15/21,...). The same, xml doc of the xml spec speaks of the generic abstraction of some structure, such as the irrelevance of order of attributes, irrelevance of spaces separating element name and attribute(s) etc... In the recommendation, how xml is stored and how it is retrieved is never a question. It is left to the implementation of parsing... It is only to the level 3 of dom spec which loading and saving start to be the focal points. Hence, if you think the text constructed like that is an xml document, it is fine as long as you know what is generic to xml technology and what is not.

[7] As to the nodeType, the nodes of the xml document are classified into a number of types. If it is "element" node, its nodeType will be 1; if it is a text node, 3, etc. You just have to step through the tutorials to familiar with all those details. It does not help to question without learning---it is very ineffective and quick to degenerate into pure imagination.

[8] The domdocument object as implemented by ms has a property call readyState. You can check that property like for ie. In ff, the implementatin is different and it is not exposed as such.
[tt] document.write (xmlDoc.readyState); //ie only; ff shown undefined.
document.write("<br />");[/tt]

I would say continue with the tutorial and learning is never linear. Believing one can "understand" every thing linearly is an undue optimism, or in my vocabulary, simplistic.
 
Thanks a lot, tsuji for that valuable reply.

It cleared a lot of doubts, ill continue learning.

Further doubts i will see your valuable help.

Thanks again for your patience and help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top