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

getElementsByTagName

Status
Not open for further replies.

acsooley

Programmer
Nov 13, 2002
32
0
0
CA
I am using DOM to read a xml file in JSP. I am using getElementsByTagName(). When I do this and want "a" I will get returned "This is a test" But I would like to read body and get all the elements, attributes and text. When I read "body" I get nothing returned. Anyone have any suggestions?
<art>
<test/>
<test1/>
<body>
<new><a href="jkhjk">This is a test</a></new>
</body>
</art>

Adam
 
If you can limit yourself to IE only you can use the document.all collection. Otherwise you will probably have to recursively traverse the children of the document.body object.

Tracy Dryden

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 
you get nothing returned from body because there is no text in the element!


<art>
<test/>
<test1/>
<body>
<new><a href="jkhjk">This is a test</a></new>
</body>
</art>

what you will need to do is create a node set from the body element:

Code:
Set objXMLDoc = CreateObject("Microsoft.XMLDOM") 
objXMLDoc.async = False 
objXMLDoc.load("your.xml") 

Set Node = objXMLDoc.documentElement.getElementsByTagName("body").selectSingleNode("new/a[@* = 'jkhjk']") 
document.write(Node.text)

untested vbscript - just to give you an idea - good reference site at

hth

simon
 
You can use innerHTML to do this (supported by most browsers but not W3C compliant) or you can recursively parse the tree. Something like:

document.getElementsByTagName("body")[0].innerHTML

Jon

"I don't regret this, but I both rue and lament it.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top