• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1319
  • Last Modified:

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-9DBA3F3182AA
        </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-CFFF53F9E8BA
        </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-5F5E5848932F
        </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("Microsoft.XMLDOM")
xmlDoc.async="false"
xmlDoc.load(xmlhttp.responseText) 'load the above xml

Set rootElement = xmlDoc.documentElement

for i = 0 to (rootElement.childNodes.item(0).childnodes.length - 1)
          response.write rootElement.childNodes.item(0).childnodes.item(i).attributes.getNamedItem("returnCode").text & " > " & rootElement.childNodes.item(0).childnodes.item(i).text
next

looking forward to your answers,
thanks.
0
dfuchs1
Asked:
dfuchs1
  • 5
  • 5
1 Solution
 
Carl TawnSystems and Integration DeveloperCommented:
Try:

<%
   Set xmlDoc = Server.CreateObject("MSXML2.DOMDocument.4.0")
   xmlDoc.LoadXML xmlhttp.responseText

   Set nodes = xmlDoc.SelectNodes("/response/record")

   For Each node In nodes
      For Each child in node.ChildNodes
          Response.Write child.nodeName & ": " & child.Text & "<br>"
      Next
   Next

   Response.Write doc.SelectSingleNode("/response/returnCode").Text

   Set nodes = Nothing
   Set xmlDoc = Nothing
%>
0
 
dfuchs1Author Commented:
Carl,
thanks for you reply, but when I run your code, for each for each loop doesn't return anything and

Response.Write doc.SelectSingleNode("/response/returnCode").Text line throws this error:
--------------
Microsoft VBScript runtime error '800a01a8'
Object required: 'xmlDoc.SelectSingleNode(...)'
--------------
why do you think that is???
0
 
Carl TawnSystems and Integration DeveloperCommented:
Its probably a problem loading the XML.

Change:

    xmlDoc.LoadXML xmlhttp.responseText

To:

    bOK = xmlDoc.LoadXML(xmlhttp.responseText)
    If Not bOK Then
        Response.Write xmlDoc.parseError.reason
        Response.Flush
    End If

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
dfuchs1Author Commented:
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("/response/returnCode").Text line. I also tried to change that to
Response.Write doc.SelectSingleNode("//response/returnCode").Text and same result ....
0
 
Carl TawnSystems and Integration DeveloperCommented:
Sorry, that one is a typo.

    Response.Write doc.SelectSingleNode("/response/returnCode").Text

Should be:

    Response.Write xmlDoc.SelectSingleNode("/response/returnCode").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.responseText) 'load the above xml

Worked ok originally, then change the code back to that.
0
 
dfuchs1Author Commented:
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("MSXML2.DOMDocument.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.
0
 
Carl TawnSystems and Integration DeveloperCommented:
Server.CreateObject("MSXML2.DOMDocument.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.
0
 
dfuchs1Author Commented:
Carl,
this is my code now:
--------
Set xmlDoc = Server.CreateObject("MSXML2.DOMDocument.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/returnCode").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.
0
 
Carl TawnSystems and Integration DeveloperCommented:
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.
0
 
dfuchs1Author Commented:
thanks Carl
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 5
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now