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

Parsing XML and filling variables

Status
Not open for further replies.

wbodger

Programmer
Apr 23, 2007
769
US
OK, I have this code for parsing an incoming XML post.

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 city : city = Trim(cStr("" &  myDoc.getElementsByTagName("cbt:LicenseeContact/cbt:City").item(0).text))  
    Dim StateCode : StateCode = Trim(cStr("" &  myDoc.getElementsByTagName("cbt:LicenseeContact/cbt:StateId").item(0).text))  
	Dim postalcode : postalcode = Trim(cStr("" &  myDoc.getElementsByTagName("cbt:LicenseeContact/cbt:PostalCode").item(0).text)) 
	Dim countrycode	: countrycode = Trim(cStr("" &  myDoc.getElementsByTagName("cbt:LicenseeContact/cbt:CountryId").item(0).text)) 
	Dim phone : phone = Trim(cStr("" &  myDoc.getElementsByTagName("cbt:LicenseeContact/cbt:Phone1").item(0).text)) 
	Dim lang : lang = Trim(cStr("" &  myDoc.getElementsByTagName("cbt:LicenseeContact/cbt:Language").item(0).text))

However, those values are not always included. For instance, I may not get Phone1. I know that my code right now will return an error, but am I missing something whereby I could easily set a default value if that field is not included in the XML doc? I know there is an IsNull function in VBScript so I could do an
Code:
IF IsNull(expression) THEN variable='some preset value'
statement, just wondering if there is something else that I could do.

Thanks,
Willie
 
You have 2 choices as far as I can see... 1) write error trapping code (On Error Resume Next ... If Err <> 0 then ...), or 2) Parse your XML from the beginning using a recursive read.

PSC

Governments and corporations need people like you and me. We are samurai. The keyboard cowboys. And all those other people out there who have no idea what's going on are the cattle. Mooo! --Mr. The Plague, from the movie "Hackers
 
Thanks for the response. I forgot I had posted this question before I solved it. I'm not at the office any longer, but will try to get my solution posted tomorrow.
 
If the value does not exist it won't show as Null but as nothing so you could do something like

if not myDoc.getElementsByTagName("cbt:LicenseeContact/cbt:phone1") is nothing then phone = Trim(cStr("" & myDoc.getElementsByTagName("cbt:LicenseeContact/cbt:phone1").item(0).text))

--------------------------------------------------------------------------------
dm4ever
My philosophy: K.I.S.S - Keep It Simple Stupid
 
Yeah, I ended up with something like that

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

And it works well, doesn't seem to really slow anything down, but if you see something that isn't quite right, I am always open to help!!

Willie
 
xml Deserialization is what you want, turn xml data straight into instances of classes in your code...never tried it with vbscript though, works a treat in .net
 
I do believe that the XML serialization is a .Net class. Perhaps when thing quiet down a little we can move to .Net (though some folks are a bit resistant to it, still!)...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top