Jump to content

Need Help Understanding Xml Dom Tutorial, Please


Zten

Recommended Posts

Hi to everyone,I am learning xml through w3schools website and i have a doubt in XML DOM(Document object model) Tutorial.Under DOM load Function topic.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 thatThis function can be stored in the header of an HTML page and called from a script in the page.=====================================Part 1

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

<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 text which will hold an xml document which has parent and child nodes.Then the function loadXMLString(text) is being called to which the text is being passed in the function in part 1it will take text and create a new XMLDocDoubt:Here

xmlDoc=new ActiveXObject("Microsoft.XMLDOM");What is xmlDoc, i mean what i can understand is that an ActiveXObject is being created to which xmlDoc acts as an instance or variable pointer to the memory location of the ActiveXObject,  xmlDoc.async="false";xmlDoc.loadXML(text);return(xmlDoc);

Then this will load the xml to the Parser which will load it in memory and return what?????Does it return an DOM document type, or javascript because thats what an DOM does load a xml file into memory and convert it into javascript i did not understand clearlyThen the control passes to part 2 to the caller

xmlDoc=loadXMLString(text);

Are we calling the function xmlDocString so that the parser will be called, which will convert the xml document into a javascriptDoubt 2:The above code shows that the author node consists of a child node called pages.Can we print both of them out without writing twice by sayinginstead of document.write(xmlDoc.getElementsByTagName("author"[0].childNodes[0].nodevalue);document.write(xmlDoc.getElementsByTagName("pages"[0].childNodes[0].nodevalue);asdocument.write(xmlDoc.getElementsByTagName("author")[0].childNodes[1].nodeValue);I mean here specifying from the 0 position of author node to 1 position of child node(pages) and the 1 position of its 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 of writing 2 lines of code.I have got the following reply from one of the users when asked to help me understand but it did not clearly help me understand and it vexed the user, so i am left with my doubts this is what the user replied to my doubtsStatements marked > are my doubts/ written by me.===========================>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.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.xmlDoc is itself already an DOMDocument object. It is taking (with loadXML() method) a text string to fill up its content.>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.>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.===========================This part is unclear to me,The user replies 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.

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, thank you for helping me understand.Is xmlDOM already an DOM Document object is it because in the function loadXmlDoc(txt) in part 1 its statedxmlDoc=new ActiveXObject("Microsoft.XMLDOM");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??"=======[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")==========Does it 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, it would be very helpful to get it clarified.====================

<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 11====================document.write(xmlDoc.getElementsByTagName("pages")[0].childNodes[0].nodeType);returns 3Why is this when we include childNodes[0] it returns different nodeType.Is it because it refers to the XML tree 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 includedAlso is this how the xml tree structure looks i mean the No 1 is assigned to all the root nodes and parent nodes and elements and 3 to child elements.

Link to comment
Share on other sites

I didn't entirely understand all your questions, but I'll answer as I can.

xmlDoc=loadXMLString(text);

The loadXMLString() function will take the string you use as a parameter, parse it, and return the Document Object of the XML, so that you can access nodes with XML DOM functions (XML DOM is not only used in Javascript).When you assign a function to a variable, you're actually assigning the return value of the function to the variable.

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

The text variable is not an XML document, it is only text. It must be passed through a parser that can turn it into an interprettable object.

What does this mean "flagged by its ready state property being 4??"
When performing an HTTP request on the XML document, the "ready state" will tell you the progress of the request. When the ready state is 4, it means that a response was received from the server.
Why is this when we include childNodes[0] it returns different nodeType.
You can see a list of node types here: http://w3schools.com/dom/dom_nodetype.aspIf you scroll down the page, you'll see the following:
1 ELEMENT_NODE2 ATTRIBUTE_NODE3 TEXT_NODE4 CDATA_SECTION_NODE5 ENTITY_REFERENCE_NODE6 ENTITY_NODE...
Node type 1 is a normal element, that is represented with tags, like "<book>". Node type 3 is just text.
Link to comment
Share on other sites

Your biggest confusion seems to be that you aren't making a difference between DOM and JavaScript. An honest (and frequent) mistake, but to understand this, you should differenciate them."XML" is really the document you'll parse. To "parse" an XML document means to take its text form, and turn it into the so called DOM tree. You can also say "a JavaScript object" instead of DOM tree, though that's not the whole story.DOM is an API. An API (in its simplest definition) is a set of classes, methods, properties and functions (or in JavaScript - objects) for performing a certain task. In the case of DOM, the task is about getting and editing XML documents.And as said, when you execute a function as the variable's value (or a function's argument or... any place where you can put a variable), what you get is the value returned from the function. In the case of:

xmlDoc=loadXMLString(text);

loadXMLString() takes an XML text, and parses it, and returns the newly created DOM tree. The root of this tree is the document's "document" object.

Why is this when we include childNodes[0] it returns different nodeType.
Are you familliar with the term "inheritance"? One kind of class (or in JavaScript's case - object) can inherit the properties and methods (again objects in JavaScript) from another class (object...).XML documents are seen in DOM as a set of nodes. A node is pretty much any part in an XML document which you can take as one whole - an element is a node, an attribute is a node, a comment is a node, plain text (regardless of where it's located) is also a node. This is also a reason why we say "DOM tree".Thus, all DOM objects (DOMElement, DOMAttribute, DOMComment, DOMTextNode, etc.) inherit methods from the base node class/object (DOMNode). Among the properties they all inherit is the nodeType property.How do you do with PHP? It has DOM too. It's probably easier to learn it in PHP than in JavaScript. At least it was easier for me.
Link to comment
Share on other sites

Thanks Inglome and boen_robot for replying with such explanatory helpful answers.I now understood a lot of new concepts thatwe load text into an parser which coverts it into an DOM tree struture, i.e an DOM document object.also a DOM is acting like an api(classes, methods, constructor so on...) which consists of different levels of nodes like element nodes, attribute nodes, text nodes so on...This was very helpful in understanding better the DOM concept.Also that the ready state property is 4 means that the server is ready and it has finished loading the doccument and is in sync then sends a response back .Also the inheritance concept was helpful in understanding that all nodes have the property of inheriting from their parent nodes or super node, so DOMElement, DOMAttribute, DOMTextNode, DOMComment etc will inherit the properties from DOMNODE.Is DOMNode here refers to rootnode or is it a different thing i mean DOMNode because RootNode in the part 2 is bookstore and all the nodes are ChildNodes so is it something like the ChildNodes inherit the properties from RootNode.Sorry if i misunderstood that bit about DomNode.But thanks for everything and your valuable time.

Link to comment
Share on other sites

DOMNode refers to "any node", including the root element and the document node. Like I said, a "node" in XML is any piece of the document that can be thought of as one whole.The document node is the root node. It corresponds to... an invisible area around the whole text... the whole document really (which is where it gets its name from). The root element has it's own element node, which is a child of the document node.Every node may have child nodes (except text nodes, but still...). "childNodes" is a property of DOMNode, and thus, DOMElement, DOMAttribute, DOMDocument, etc. all have it.In the example from W3Schools, the document node (again: not visible as text) has one childNode - the bookstore element node. That node has several book elements in childNodes, along with several whitespace text nodes. The whitespace text nodes have no childs, but each book element has further nodes in "childNodes".Note that the exact names of the classes (DOMNode, DOMDocument, etc.) may vary slightly from language from language, but their concept is the same. I'm using the PHP naming notation (and I'd again urge you to try and learn DOM from PHP, since PHP is more demanding for clean code than JavaScript, and has better error reporting in general).

Link to comment
Share on other sites

Thank you very much boen_robot for that explanation, thanks a lot for your time,Well i am actually new to XML, but i have knowledge in JAVA,PL/SQL and dealing with databases its better to know xml, so thats why i am learning it also to expand my horizon.Well when i was learning xml i felt its better i learnt the structure before i jumped any further learning so i started learning DOM, r u suggesting to learn DOM in PHP tutorial, is it similar to XML DOM and easier as you were suggesting.Thanks a lot.

Link to comment
Share on other sites

I'm suggesting you read the DOM tutorial, but instead of following the examples, translate them into PHP first. For example:

xmlDoc=loadXMLDoc("books.xml");x=xmlDoc.getElementsByTagName("title")[0];

Translated into PHP, this is:

$xmlDoc = new DOMDocument;$xmlDoc->load("books.xml");$x = $xmlDoc->getElementsByTagName("title")->item(0);

Alternatively, read this part of the PHP tutorial, and follow the DOM tutorial with that secion in mind.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...