Active Server Pages Programming (ASP)/XML Parsing Continued
Expert: Srini Nagarajan - 6/3/2008
QuestionQUESTION: Hi Srini, I ran out of follow-ups so they made me ask a new question. :) The last example you gave me works well when using an existing .xml file that lives on the local server. However, I get my XMl output from a remote PHP file on a server I have no control over. Because of that, I need to feed the contents of an XML file into the XMLDOM parser instead of a filename. That's where the hang-up is. See what I mean? All the stuff you showed me works beautifully if I have an actual local filename to feed into the parser, but I don't have that available unfortuantely. :( So again, this is the on thing I've tried so far with no success even though it "should" work in theory:
---
Set mydoc = Server.CreateObject("Microsoft.XMLDOM")
mydoc.loadXML(STRING_VARIABLE_CONTAINING_XML_CODE)
TotalA = mydoc.selectNodes("//dce/response/watchlists/watchlist").length
TotalB = mydoc.selectNodes("/dce/response/watchlists/watchlist").length
TotalC = mydoc.selectNodes("dce/response/watchlists/watchlist").length
---
All 3 total variables end up being 0. :( Hopefully you can help me with this. Thanks again for your assistance Srini. It's appreciated.
ANSWER: Hi,
Sorry for i was away and just checking your email.
when you get the XML from different Server thru URL into STRING_VARIABLE_CONTAINING_XML_CODE, are you getting any data?
Did you display the XML also did you see what is the ERR you are getting?
If your XML comes like this including XML and RESPONSE header
<?xml...........>
<response .............>
<dce>
blah....
</dce>
</response>
you should remove the first two lines, so that your /dce get the value directly.
I'll try to create a simple page for you and send the code once I am done.
Thanks
-Srini
---------- FOLLOW-UP ----------
QUESTION: Thanks Srini. I'm not getting any errors and yes I did display the STRING_VARIABLE_CONTAINING_XML_CODE which is well formed. However, it does indeed contain the header variables. Here's an excerpt of the XML I'm feeding as the STRING_VARIABLE_CONTAINING_XML_CODE variable:
<?xml version="1.0"?>
<!DOCTYPE dce [
<!ENTITY apos "'">
.. about 100 more ENTITY entries ...
<!ENTITY amp "&">
]>
<dce>
<response>
<outcome>success</outcome>
<watchlists>
<watchlist>
<watchlist_id>90209</watchlist_id>
<retailer_id>1160522</retailer_id>
<name>mp3 players & calculators</name>
<default>0</default>
<send_callback>0</send_callback>
</watchlist>
</watchlists>
</response>
</dce>
So you think I should somehow strip out the XML and doctype header? Hmmmm. Not sure how I would do that, but anything's worth a try at this point. :) Looking forward to seeing your code. I don't think I've ever been this stuck before. :)
ANSWER: Hi,
I am busy with moving my house (so tired not able to type~!!!) once I setup everything I'll spend some time to create a page for you.
THanks
-Srini
---------- FOLLOW-UP ----------
QUESTION: No problem at all. I know how hectic that can be. Just don't forget about me! :)
AnswerHi
I dedicated few hours for you and found the solution!! :)
here is the issue
<name>mp3 players & calculators</name> - You can't have & (and sign) in the XML.
This will suppose to work as part of DocType entity... But ASP is not supporting Entity and throwing error!!!
Can you check the following code this should work for you. And also display detailed Error Message,
Please change the variable to your variable name and give a try.
'WORKING VERSION!!!!!
XML_CODE = ""
XML_CODE = XML_CODE &"<?xml version='1.0'?>"
XML_CODE = XML_CODE & "<dce>"
XML_CODE = XML_CODE & "<response>"
XML_CODE = XML_CODE & "<outcome>success</outcome>"
XML_CODE = XML_CODE & "<watchlists>"
XML_CODE = XML_CODE & "<watchlist>"
XML_CODE = XML_CODE & "<watchlist_id>90209</watchlist_id>"
XML_CODE = XML_CODE & "<retailer_id>1160522</retailer_id>"
XML_CODE = XML_CODE & "<name>mp3 players & calculators</name>"
XML_CODE = XML_CODE & "<default>0</default>"
XML_CODE = XML_CODE & "<send_callback>0</send_callback>"
XML_CODE = XML_CODE & "</watchlist>"
XML_CODE = XML_CODE & "<watchlist>"
XML_CODE = XML_CODE & "<watchlist_id>90209</watchlist_id>"
XML_CODE = XML_CODE & "<retailer_id>1160522</retailer_id>"
XML_CODE = XML_CODE & "<name>mp3 players & calculators</name>"
XML_CODE = XML_CODE & "<default>0</default>"
XML_CODE = XML_CODE & "<send_callback>0</send_callback>"
XML_CODE = XML_CODE & "</watchlist>"
XML_CODE = XML_CODE & "</watchlists>"
XML_CODE = XML_CODE & "</response>"
XML_CODE = XML_CODE & "</dce>"
Set mydoc = Server.CreateObject("Microsoft.XMLDOM")
myDoc.async=false
mydoc.loadXML(XML_CODE)
If mydoc.parseError.errorCode <> 0 Then
Set oErr = mydoc.parseError
sErrMsg = "XML Parsing Error. File: " & oErr.url & " <BR> Reason : " & oErr.reason & " <BR>Line: " & oErr.line & ", <BR>Character: " & oErr.linepos & ", <BR>Text: " & oErr.srcText
Response.write sErrMsg
ENd if
set oNode = mydoc.selectNodes("//response/watchlists/watchlist")
Response.write "Length : " & oNode.length & "<BR><BR><BR>"
Let me know what error it's displaying in the error if any.
-Srini