【问题标题】:Classic ASP updating child nodes in XML经典 ASP 更新 XML 中的子节点
【发布时间】:2011-06-19 21:52:46
【问题描述】:

我正在尝试更新 XML 文件的子节点 IE 更改值。

XML 文件如下所示:

<user>
  <firstname>Andre</firstname>
  <lastname>Bruton</lastname>
</user>

这是我的经典 asp 代码:

users_firstname = "Tristan"  ''# New code to put in the XML file

Set xmlObj = Server.CreateObject("MSXML2.FreeThreadedDOMDocument")
xmlObj.async = False
xmlObj.setProperty "ServerHTTPRequest", True
xmlObj.Load(cURL)
If xmlObj.parseError.errorCode <> 0 Then
  Response.Write "Error Reading File - " & xmlObj.parseError.reason & "<p>"
End If

Set xmlList = xmlObj.getElementsByTagName("user")
For Each xmlItem In xmlList
  For Each xmlItem2 In xmlItem.childNodes
    a = xmlItem2.nodeName
    if a = "firstname" then firstname = xmlItem2.text
    if a = "lastname" then lastname = xmlItem2.text
  Next
Next

If firstname <> users_firstname Then
  Set nodeBook = xmlObj.selectSingleNode("//firstname")
  nodeBook.setAttribute "firstname", users_firstname
  Response.Write nodeBook.getAttribute("firstname")
  xmlObj.save(cDir & cFile)
End If

Set xmlObj = Nothing

问题在于它向 XML 文件添加了一个新部分,而不是将 firstname 的值从 Andre 更新为 Tristan。 XML 如下所示:

<user>
<firstname firstname="Andre6">Andre</firstname>
<lastname>Bruton</lastname>
</user>

应该是这样的:

<user>
<firstname>Tristan</firstname>
<lastname>Bruton</lastname>
</user>

知道如何解决这个问题吗?

【问题讨论】:

  • 一周前我放弃了 Classic ASP。我从 PHP 开始。最初是一项艰巨的任务,但现在好多了。 PHP 很容易安装,即使在 Windows 上也是如此,它很轻,可以做更多的事情。我很惊讶我没有更早地转向 PHP。我想旧习惯很难改变。

标签: xml vbscript asp-classic


【解决方案1】:

您应该使用 .Text 属性,而不是使用 .setAttribute 方法。

If firstname <> users_firstname Then
    Set nodeBook = xmlObj.selectSingleNode("//firstname")
    nodeBook.Text = users_firstname
    Response.Write nodeBook.Text
    xmlObj.save(cDir & cFile)
End If

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多