Link to home
Start Free TrialLog in
Avatar of dfuchs1
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-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.
ASKER CERTIFIED SOLUTION
Avatar of Carl Tawn
Carl Tawn
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of dfuchs1
dfuchs1

ASKER

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???
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

Avatar of dfuchs1

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("/response/returnCode").Text line. I also tried to change that to
Response.Write doc.SelectSingleNode("//response/returnCode").Text and same result ....
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.
Avatar of dfuchs1

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("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.
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.
Avatar of dfuchs1

ASKER

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.
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.
Avatar of dfuchs1

ASKER

thanks Carl