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!

How to handle optional elements when parsing into variables with vbs 1

Status
Not open for further replies.

wbodger

Programmer
Apr 23, 2007
769
0
0
US
So, I receive this post from an outside vendor and there are a few elements that may or may not be present. What I am trying to figure out is how I can handle those elements that may or may not be there. I had thought I could use ISNULL, but I cannot get it to work. The code below:
Code:
	Dim myDoc  
	  
	set myDoc = Server.CreateObject("Microsoft.XMLDOM")  
	  
	myDoc.async=false  
	  
	myDoc.loadXML(request.Form("xml"))

	Dim Root
	Set Root = MyDoc.documentElement

	Dim street			: street = Trim(cStr("" &  myDoc.getElementsByTagName("cbt:LicenseeContact/cbt:Street1").item(0).text))
	Dim phone			: IF ISNULL(myDoc.getElementsByTagName("cbt:LicenseeContact/cbt:Phone1").item(0).text) then phone="" ELSE phone = Trim(cStr("" &  myDoc.getElementsByTagName("cbt:LicenseeContact/cbt:Phone1").item(0).text)) END IF
works fine with just Street1, but once I add Phone1 (and don't include it in the XML being passed) I get the error
Code:
Object required: 'getElementsByTagName(...).item(...)'

Anybody out there run into this problem before or know how I should handle it? When this field is available I need it, otherwise I will just set it to 0, but it isn't working for me!

Thanks,
Willie
 
[0] It seems to be too much clusted to put thing on one line.

[1] Mind you, this
"cbt:LicenseeContact/cbt:phone1"
can never be a tag name, ever. So the script is bound to fail whatever.

[2] But, I can visualize what you have in mind. This is how.
[tt]
Dim phone
IF (myDoc.getElementsByTagName("cbt:phone1").item(0) is nothing) THEN
phone=""
ELSE
phone = Trim(cStr(myDoc.getElementsByTagName("cbt:phone1").item(0).text))
END IF
[/tt]
ps: Eliminate all those cstr("" & ...)! Contrary to what one may be thinking it is clever, it is far from being a good scripter would do.
 
Sorry, I missed this response yesterday. OK, I can understand what you are saying in [1], but you are, in fact, incorrect. Now, that structure may not be the best way to write it, but it does work. If the element is there, this code works just fine. Again, not saying that it is the best way to do it, but it works. What I have in the XML file is
Code:
<cbt:LicenseeContact>
<cbt:Phone1>555-1212</cbt:Phone1>
</cbt:LicenseeContact>
which is where I came up with the <cbt:licenseecontact/cbt:phone1>
What is the proper way to achieve this result where I select the elements from within the node cbt:LicenseeContact? FYI, there are also similar nodes cbt:BillingContact and cbt:DeliveryContact that have the exact same element names.
Code:
<cbt:BillingContact>
	<cbt:Language>English</cbt:Language>
	<cbt:LanguageId>en</cbt:LanguageId>
	<cbt:Firstname>Willie</cbt:Firstname>
	<cbt:Lastname>Bodger</cbt:Lastname>
	<cbt:State>Washington</cbt:State>
	<cbt:StateId>WA</cbt:StateId>
	<cbt:Country>USA</cbt:Country>
	<cbt:CountryId>US</cbt:CountryId>
</cbt:BillingContact>
<cbt:DeliveryContact>
	<cbt:Language>English</cbt:Language>
	<cbt:LanguageId>en</cbt:LanguageId>
	<cbt:Company>Laplink</cbt:Company>
	<cbt:Firstname>Willie</cbt:Firstname>
	<cbt:Lastname>Bodger</cbt:Lastname>
	<cbt:State>Washington</cbt:State>
	<cbt:StateId>WA</cbt:StateId>
	<cbt:Country>USA</cbt:Country>
	<cbt:CountryId>US</cbt:CountryId>
</cbt:DeliveryContact>
<cbt:LicenseeContact>
	<cbt:Language>English</cbt:Language>
	<cbt:LanguageId>en</cbt:LanguageId>
	<cbt:Company>Laplink</cbt:Company>
	<cbt:Firstname>Willie</cbt:Firstname>
	<cbt:Lastname>Bodger</cbt:Lastname>
	<cbt:State>Washington</cbt:State>
	<cbt:StateId>WA</cbt:StateId>
	<cbt:Country>USA</cbt:Country>
	<cbt:CountryId>US</cbt:CountryId>
</cbt:LicenseeContact>

So, the Is Nothing code works, and now I am trying to build a function so I can be a bit more portable with the element checking.
Code:
function TestForValue(XmlNode)
	Dim objtest, strReturnValue

	Set objtest = xmlNode

	if Not (objtest Is Nothing) Then 
		strReturnValue= objtest.item(0).text
	else
		strReturnValue= ""
	End if

	set objtest = nothing

	TestForValue = strReturnValue
	set strReturnValue = nothing
end function
and calling like
Code:
TestForValue(myDoc.getElementsByTagName("cbt:LicenseeContact/cbt:Phone1"))
which gives me an error as it tries to find objtest.item(0).text and not myDoc.getElementsByTagName("cbt:LicenseeContact/cbt:phone1").item(0).text (which works)

Any thoughts?

wb
 
So, I got this to work. The change I had to make was to add the node when setting objtest
Code:
function TestForValue(XmlNode)
    Dim objtest, strReturnValue

    Set objtest = xmlNode.item(0)

    if Not (objtest Is Nothing) Then
        strReturnValue= objtest.text
    else
        strReturnValue= ""
    End if

    set objtest = nothing

    TestForValue = strReturnValue
    set strReturnValue = nothing
end function
However, I still have the <cbt:LicenseeContact/cbt:phone1> code in there because I can't get anything else to work.

Willie
 
I know what you say at the first few sentense. I am incorrect? maybe. That's ms specific implementation of getElementsByTagName(). If you want, stick to what you know. The rest, I pass. I hope there is no question in there you want to know.
 
Now that I can sit down to read, here is what I would say to help.

[1.1] Using anything like xpath in getElementsByTagName() is ms implementation-specific. It is fine if you buy the extension. But it is to me unsatisfactory as it would then a misnomer. On the specific of using xpath in an again ms implementation (with the useful info on cross-browser issue if one is one an user-agent web application) see my involvement in this previous thread.
SelectNodes() and SelectSingleNode() are ms specific implementation of xpath parser. You would say they are both ms-specific extension, why object getElementsByTagName() using xpath but welcome SelectNodes()..., it is because the former would be a misnomer _and_ in odd terms with standard/recommendation, whereas selectnodes() etc are not (or yet) in the standard.

[2.1] This you said works
> Set objtest = xmlNode.item(0)
If I can highlight this, that is what the form I had posted. The getElementsByTagName() return IXMLDOMSelection type which would never provoke a runtime error for well-tempered purpose, whereas, it's item content is the object noting specific to vbs just like empty in string.

[2.2] If you use only the tag name cbt:phone1 with the method, you can always control its parent tag name like this.
[tt] if objtest.parentNode.tagName="cbt:LicenceeContact" then
'do something
else
'do nothing
end if
[/tt]
[3] If you are using it in server environment, you would have better to look for msxml2.domdocument and its thread-safe version. That would be a more versatile and better supported (rewritten) and more stable component to be used in the server environment. But, you can always do it later until time.
 
Great, thank you for the info. I will definitely read up on this and see if I can implement better code!!

wb
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top