用vbscript解析xml [英] parsing xml with vbscript
问题描述
有人有使用vbscript解析xml的示例吗?我有一个序列化为XML的.NET通用列表,该列表将发送到经典的ASP页面.我以为我可以使用XMLDom,但是这些库似乎未安装在服务器上,因此我正在寻找另一种解决方案. (出现"Object Required:documentElement"错误)
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)
基本上,我以xml字符串的形式传递了大约15个对象的列表,该XML字符串包含标题和主要文章部分,我想遍历该列表并打印出来.
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.
这是我发现未安装XMLDom之前的状态:
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
感谢任何帮助-这些天我的VBScript非常生锈!
Any help appreciated - my VBScript is pretty rusty these days!
编辑-也尝试过MSXML2.DOMDocument
,但最终出现对象必需"错误.
EDIT - Tried as well with MSXML2.DOMDocument
but ended up with a Object Required error.
更新-应@ulluoink的要求提供了示例XML:
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>
推荐答案
通常,如果没有错误/合理性检查,则不应使用DOM方法.将XML解析"开始的简约框架应用于您的输入:
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
输出:
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.
这篇关于用vbscript解析xml的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!