dfuchs1
asked on
loop through and read XML document using Microsoft.XMLDOM
hey gurus,
i've searched many different solutions on EE, but still can't get any of the solutions to work w/ my code.
here's an xml fragment:
-------------------------- -
<?xml version="1.0" encoding="iso-8859-1"?>
<response>
<returnCode>
1
</returnCode>
<record>
<RateId>
5A1F8833-0586-4C99-AA65-9D BA3F3182AA
</RateId>
<PolicyType>
1
</PolicyType>
<ExtendedCoverage>
0
</ExtendedCoverage>
<SpecialCoverage>
0
</SpecialCoverage>
<ProtectionClass>
1
</ProtectionClass>
<InsertDate>
2006-04-12 18:13:07.0
</InsertDate>
<InsertName>
Joe Moe
</InsertName>
<ModifyDate>
2006-04-12 18:13:07.0
</ModifyDate>
<ModifyName>
Joe Moe
</ModifyName>
</record>
<record>
<RateId>
4F3898A9-6290-4BB2-8114-CF FF53F9E8BA
</RateId>
<PolicyType>
1
</PolicyType>
<ExtendedCoverage>
0
</ExtendedCoverage>
<SpecialCoverage>
0
</SpecialCoverage>
<ProtectionClass>
2
</ProtectionClass>
<InsertDate>
2006-04-12 18:13:07.0
</InsertDate>
<InsertName>
Joe Moe
</InsertName>
<ModifyDate>
2006-04-12 18:13:07.0
</ModifyDate>
<ModifyName>
Joe Moe
</ModifyName>
</record>
<record>
<RateId>
75201346-15D0-4F55-9517-5F 5E5848932F
</RateId>
<PolicyType>
1
</PolicyType>
<ExtendedCoverage>
0
</ExtendedCoverage>
<SpecialCoverage>
0
</SpecialCoverage>
<ProtectionClass>
3
</ProtectionClass>
<InsertDate>
2006-04-12 18:13:07.0
</InsertDate>
<InsertName>
Joe Moe
</InsertName>
<ModifyDate>
2006-04-12 18:13:07.0
</ModifyDate>
<ModifyName>
Joe Moe
</ModifyName>
</record>
</response>
-------------------------- -
how do I loop through the nodes and get each value, regardless on how many <record> </record> nodes there are. Also, it would help to get the node name and then it's value. I also need to specifically read the <returnCode> value.
I tried this but no luck:
set xmlDoc = Server.CreateObject("Micro soft.XMLDO M")
xmlDoc.async="false"
xmlDoc.load(xmlhttp.respon seText) 'load the above xml
Set rootElement = xmlDoc.documentElement
for i = 0 to (rootElement.childNodes.it em(0).chil dnodes.len gth - 1)
response.write rootElement.childNodes.ite m(0).child nodes.item (i).attrib utes.getNa medItem("r eturnCode" ).text & " > " & rootElement.childNodes.ite m(0).child nodes.item (i).text
next
looking forward to your answers,
thanks.
i've searched many different solutions on EE, but still can't get any of the solutions to work w/ my code.
here's an xml fragment:
--------------------------
<?xml version="1.0" encoding="iso-8859-1"?>
<response>
<returnCode>
1
</returnCode>
<record>
<RateId>
5A1F8833-0586-4C99-AA65-9D
</RateId>
<PolicyType>
1
</PolicyType>
<ExtendedCoverage>
0
</ExtendedCoverage>
<SpecialCoverage>
0
</SpecialCoverage>
<ProtectionClass>
1
</ProtectionClass>
<InsertDate>
2006-04-12 18:13:07.0
</InsertDate>
<InsertName>
Joe Moe
</InsertName>
<ModifyDate>
2006-04-12 18:13:07.0
</ModifyDate>
<ModifyName>
Joe Moe
</ModifyName>
</record>
<record>
<RateId>
4F3898A9-6290-4BB2-8114-CF
</RateId>
<PolicyType>
1
</PolicyType>
<ExtendedCoverage>
0
</ExtendedCoverage>
<SpecialCoverage>
0
</SpecialCoverage>
<ProtectionClass>
2
</ProtectionClass>
<InsertDate>
2006-04-12 18:13:07.0
</InsertDate>
<InsertName>
Joe Moe
</InsertName>
<ModifyDate>
2006-04-12 18:13:07.0
</ModifyDate>
<ModifyName>
Joe Moe
</ModifyName>
</record>
<record>
<RateId>
75201346-15D0-4F55-9517-5F
</RateId>
<PolicyType>
1
</PolicyType>
<ExtendedCoverage>
0
</ExtendedCoverage>
<SpecialCoverage>
0
</SpecialCoverage>
<ProtectionClass>
3
</ProtectionClass>
<InsertDate>
2006-04-12 18:13:07.0
</InsertDate>
<InsertName>
Joe Moe
</InsertName>
<ModifyDate>
2006-04-12 18:13:07.0
</ModifyDate>
<ModifyName>
Joe Moe
</ModifyName>
</record>
</response>
--------------------------
how do I loop through the nodes and get each value, regardless on how many <record> </record> nodes there are. Also, it would help to get the node name and then it's value. I also need to specifically read the <returnCode> value.
I tried this but no luck:
set xmlDoc = Server.CreateObject("Micro
xmlDoc.async="false"
xmlDoc.load(xmlhttp.respon
Set rootElement = xmlDoc.documentElement
for i = 0 to (rootElement.childNodes.it
response.write rootElement.childNodes.ite
next
looking forward to your answers,
thanks.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Its probably a problem loading the XML.
Change:
xmlDoc.LoadXML xmlhttp.responseText
To:
bOK = xmlDoc.LoadXML(xmlhttp.res ponseText)
If Not bOK Then
Response.Write xmlDoc.parseError.reason
Response.Flush
End If
Change:
xmlDoc.LoadXML xmlhttp.responseText
To:
bOK = xmlDoc.LoadXML(xmlhttp.res
If Not bOK Then
Response.Write xmlDoc.parseError.reason
Response.Flush
End If
ASKER
Carl,
when I run that, the response is:
The system cannot locate the object specified.
and I still get the same error on the Response.Write doc.SelectSingleNode("/res ponse/retu rnCode").T ext line. I also tried to change that to
Response.Write doc.SelectSingleNode("//re sponse/ret urnCode"). Text and same result ....
when I run that, the response is:
The system cannot locate the object specified.
and I still get the same error on the Response.Write doc.SelectSingleNode("/res
Response.Write doc.SelectSingleNode("//re
Sorry, that one is a typo.
Response.Write doc.SelectSingleNode("/res ponse/retu rnCode").T ext
Should be:
Response.Write xmlDoc.SelectSingleNode("/ response/r eturnCode" ).Text
I was using doc in my test script, but forgot to switch it back to your variable name when posting. The other error is occuring because the DOMDocument is unable to load the XML.
If:
xmlDoc.load(xmlhttp.respon seText) 'load the above xml
Worked ok originally, then change the code back to that.
Response.Write doc.SelectSingleNode("/res
Should be:
Response.Write xmlDoc.SelectSingleNode("/
I was using doc in my test script, but forgot to switch it back to your variable name when posting. The other error is occuring because the DOMDocument is unable to load the XML.
If:
xmlDoc.load(xmlhttp.respon
Worked ok originally, then change the code back to that.
ASKER
I figured out your typo at your first post. I tried changing from load to loadXML and no luck. here's the response now
Invalid at the top level of the document.
In order to create Server.CreateObject("MSXML 2.DOMDocum ent.4.0") object, which dll do I need to have in my win/system32 folder? I do have msxml2.dll, msxml3.dll, msxml4.dll and msxml6.dll ...??
I'm increasing points to 200. Please help.
Invalid at the top level of the document.
In order to create Server.CreateObject("MSXML
I'm increasing points to 200. Please help.
Server.CreateObject("MSXML 2.DOMDocum ent.4.0") uses msxml4.dll, which you have. If it wasn't there then it would throw a different error.
It sounds like there is a problem retrieving the XML from your XmlHttp object.
Try putting the results of xmlhttp.ResponseText into a variable and displaying it to make sure it is coming back properly:
sResult = xmlhttp.responseText
Response.Write sResult
Response.Flush
You may have to "View Source" the page to see the XML.
It sounds like there is a problem retrieving the XML from your XmlHttp object.
Try putting the results of xmlhttp.ResponseText into a variable and displaying it to make sure it is coming back properly:
sResult = xmlhttp.responseText
Response.Write sResult
Response.Flush
You may have to "View Source" the page to see the XML.
ASKER
Carl,
this is my code now:
--------
Set xmlDoc = Server.CreateObject("MSXML 2.DOMDocum ent.4.0")
bOK = xmlDoc.loadXML("nc.xml")
If Not bOK Then
Response.Write xmlDoc.parseError.reason
Response.Flush
End If
Response.Write xmlDoc.SelectSingleNode("/ response/r eturnCode" ).Text
Set xmlDoc = Nothing
-----------
nc.xml file has the above xml fragment. When I run this, I get
Microsoft VBScript runtime error '800a01a8'
Object required: '[object]'
on response.write line.
this is my code now:
--------
Set xmlDoc = Server.CreateObject("MSXML
bOK = xmlDoc.loadXML("nc.xml")
If Not bOK Then
Response.Write xmlDoc.parseError.reason
Response.Flush
End If
Response.Write xmlDoc.SelectSingleNode("/
Set xmlDoc = Nothing
-----------
nc.xml file has the above xml fragment. When I run this, I get
Microsoft VBScript runtime error '800a01a8'
Object required: '[object]'
on response.write line.
If you are loading from a file then you need to use:
bOK = xmlDoc.Load("nc.xml")
Load is used to load XML from a file, LoadXML is used to load XML from a string.
bOK = xmlDoc.Load("nc.xml")
Load is used to load XML from a file, LoadXML is used to load XML from a string.
ASKER
thanks Carl
ASKER
thanks for you reply, but when I run your code, for each for each loop doesn't return anything and
Response.Write doc.SelectSingleNode("/res
--------------
Microsoft VBScript runtime error '800a01a8'
Object required: 'xmlDoc.SelectSingleNode(.
--------------
why do you think that is???