替换xml文本

前端之家收集整理的这篇文章主要介绍了替换xml文本前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个包含以下标记的xml文件
<gmd:title>
    <gco:CharacterString>READER FOREVER LEADER</gco:CharacterString>
</gmd:title>

我试图替换标题

<gmd:title>
    <gco:CharacterString>CHANGE TITLE</gco:CharacterString>
</gmd:title>

但我只有这个代码

Set objXMLDoc = CreateObject("Microsoft.XMLDOM")
objXMLDoc.async = False
objXMLDoc.load("albums.xml")
Set Elem = objXMLDoc.documentElement.selectSingleNode("gco:CharacterString") 
 MsgBox(Elem.text)

我怎么能用vbScript做到这一点?

the first hit for the obvious google search获得了一些帮助,我从 here修改了我的答案,得到:
Dim oFS    : Set oFS  = CreateObject("Scripting.FileSystemObject")
  Dim sFSpec : sFSpec   = goFS.GetAbsolutePathName("..\testdata\xml\so15393560.xml")
  Dim sNS    : sNS      = "xmlns:gmd='urn:x:y:z:1'  xmlns:gco='urn:x:y:z:1'"
  Dim oXML   : Set oXML = CreateObject("Msxml2.DOMDocument")
  oXML.setProperty "SelectionLanguage","XPath"
  oXML.setProperty "SelectionNamespaces",sNS
  oXML.async = False
  oXML.load sFSpec
  If 0 = oXML.parseError Then
     WScript.Echo oXML.xml
     WScript.Echo "-----------------"
     Dim sXPath : sXPath    = "/gmd:title/gco:CharacterString"
     Dim ndFnd  : Set ndFnd = oXML.selectSingleNode(sXPath)
     If ndFnd Is Nothing Then
        WScript.Echo sXPath,"not found"
     Else
        WScript.Echo ndFnd.text
        WScript.Echo "-----------------"
        ndFnd.text = "Abracadabra"
        WScript.Echo oXML.xml
     End If
  Else
     WScript.Echo oXML.parseError.reason
  End If

输出

<gmd:title xmlns:gmd="urn:x:y:z:1" xmlns:gco="urn:x:y:z:1">
        <gco:CharacterString>READER FOREVER LEADER</gco:CharacterString>
</gmd:title>

-----------------
READER FOREVER LEADER
-----------------
<gmd:title xmlns:gmd="urn:x:y:z:1" xmlns:gco="urn:x:y:z:1">
        <gco:CharacterString>Abracadabra</gco:CharacterString>
</gmd:title>
原文链接:https://www.f2er.com/xml/293082.html

猜你在找的XML相关文章