【问题标题】:Accessing XML Attributes访问 XML 属性
【发布时间】:2016-01-22 21:49:53
【问题描述】:

我有一个包含节点和属性的 XML 文件。我正在使用经典 ASP 从 XML 访问和接收数据。但是 XML 文件有一些我应该在屏幕上打印的属性。

XML 文件类似于

<root>
<product>
<node1>Node1 Value</node1>
<node2>Node2 Value</node2>
<attribute value="category">Category Name</attribute>
</product>
</root>

我正在使用这个脚本接收数据

Set objXMLDoc = Server.CreateObject("MSXML2.DOMDocument.3.0")
objXMLDoc.async = True
objXMLDoc.load Server.MapPath("ProductList3.xml")

Dim xmlProduct
For Each xmlProduct In objXMLDoc.documentElement.selectNodes("product")

     Dim node1 : node1 = xmlProduct.selectSingleNode("node1").text
     Dim node2 : node2 = xmlProduct.selectSingleNode("node2").text

     Response.Write "<b>node1:</b>" & Server.HTMLEncode(node1) & "<br> "
     Response.Write "<b>node2:</b>" & Server.HTMLEncode(node2) & "<br>"   %>
Next

访问节点没有任何问题,但我需要获取属性值“类别”,所以我尝试了类似

Dim category : Set category = getText(xmlProduct.SelectNodes("root/product/attribute value[@name='category']")

但我收到了")" required in line 52 error (err no:800a03ee)

Set category= getText(xmlProduct.SelectNodes("root/attribute value[@name='Category']")

我必须在属性中获取类别名称但找不到任何解决方案,也许我在第 52 行完全错了。你们能帮我解决问题吗?

【问题讨论】:

  • 您的 XML 中没有名为 urun 的节点。请仔细检查您的示例代码是否与您的示例输入文档匹配。

标签: xml asp-classic


【解决方案1】:
Dim productList, product, node1, node2, category

Set productList = Server.CreateObject("MSXML2.DOMDocument.3.0")
productList.async = False  ' you don't want async document loading on the server
productList.load Server.MapPath("ProductList3.xml")

For Each product In productList.selectNodes("/root/product")
     Set node1 = product.selectSingleNode("./node1")
     Set node2 = product.selectSingleNode("./node2")
     Set category = product.selectSingleNode("./attribute[@value='category']")

     Response.Write "<b>node1:</b>" & Server.HTMLEncode(node1.text) & "<br>"
     Response.Write "<b>node2:</b>" & Server.HTMLEncode(node2.text) & "<br>"
     Response.Write "<b>category:</b>" & Server.HTMLEncode(category.text) & "<br>"
Next

由于在不知道该节点是否实际存在的情况下选择节点并使用它们的属性不是很聪明(selectSingleNode 可以返回Nothing,这将导致上面代码中的运行时错误),以下是很多使用更安全:

For Each product In productList.selectNodes("/root/product")
     node1 = GetText(product, "./node1")
     node2 = GetText(product, "./node2")
     category = GetText(product, "./attribute[@value='category']")

     Response.Write "<b>node1:</b>" & Server.HTMLEncode(node1) & "<br>"
     Response.Write "<b>node2:</b>" & Server.HTMLEncode(node2) & "<br>"
     Response.Write "<b>category:</b>" & Server.HTMLEncode(category) & "<br>"
Next

Function GetText(context, xpath)
    Dim node
    GetText = ""
    If Not context Is Nothing And xpath > "" Then
        Set node = context.selectSingleNode(xpath)
        If Not node Is Nothing Then GetText = node.text
    End If
End Function

【讨论】:

  • 感谢您的帮助。编辑了文字,希望对其他人有很大帮助。
  • 不幸的是,其中一个属性中有一个“ç”字母。有没有办法从这个属性中获取数据:Content
  • @Serhat 上面的代码可以读取所有字符,不要更改。在您的情况下,ç 字符会发生什么情况?
  • 只是不写属性的内容。我通过在 中查找并用“cik”替换“çık”来确认它,然后它工作正常。
  • 但我必须修复它,因为如果我每次更新 XML 文件时都必须进行替换,那么代码将无用。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-28
  • 2016-11-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-31
相关资源
最近更新 更多