Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
573 views
in Technique[技术] by (71.8m points)

vbscript - Replacing xml Text

I have a xml file which contains the following tags:

<gmd:title>
    <gco:CharacterString>READER FOREVER LEADER</gco:CharacterString>
</gmd:title>

I have tried to replace the title:

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

But I have only this code:

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

How can I do that using vbScript?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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("..estdataxmlso15393560.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

output:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...