Reputation: 1605
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>
Upvotes: 0
Views: 4111
Reputation: 38745
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.
Upvotes: 1
Reputation: 200293
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
Upvotes: 1