1

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?

John Saunders
  • 160,644
  • 26
  • 247
  • 397
HelpOverFlow
  • 11
  • 1
  • 4
  • Look here http://stackoverflow.com/questions/11726266/searching-xml-using-vbs-and-changing-a-value to see if using the SelectionNamespaces property properly solves your problem. – Ekkehard.Horner Mar 13 '13 at 18:48

1 Answers1

2

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

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>
Community
  • 1
  • 1
Ekkehard.Horner
  • 38,498
  • 2
  • 45
  • 96
  • I wish you'd make a canonical question and answer on this, instead of posting the same answer each time the question is asked. You could make the Q and A be at the MSXML level and other answers could translate to different languages and/or platforms (VBScript, for instance). See "[What is a NullReferenceException in .NET?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net)" for an example of a popular canonical Q&A - won me two gold badges, BTW. – John Saunders Mar 13 '13 at 22:02
  • Dear Ekkehard Horner, while running the code you wrote, the output was "/gmd:title/gco:CharacterString"not found. It seems that the code could not find the right path that leads to the title´s text. What do you say? Thanks for your time – HelpOverFlow Mar 14 '13 at 12:39
  • @HelpOverFlow - I say: The XML you posted a fragment of does not contain a root `gmd:title` - as my sample does and the XPath expression (mark the leading /) assumes. – Ekkehard.Horner Mar 14 '13 at 17:17