0

任何人有任何用 vbscript 解析 xml 的例子吗?我有一个序列化为 XML 的 .NET 通用列表,我将其发送到经典的 asp 页面。我以为我可以使用 XMLDom,但是这些库似乎没有安装在服务器上,所以我正在寻找另一种解决方案。(收到“需要对象:documentElement”错误)

基本上,我以包含标题和主要文章部分的 xml 字符串的形式传递了大约 15 个对象的列表,我想遍历列表并打印出两者。

这是我在发现未安装 XMLDom 之前所拥有的:

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 很生疏!

编辑- 也尝试过,MSXML2.DOMDocument但最终出现需要对象错误。

更新 - 应@ulluoink 的要求包含的示例 XML:

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

2 回答 2

1

一般来说,你不应该使用没有错误/合理性检查的 DOM 方法。一个从 XML“解析”开始的简约框架,应用于您的输入:

  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!

清楚地表明,没有任何属性可以循环。

于 2013-06-10T10:04:50.170 回答
1

AFAIK 属性对象没有属性HeadlineContent. 您是否尝试编写属性值HeadlineContent子节点的值?为此,您需要这样的东西:

For Each attr In objAttributes
  If attr.Name = "Headline" Or attr.Name = "Content" Then
    response.write attr.Value & "<br>"
  End If
Next
于 2013-06-10T10:01:42.130 回答