|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
|
|
Free Web 2.0 Code Generator! Generate data entry and reporting .NET Web apps in minutes. Quickly create visually stunning, feature-rich apps that are easy to customize and ready to deploy. Download Now!
|
|
#1
|
|||
|
|||
|
XML Confusion
Alright... Here's something I'm confused about...
Why not just use the getElementsByTagName() method EVERYTIME you parse a document? Rather than using childNode/parentNode, etc., isn't it easier to just grab every element by the tag name? I'm sure there's an explanation as to why you shouldn't... so if anybody can shed some light, I would appreciate it!
__________________
____________________________________________ Developer Shed Weekly Writer | DevArticles Forum Moderator Build Your Own KlipFolio Klip With PHP FrankManno.com - Under Construction Design Interactive Group - Under Construction |
|
#2
|
|||
|
|||
|
Given a node, how would you find its parent using getElementsByTagName()?
Also, not every node is an element node. There are text nodes, attribute nodes, comment nodes, ... By the way, the methods getElementsByTagName() and getElementById(), and the properties childNodes, parentNode etc. are for traversing the XML tree, not for parsing it. |
|
#3
|
|||
|
|||
|
md2perpe,
Thanks for the brief explanation... I had a feeling I was using the wrong terminology! ![]() But in order to parse the document, you have to traverse it, don't you? Also, you said that not everything is an element... That brings up another question I had regarding whitespace. The book I have explains that whitespace is considered a node itself... Is this always true? Does it depend on the DOM model you're using (ie: Microsoft.XMLDOM, PHP's XML_DOM, etc.)? [quote] Given a node, how would you find its parent using getElementsByTagName()?[/node] Can you explain this a little further? Thanks! |
|
#4
|
||||||
|
||||||
|
Quote:
What do YOU mean with parsing an XML document? For me parsing is the process of reading the text file and building a tree. By traversing I mean walking a tree in some way. Quote:
I'm not an expert on the XML DOM, but I've seen a difference in whitespace handling between Internet Explorer and Mozilla Firebird. IE considered "<tag> </tag" as empty, Mozilla as it having an inner text node consisting of spaces. Probably Mozilla is doing it the "right" way. Quote:
Unfortunately, I can't find a good example right now. If you had been proposing that one should use getElementById(), I'd unstood your point better, but using getElementsByTagName()... Nope... How would you select a particular element node with that? There can be a lot of element nodes of the same type. Just consider an (X)HTML unordered list: <ol> <li>Alpha</li> <li>Beta</li> <li>Gamma</li> </ol> How would you select the Beta list item using only getElementsById()? |
|
#5
|
|||||
|
|||||
|
Quote:
I see where you're coming from... But don't you have to traverse the document in order to parse it? That's why I see them as being one and the same... Quote:
I see your point in that... But you mentioned in your first post: Quote:
I found the answer, as I've used it myself in this last assignment. Here's an example: Code:
exList = xmldoc.getElementsByTagName("yourElement");
for (i = 0; i < exList.length; i++){
parentNode = exList.item(i).parentNode.firstChild.text;
}
That's just an example (using JavaScript)... But that would return the Parent element's text node. |
|
#6
|
||||
|
||||
|
Quote:
It would return the content of the last yourElement's parents text node (if there is one, otherwise there will be an error). Elements of the same type can have different parent nodes: <ul><li>Alpha</li></ul> <ul><li>Sigma</li></ul> The Alpha li-node and the Sigma li-node have different parents (ul-nodes). Also neither of the parents have any text child node. And look! You're using parentNode. Remember what you wrote in your first post? Quote:
|
|
#7
|
|||
|
|||
|
Quote:
That's true... But if the elements are of the same parent, you'll be returned what you're expecting. I guess it's all relative to the way you layout your document... I'm fairly new to XML, so certain concepts are still being learned.... If you have any other tips, please let me know, as I've found your perspective very insightful! |
![]() |
| Viewing: Dev Articles Community Forums > Web Design > Advanced Web Development > XML Confusion |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|