0

Anyone have any examples of parsing xml with vbscript? I have a .NET generic list serialized into XML that I'm sending to a classic asp page. I thought I'd be able to use XMLDom, but the libraries don't seem to be installed on the server, so I'm looking for another solution. (Was getting "Object Required: documentElement" error)

Basically I'm passing a list of around 15 objects in the form of an xml string that contains a headline and a main article section, and I want to loop through the list and print out both.

This was what I had before I found out XMLDom wasn't installed:

set xmlDoc=CreateObject("Microsoft.XMLDOM")
xmlDoc.async="false"
xmlDoc.loadXML(item)

Set objFirstChild = xmlDoc.documentElement.firstChild
Set objAttributes = objFirstChild.attributes
For Each Attr in objAttributes
   Response.write(Attr.Headline & "<br>")
   Response.write(Attr.Content & "<br>")
Next
Response.End

Any help appreciated - my VBScript is pretty rusty these days!

EDIT - Tried as well with MSXML2.DOMDocument but ended up with a Object Required error.

UPDATE - Sample XML included at request of @ulluoink:

<?xml version="1.0" encoding="utf-8"?>
<articles>
  <article>
    <newsID>7</newsID>
    <headline>This is headline 1</headline>
    <content><![CDATA[<p>This is the start of the main content of the article</p><p>This is the next paragraph.</p> ]]></content>
    <date>04/06/2013 00:00</date>
  </article>
  <article>
    <newsID>7</newsID>
    <headline>This is headline 2</headline>
    <content><![CDATA[<p>This is the start of the main content of the article</p><p>This is the next paragraph.</p> ]]></content>
    <date>04/06/2013 00:00</date>
  </article>
  <article>
    <newsID>7</newsID>
    <headline>This is headline 3</headline>
    <content><![CDATA[<p>This is the start of the main content of the article</p><p>This is the next paragraph.</p> ]]></content>
    <date>04/06/2013 00:00</date>
  </article>
</articles>
2
  • if xmldom "was not installed" you would get the error at the line where you createobject("...") Commented Jun 10, 2013 at 7:12
  • E-on, now it's hard to find out server without pre-installed XML, ulluoink right. Please checkout Your code. In our (in company) ASPClassic scripts we are using statement like this Set xmlDoc = Server.CreateObject("Msxml2.DOMDocument.3.0"). Please specify exact string number, where error raised. If it's a not string, with Server.CreateObject, try check xmlDoc.parseError property and watch on returned object. Commented Jun 10, 2013 at 9:45

2 Answers 2

1

AFAIK attribute objects don't have a property Headline or Content. Are you trying to write the values of the attributes Headline and Content of the child node? For that you need something like this:

For Each attr In objAttributes
  If attr.Name = "Headline" Or attr.Name = "Content" Then
    response.write attr.Value & "<br>"
  End If
Next
Sign up to request clarification or add additional context in comments.

Comments

1

In general, you shouldn't use DOM methods without error/plausibility checks. A minimalistic skeleton for starting with XML 'parsing' applied to your input:

  Dim sFSpec : sFSpec    = resolvePath( "..\data\17014567.xml" )
  Dim oXDoc  : Set oXDoc = CreateObject( "Msxml2.DOMDocument" )
  oXDoc.setProperty "SelectionLanguage", "XPath"
  oXDoc.async = False
  oXDoc.load sFSpec

  If 0 = oXDoc.ParseError Then
     WScript.Echo sFSpec, "looks ok"
     ' ? Set objFirstChild = xmlDoc.documentElement.firstChild
     Dim X : Set X = oXDoc.documentElement.firstChild
     WScript.Echo 0, TypeName(X), X.tagName
     ' ? Set objAttributes = objFirstChild.attributes
     Set X = X.attributes
     WScript.Echo 1, TypeName(X), X.length
     If 0 < X.length Then
        Dim Attr
        For Each Attr in X
            ' ? Attr.Headline, Attr.Content
        Next
     Else
        WScript.Echo 2, "no attributes!"
     End If
  Else
     WScript.Echo oXDoc.ParseError.Reason
  End If

output:

E:\trials\SoTrials\answers\8194209\data\17014567.xml looks ok
0 IXMLDOMElement article
1 IXMLDOMNamedNodeMap 0
2 no attributes!

clearly show, that there aren't any attributes to loop over.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.