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

node exists?

Status
Not open for further replies.

jez

Programmer
Apr 24, 2001
370
VN
Hi

I am trying to check if a node exists in an XML file before i output the value of one of it's attributes.

I am using ASP. (unfortunately)

I have tried using isNull but it always returns true using the MS dom.

If i use selectNodes with an XQL query and then looking at the length value of the object returned, it causes my existing code to break because i am looping through all the elements of a certain name within my domdoc.

Isnt there a simple method for checking an element exists and if not why not surely it should be part of the specification (for error checking if nothing else).


Thanks

Jez
 
This is fairly simple to accomplish using an xPath query and the selectSingleNode method of the DOM object. For example (found in Google Groups):

-----
Code:
strXPath = "MyCollection[MyAttributeName =
'MyAttributeValue']/MyNodePath/MyNodePath/MyNodeName"
If xmlDoc.selectSingleNode(strXPath) Is Nothing Then
    ' The node does not exist
End If
-----

Hope this helps.

Glen Appleton

VB.Net student.
 
thanks for the reponse but it doesnt work.

my code...

xpth = "//Meeting/Race[" & raceNo & "]/Horse[" & counter & "]/LastRunDays"
if oXML.selectSingleNode(xpth) Is Nothing then response.write "error" else
response.write oXML.selectSingleNode(xpth).getAttribute("days")

this outputs the word "error" and then continues to try and output the getAttrribute and causes an error anyway.


 
Glen,

Just wanted to say, it worked like a charm for me. I used it in ASP with VBScript.

Thanks!
 
since i posted this, i have had various problem with the XML type methods im using and i have pinned it down to the version of the XMLdoc object i am creating for instance

Set oXMLDom = Server.CreateObject("MSXML2.DOMDocument")

seems to be far more reliable than

Set oXml = Server.CreateObject("Microsoft.xmldom")

but then trying the version 3 of this object just seems to break everything.

I will try this process again, to see if it still doesnt work.

JEz
 
Hi Jez,

I've only used the MSXML2 objects (in MSXML 3.0 and 4.0), and I can tell you that I haven't had any issues with the libraries so far. Make sure you check out the SDK documentation for any new methods or properties that might help with your project.


Glen Appleton

VB.Net student.
 
You can also use the count() function in XPath. The only question is if it's supported in your version of the XML parser.

CHip H.


If you want to get the best response to a question, please check out FAQ222-2244 first
 
MSXML 2.0 is obsolete and can be unpredictable. Use at your own risk.
 
harebrain, can you tell me specifically which XML object to use then? (for reliability etc in your opinion)
 
It's not the objects, it's the entire library. I'm not an MSXML expert, but my understanding is that 3.0 is the first version that conforms to the W3C recommendation. Version 2.0 "jumped the gun" -- the XML-related technologies (XPath, etc.) were implemented before the recommendation was finalized. When the recommendation was finalized, MS was left with an idiosyncratic implementation. You can infer why they released MSXML3.

Personally, I'd go with the most current version.
 
I too am not big into ASP, so to use the version 3 library would my code be....


Set oXMLDom = Server.CreateObject("MSXML3.DOMDocument")


 
Actually, no. If you read the SDK documentation for 3.0 (and 4.0), you will see that the object is still MSXML2. For version dependant reference to the 3.0 DOM document, use:

Dim objDoc As New MSXML2.DOMDocument30

For version dependant reference to the 4.0 DOM document, use:

Dim objDoc As New MSXML2.DOMDocument40

Hope this helps.


Glen

VB.Net student.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top