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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

<contact> <name> <firstname>sa

Status
Not open for further replies.

saurabh

Programmer
Jul 10, 2000
27
0
0
IN
<contact>
<name>
<firstname>saurabh
<mr> mr </mr>
</firstname>
<mname> kumar </mname>
<lastname> mal </lastname>
</name>
<address> d 32 </address>
<street> old </street>
<city> delhi </city>
<state> ND </state>
<telephone> 33333 </telephone>
<email> in@in.com </email>
</contact>

This my sample XML.....

This is my sample XML. Now the problem i am facing is that i want to print the name and value for every tag (incl of child tags).

1. Now if i find out the length of the address tag (which has no child nodes) then the length returned is 1....but if check the length of tag which has 1 child tag then also the length returned is 1...i am checking length of tag using (haschildnodes.length). So how do i check the number of child nodes present in case of above XML sample.

2. What is the difference between (nodevalue) and (text) methods of XMLDOM. eg. In case of <name> tag i want to print name and value of every tag but if i use (text) it prints the value - saurabh mr kumar mal ---- and nodevalue returns null. How should i do it.

Thanks
 
1. Technically, the address tag does have a child node, though you don't specifically name it. Any node that has text within it (ex. Address's &quot;d 32&quot;) contains a child of type <#text>. In this instance, the <address> node's child text is &quot;d 32&quot;. Also, using the haschildnodes method will return only child nodes - not grandchild nodes, or any deeper in the tree.

2. nodevalue will return the value of a specified node, whether that node is an attribute type node, or an element type node, or other (there are a total of 12 node types). If you check nodename for your <address> tag, it will return &quot;address&quot;. The <address> element doesn't have a value, which explains why it (or any other element, for that matter) will return a null when you use the nodevalue method. These elements do however contain child text nodes which contain the text values of your data.

It's confusing, I admit. One problem is that you have set up your xml file a little awkwardly. It really should make logical sense to anyone looking at it, as its sole purpose is to describe data. For example, why do you have an element of type <mr> as a child of your firstname element? I think I understand what you were getting at, but is it the best way to describe your data? At the very least, it may make more sense to change this <mr> element to a @title attribute of the <name> element. Actually, this change would be kind of cool, because not only would it solve your text querying problems, but it would open the door to some pretty cool searches. For instance, you could selectively search your data for males, or females pretty easily. All elements with an attribute of type &quot;title&quot; with a value of &quot;Mr.&quot; would be presumably male, and value of &quot;Mrs.&quot; presumably female, etc.

Hope this helps...
 
thanks for the real cool info......

thanks
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top