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

How to access optional value without errors? 1

Status
Not open for further replies.

MissouriTiger

Programmer
Oct 10, 2000
185
US
I would be grateful for some advice. I'm an Asp developer & I use XML about twice a year, just often enough to get me into trouble.

I'm building a Google Checkout integration. I wrote a script which receives an XML doc from GOogle & extracts data from it.

Problem: Certain nodes may, or may not, contain a value, but the tag is always there. So my script generates an error when attempting to access a non-exstent child node (an optional value that isn't there). What I am doing at this poiont is just "On Error resume Next", but i hate to do that. Is there a different syntax I can use that won't throw an error when the value isn't there?

Example XML:

<email>joe@bloe.com</email>
<contact-name>Joe Bloe</contact-name>
<company-name />

Example Code:

pEmail = objDomGN.getElementsByTagName("email")(0).childNodes(0).nodeValue

pContact = objDomGN.getElementsByTagName("contact-name")(0).childNodes(0).nodeValue

pCompany = objDomGN.getElementsByTagName("company-name")(0).childNodes(0).nodeValue

The last line of code causes the error, since in this case there is no company name. How can I fix this not to cause an error?

Any advice will be greatly appreciated.



Greg Norris
Software Developer & all around swell guy


__________________________________________________
Constructed from 100% recycled electrons.
 
>pCompany = objDomGN.getElementsByTagName("company-name")(0).childNodes(0).nodeValue
[tt]pCompany = objDomGN.getElementsByTagName("company-name")(0).[blue]text[/blue][/tt]
It is applicable to the other two using nodeValue if so desired.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top