Read an element from XML in ASP

M

Mangler

I am trying to get the data inside of an element from an XML file
stream. Today is my first attempt to try this and of course nothing I
find on the net is working. Here is what I have:

<%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>
<%
Dim xmlDoc
set xmlDoc = createObject("MSXML2.DOMDocument")

xmlDoc.async = False
xmlDoc.setProperty "ServerHTTPRequest", true


xmlDoc.load("URL_TO_XML_DOCUMENT")

response.write xmlDoc.selectNodes("\\EMRSV4.0Response
\MerchandiseReturnLabel")


%>



here is what the XML document looks like:

<EMRSV4.0Response>
<Zone>5</Zone>
<MerchandiseReturnLabel>JVBERi0xLjINCjUgMCBvYmoN=</
MerchandiseReturnLabel>
<DeliveryConfirmationNumber>420327139183805213907147133548</
DeliveryConfirmationNumber>
<InsuranceCost>1.75</InsuranceCost>
<PDUPOBox>240 SPRINGVIEW COMMERCE DR</PDUPOBox>
<PDUCity>DEBARY</PDUCity>
<PDUState>FL</PDUState>
<PDUZip5>32713</PDUZip5>
<PDUZip4>4834</PDUZip4>
<Postnet>32713483440</Postnet>
<CustomerAddress1 />
<CustomerAddress2>6406 IVY LN</CustomerAddress2>
<CustomerCity>GREENBELT</CustomerCity>
<CustomerState>MD</CustomerState>
<CustomerZip5>20770</CustomerZip5>
<CustomerZip4>1441</CustomerZip4>
<CustomerPostNet>20770144106</CustomerPostNet>
</EMRSV4.0Response>


I just need to return the value inside the MerchandiseReturnLabel
tag.

Can anyone help?
 
M

Martin Honnen

Mangler said:
I am trying to get the data inside of an element from an XML file
stream. Today is my first attempt to try this and of course nothing I
find on the net is working. Here is what I have:

<%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>
<%
Dim xmlDoc
set xmlDoc = createObject("MSXML2.DOMDocument")

It is better to use version indentifier e.g. for MSXML 3 you would use
Set xmlDoc = CreateObject("MSXML2.DOMDocument.3.0")
as that way you at least know which MSXML version exactly you use.
MSXML 3 is available anywhere where IE 6 or later is installed.
xmlDoc.async = False
xmlDoc.setProperty "ServerHTTPRequest", true


xmlDoc.load("URL_TO_XML_DOCUMENT")


If you use MSXML 3, as suggested above, then you first should set
xmlDoc.setProperty "SelectionLanguage", "XPath"
before using selectNodes or selectSingleNode.
response.write xmlDoc.selectNodes("\\EMRSV4.0Response
\MerchandiseReturnLabel")

XPath uses the slash "/", not the backslash.
And if you want a single node then don't use selectNodes, use
selectSingleNode e.g.
Response.Write
xmlDoc.selectSingleNode("EMRSV4.0Response/MerchandiseReturnLabel").text
 
P

p byers

Martin,


In the furtherance of my knowledge base, I used the following script





<%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>
<%
Dim xmlDoc
''set xmlDoc = createObject("MSXML2.DOMDocument")
Set xmlDoc = CreateObject("MSXML2.DOMDocument.3.0")

xmlDoc.async = False
xmlDoc.setProperty "ServerHTTPRequest", true
xmlDoc.setProperty "SelectionLanguage", "XPath"


xmlDoc.load("URL_TO_XML_DOCUMENT")

''response.write
xmlDoc.selectNodes("\\EMRSV4.0Response\MerchandiseReturnLabel")
Response.Write
xmlDoc.selectSingleNode("EMRSV4.0Response/MerchandiseReturnLabel").text


%>





. . . and was rewarded with


Error Type:
Microsoft VBScript runtime (0x800A01A8)
Object required: 'selectSingleNode(...)'
/test/getXMLelementTEXT.asp, line 15


Browser Type:
Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Trident/4.0; .NET CLR
1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729;
yie8)




Pete (Northolt UK)
 
D

Dan

p byers said:
Martin,


In the furtherance of my knowledge base, I used the following script





<%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>
<%
Dim xmlDoc
''set xmlDoc = createObject("MSXML2.DOMDocument")
Set xmlDoc = CreateObject("MSXML2.DOMDocument.3.0")

xmlDoc.async = False
xmlDoc.setProperty "ServerHTTPRequest", true
xmlDoc.setProperty "SelectionLanguage", "XPath"


xmlDoc.load("URL_TO_XML_DOCUMENT")

This is trying to load the string "URL_TO_XML_DOCUMENT" as XML, which will
fail. I think you meant to use

xmlDoc.load(URL_TO_XML_DOCUMENT)

assuming that URL_TO_XML_DOCUMENT is a variable holding the url.

''response.write
xmlDoc.selectNodes("\\EMRSV4.0Response\MerchandiseReturnLabel")
Response.Write
xmlDoc.selectSingleNode("EMRSV4.0Response/MerchandiseReturnLabel").text


Where are your slashes denoting the root (\\)? Why are you using / instead
of \?

response.write
xmlDoc.selectSingleNode("\\EMRSV4.0Response\MerchandiseReturnLabel").text
%>





. . . and was rewarded with


Error Type:
Microsoft VBScript runtime (0x800A01A8)
Object required: 'selectSingleNode(...)'
/test/getXMLelementTEXT.asp, line 15

The error is due to the node not being found. See the changes as above.

Dan
 
D

Dan

p byers said:
xmlDoc.selectSingleNode("EMRSV4.0Response/MerchandiseReturnLabel").text

Sorry, in the last reply I stated that you should use \\ and \. That's
wrong, it should be

xmlDoc.selectSingleNode("//EMRSV4.0Response/MerchandiseReturnLabel").text

The // at the start tells the selection handler to base the request off the
XML root. It shouldn't be required as you're starting out from there anyway,
but it's always worth qualifying it just in case code is added before this
later and then you find that you're trying to pull the node from a child
node which won't work.

However, this probably wasn't the case of the error anyway, the likely
reason was not having any XML in xmlDoc because you attempted to load a
non-XML string into rather than the XML from the URL.
 
M

Martin Honnen

p said:
Dim xmlDoc
''set xmlDoc = createObject("MSXML2.DOMDocument")
Set xmlDoc = CreateObject("MSXML2.DOMDocument.3.0")

xmlDoc.async = False
xmlDoc.setProperty "ServerHTTPRequest", true
xmlDoc.setProperty "SelectionLanguage", "XPath"


xmlDoc.load("URL_TO_XML_DOCUMENT")

''response.write
xmlDoc.selectNodes("\\EMRSV4.0Response\MerchandiseReturnLabel")
Response.Write
xmlDoc.selectSingleNode("EMRSV4.0Response/MerchandiseReturnLabel").text


%>





. . . and was rewarded with


Error Type:
Microsoft VBScript runtime (0x800A01A8)
Object required: 'selectSingleNode(...)'
/test/getXMLelementTEXT.asp, line 15

Well how exactly does the XML look?
Check xmlDoc.parseError.errorCode/reason after the load call, maybe the
XML does not get loaded at all.
 
P

p byers

Martin said:
Well how exactly does the XML look?
Check xmlDoc.parseError.errorCode/reason after the load call, maybe the
XML does not get loaded at all.

Just for your information, I switched my brain on and employed some of my
exisiting knowledge rather than just blindly "Copying and Pasting"

Sorry to have wasted your time !!

The two working files are shown below (watch out for wrap)

Thank you for your patience with a daft old fart !!

Pete (Northolt UK)


==================================================================
== getXMLelementTEXT.asp ==
==================================================================

<%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>
<%
Dim xmlDoc
''set xmlDoc = createObject("MSXML2.DOMDocument")
''Set xmlDoc = CreateObject("MSXML2.DOMDocument.3.0")

xmlFileName = "http://pb-big-tosh3/test/URL_TO_XML_DOCUMENT.xml"

set xmlDoc = Server.CreateObject("MSXML2.FreeThreadedDOMDocument.4.0")
if xmlDoc is nothing then
Response.Write "objDocument object not created<br>"
else
If Err Then
Response.Write "XML DomDocument Object Creation Error - <BR>"
Response.write Err.Description
else
xmlDoc.async = False
bLoaded = xmlDoc.Load(xmlFileName)
if (bLoaded = False) then
Response.Write (xmlFileName & " - Load Failed<P>")
Response.Write("Error code: " & xmlDoc.parseError.errorCode)
Response.Write("<br />Error reason: " & xmlDoc.parseError.reason)
Response.Write("<br />Error line: " & xmlDoc.parseError.line)
Response.Write("<br />Error linepos: " & xmlDoc.parseError.linepos)
Response.Write("<br />Error srcText: " & xmlDoc.parseError.srcText)
Response.Write("<br />Error url: " & xmlDoc.parseError.url)
Response.Write("<br />Error filepos: " & xmlDoc.parseError.filepos)
Response.End
else
xmlDoc.setProperty "SelectionLanguage", "XPath"
set createXMLDocFromFileAbs = xmlDoc

xmlDoc.async = False
xmlDoc.setProperty "ServerHTTPRequest", true
xmlDoc.setProperty "SelectionLanguage", "XPath"

''response.write
xmlDoc.selectNodes("\\EMRSV4.0Response\MerchandiseReturnLabel")
Response.Write
xmlDoc.selectSingleNode("EMRSV4.0Response/MerchandiseReturnLabel").text
end if
end if
end if


%>




==================================================================
== URL_TO_XML_DOCUMENT.xml ==
==================================================================

<?xml version="1.0" encoding="UTF-8"?>
<EMRSV4.0Response>
<Zone>5</Zone>
<MerchandiseReturnLabel>JVBERi0xLjINCjUgMCBvYmoN=</MerchandiseReturnLabel>

<DeliveryConfirmationNumber>420327139183805213907147133548</DeliveryConfirmationNumber>

<InsuranceCost>1.75</InsuranceCost>
<PDUPOBox>240 SPRINGVIEW COMMERCE DR</PDUPOBox>
<PDUCity>DEBARY</PDUCity>
<PDUState>FL</PDUState>
<PDUZip5>32713</PDUZip5>
<PDUZip4>4834</PDUZip4>
<Postnet>32713483440</Postnet>
<CustomerAddress1 />
<CustomerAddress2>6406 IVY LN</CustomerAddress2>
<CustomerCity>GREENBELT</CustomerCity>
<CustomerState>MD</CustomerState>
<CustomerZip5>20770</CustomerZip5>
<CustomerZip4>1441</CustomerZip4>
<CustomerPostNet>20770144106</CustomerPostNet>
</EMRSV4.0Response>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top