替换 xml 文本 [英] Replacing xml Text

查看:35
本文介绍了替换 xml 文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含以下标签的 xml 文件:

I have a xml file which contains the following tags:

<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 做到这一点?

How can I do that using vbScript?

推荐答案

第一次成功的帮助下谷歌搜索,我从这里修改了我的答案:

With more than a little help from the first hit for the obvious google search, I modified my answer from here to get:

  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>

这篇关于替换 xml 文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆