Trying to change an XML file automatically with VBScript to change defaults in OpenOffice

PRSEXPERT
PRSEXPERT used Ask the Experts™
on
I am trying to get this script to work, and I am now asking for assistance.

It will replace the value with value_a, but it will not tag correctly.

Please assist.

Thanks

I have attached the xml file and the vbscript is:


Dim XmlDoc, value_a, value_b, value_c
value_a = "<value>MS Office 97<"&"/value>"
value_c = value_a

Set XmlDoc = CreateObject("Msxml2.DOMDocument.4.0")
XmlDoc.async = False
If XmlDoc.load("writer.xml") Then
    XmlDoc.setProperty "SelectionLanguage", "XPath"
    XmlDoc.setProperty "SelectionNamespaces","xmlns:oor='" & XmlDoc.documentElement.namespaceURI & "'"
    Set value_b = XmlDoc.selectsinglenode("oor:data/oor:component-data[attribute::oor:name='Setup']/node[attribute::oor:name='Office']/node[attribute::oor:name='Factories']/node[attribute::oor:name='com.sun.star.text.TextDocument']/prop[attribute::oor:name='ooSetupFactoryDefaultFilter']")
        If Not value_b Is Nothing Then
            WScript.Echo "Found value " & value_b.text
            value_b.text = value_c
            strResult = XmlDoc.save("file2.xml")
        Else
            WScript.Echo "value element not found."
        End If
    Else
        WScript.Echo "Error parsing XML: " & XmlDoc.parseError.reason
End If
 writer.xml
file2.xml
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Kent DyerIT Security Analyst Senior

Commented:
Running the code from the start..

Microsoft (R) Windows Script Host Version 5.8
Copyright (C) Microsoft Corporation. All rights reserved.

Error parsing XML: System error: -2146697210.

Changed line 19 to:
If XmlDoc.load("C:\Users\Kent\Documents\AdminScriptEditor\writer.xml") Then

Open in new window


Changed line 26 to:
strResult = XmlDoc.save("C:\Users\Kent\Documents\AdminScriptEditor\file2.xml")

Open in new window


Now, when run we see:

Microsoft (R) Windows Script Host Version 5.8
Copyright (C) Microsoft Corporation. All rights reserved.

Found value writer8

HTH,

Kent

Author

Commented:
I actually am getting that already, but if you look in file2 where it made the modifications, you will see it messes up the formatting.

I had it working, it just will not leave the tags and only replace the value.

ie.

under the section Property that I pull:

prop[attribute::oor:name='ooSetupFactoryDefaultFilter']

<value>writer8</value>

which it outputs as 'writer8' when i query var value_b.text as you saw in my script.

it just cannot replace that value correctly no matter what I have done.

I have tried setting value_b.text ="MS Office 97" and the current value.

this is what I am hoping to achieve this value under the property that I sited above.

<value>MS Office 97</value>

Author

Commented:
Actually I took snips of the xml files before and after my script.

BEFORE:
<prop oor:name="ooSetupFactoryDefaultFilter">
  <value>writer8</value>
  </prop>
- <prop oor:name="ooSetupFactoryEmptyDocumentURL" oor:finalized="true">
  <value>private:factory/swriter</value>
  </prop>

AFTER:
  <prop oor:name="ooSetupFactoryDefaultFilter"><value>MS Office 97</value></prop>
- <prop oor:name="ooSetupFactoryEmptyDocumentURL" oor:finalized="true">
  <value>private:factory/swriter</value>
  </prop>
Dim fileName, node, New_Value

Call GetArguments(fileName, node, New_Value)

Call XMLSetValue(fileName, node, New_Value)

Sub GetArguments(ByRef fileName, ByRef node, ByRef New_Value)
  ' Define local variables.
  Dim args

  Set args = WScript.Arguments

  If (args.Length = 3) Then
    fileName = args(0)
    node = args(1)
    New_Value = args(2)
  Else
    Call WScript.Echo( _
      "The syntax of this command is:" & vbCrLf & _
      "" & vbCrLf & _
      "xmlwriter0.vbs [File Name], [Node Value], [New_Value]" & vbCrLf & _
      "Ex: xmlwriter0.vbs ""filename"" ""node"" ""New_Value""")
                       
    Call WScript.Quit()
  End If
End Sub

Function XMLSetValue(fileName, node, New_Value)
Dim XmlDoc, Old_Value
'New_Value = "MS Word 97"
'node = "oor:data/oor:component-data[attribute::oor:name='Setup']/node[attribute::oor:name='Office']/node[attribute::oor:name='Factories']/node[attribute::oor:name='com.sun.star.text.TextDocument']/prop[attribute::oor:name='ooSetupFactoryDefaultFilter']/value"
'filename = "writer.xcd"

Set XmlDoc = CreateObject("Msxml2.DOMDocument.4.0")
XmlDoc.async = False
If XmlDoc.load(filename) Then
    XmlDoc.setProperty "SelectionLanguage", "XPath"
    XmlDoc.setProperty "SelectionNamespaces","xmlns:oor='" & XmlDoc.documentElement.namespaceURI & "'"
    Set Old_Value = XmlDoc.selectsinglenode(node)
        If Not Old_Value Is Nothing Then
'            WScript.Echo "Found value " & Old_Value.text
            Old_Value.text = New_Value
            strResult = XmlDoc.save(filename)
        Else
            WScript.Echo "value element not found."
        End If
    Else
        WScript.Echo "Error parsing XML: " & XmlDoc.parseError.reason
End If
End Function

Author

Commented:
This was a pain to figure out, but this script will parse through the open office apps and change default associations.  I actually created an installer and am willing to share the knowledge with anyone that needs it.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial