Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP General > Read an element from XML in ASP

Reply
Thread Tools

Read an element from XML in ASP

 
 
Mangler
Guest
Posts: n/a
 
      01-12-2010
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>4203271391838052139071 47133548</
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?

 
Reply With Quote
 
 
 
 
Martin Honnen
Guest
Posts: n/a
 
      01-13-2010
Mangler wrote:
> 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




--

Martin Honnen --- MVP XML
http://msmvps.com/blogs/martin_honnen/
 
Reply With Quote
 
 
 
 
p byers
Guest
Posts: n/a
 
      01-14-2010
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\Merchandise ReturnLabel")
Response.Write
xmlDoc.selectSingleNode("EMRSV4.0Response/MerchandiseReturnLabel").text


%>





. . . and was rewarded with


Error Type:
Microsoft VBScript runtime (0x800A01A
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;
yie




Pete (Northolt UK)



Mangler wrote:

> 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>4203271391838052139071 47133548</
> 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?


 
Reply With Quote
 
Dan
Guest
Posts: n/a
 
      01-14-2010

"p byers" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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\Merchandise ReturnLabel")
> 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\Mercha ndiseReturnLabel").text

>
> %>
>
>
>
>
>
> . . . and was rewarded with
>
>
> Error Type:
> Microsoft VBScript runtime (0x800A01A
> Object required: 'selectSingleNode(...)'
> /test/getXMLelementTEXT.asp, line 15


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

Dan



>
> 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;
> yie
>
>
>
>
> Pete (Northolt UK)
>
>
>
> Mangler wrote:
>
>> 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>4203271391838052139071 47133548</
>> 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?

>




 
Reply With Quote
 
Dan
Guest
Posts: n/a
 
      01-14-2010

"p byers" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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.

--
Dan


 
Reply With Quote
 
Martin Honnen
Guest
Posts: n/a
 
      01-14-2010
p byers wrote:

> 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\Merchandise ReturnLabel")
> Response.Write
> xmlDoc.selectSingleNode("EMRSV4.0Response/MerchandiseReturnLabel").text
>
>
> %>
>
>
>
>
>
> . . . and was rewarded with
>
>
> Error Type:
> Microsoft VBScript runtime (0x800A01A
> 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.

--

Martin Honnen --- MVP XML
http://msmvps.com/blogs/martin_honnen/
 
Reply With Quote
 
p byers
Guest
Posts: n/a
 
      01-15-2010


Martin Honnen wrote:

>
>
> 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.
>
> --
>
> Martin Honnen --- MVP XML
> http://msmvps.com/blogs/martin_honnen/


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.FreeThreadedDOMDocumen t.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\Merchandise ReturnLabel")
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>4203271391838052139071 47133548</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>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
nonblocking read of one xml element? mh@pixar.com Python 1 02-06-2008 12:38 AM
how to Update/insert an xml element's text----> (<element>text</element>) HANM XML 2 01-29-2008 03:31 PM
In xml schema, what's the difference between attribute "final" and "block" in element "element" tankbattle XML 1 07-21-2006 07:51 AM
Problem to insert an XML-element by XSLT-converting from one XML-file into another XML-file jkflens XML 2 05-30-2006 09:41 AM
XML: How to read element value? O.B. Java 2 01-29-2006 01:34 PM



Advertisments