Advanced Web Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsWeb DesignAdvanced Web Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old September 20th, 2003, 04:38 PM
FrankieShakes FrankieShakes is offline
Frank The Tank!
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: Jun 2002
Location: Toronto, Canada
Posts: 1,246 FrankieShakes User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 9
Send a message via ICQ to FrankieShakes Send a message via MSN to FrankieShakes
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

Reply With Quote
  #2  
Old September 21st, 2003, 04:13 AM
md2perpe md2perpe is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Sep 2003
Posts: 12 md2perpe User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
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.

Reply With Quote
  #3  
Old September 21st, 2003, 02:31 PM
FrankieShakes FrankieShakes is offline
Frank The Tank!
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: Jun 2002
Location: Toronto, Canada
Posts: 1,246 FrankieShakes User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 9
Send a message via ICQ to FrankieShakes Send a message via MSN to FrankieShakes
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!

Reply With Quote
  #4  
Old September 22nd, 2003, 04:38 PM
md2perpe md2perpe is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Sep 2003
Posts: 12 md2perpe User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Quote:
But in order to parse the document, you have to traverse it, don't you?

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:
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.)?

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:
Quote:
Given a node, how would you find its parent using getElementsByTagName()?


Can you explain this a little further?

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()?

Reply With Quote
  #5  
Old September 25th, 2003, 06:24 PM
FrankieShakes FrankieShakes is offline
Frank The Tank!
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: Jun 2002
Location: Toronto, Canada
Posts: 1,246 FrankieShakes User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 9
Send a message via ICQ to FrankieShakes Send a message via MSN to FrankieShakes
Quote:
Originally posted by md2perpe

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.


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:
Originally posted by md2perpe

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()?


I see your point in that... But you mentioned in your first post:

Quote:
Given a node, how would you find its parent using getElementsByTagName()?


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.

Reply With Quote
  #6  
Old September 26th, 2003, 01:46 PM
md2perpe md2perpe is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Sep 2003
Posts: 12 md2perpe User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
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.

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:
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?

Reply With Quote
  #7  
Old September 26th, 2003, 03:43 PM
FrankieShakes FrankieShakes is offline
Frank The Tank!
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: Jun 2002
Location: Toronto, Canada
Posts: 1,246 FrankieShakes User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 9
Send a message via ICQ to FrankieShakes Send a message via MSN to FrankieShakes
Quote:
Originally posted by md2perpe

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?


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!

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsWeb DesignAdvanced Web Development > XML Confusion


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump




 Free IT White Papers!
 
Create the Optimal Architecture for your Critical Applications
Warburton's the largest independently owned bakery in the UK faced a number of difficult challenges in providing the most robust yet efficient IT infrastructure for their organization's success. IBM's services combined with their xSeries servers created the perfect platform for their SAP environment with sufficient flexibility, and did so in very time effective fashion.

Request Your Free Technology Downloads!
 
Five Best Practices for Deploying a Successful Service-Oriented Architecture
This white paper describes the benefits you can expect with SOA, and how IBM can help take your business there.

Request Your Free Technology Downloads!
 
Gartner Magic Quadrant for Application Delivery Controllers
Gartner summarizes its view on Application Delivery Controllers, evaluates strengths and weaknesses of solutions, and provides Magic Quadrant reporting for a quick comparison across all vendors. Learn from Gartner how you can benefit from an all-in-one device like Citrix NetScaler that delivers the highest levels of availability, performance and security.

Request Your Free Technology Downloads!
 
Knowledge is Power
What you don't know can hurt you, and is likely costing you money and increasing your security risks during an era of scarce resources. This white paper proposes six key strategies that enterprise security managers can use to improve their network defense posture.

Request Your Free Technology Downloads!
 
Rationalizing the Multi-Tool Environment
The rationalized multi-tool approach is flexible, scalable and cost effective. It provides the necessary input to the IT service management business processes. It preserves prior investments in monitoring tools, empowers technologists to select the best tools with which to do their jobs, and enhances effective response to incidents.

Request Your Free Technology Downloads!
 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 




© 2003-2010 by Developer Shed. All rights reserved. DS Cluster 2 Hosted by Hostway
For more Enterprise Application Development news, visit eWeek