HOW TO: Manipulate XML files using Columbus Script (VBScript)
  • KB ID: AA-00615
  • Created: 04/26/2012 11:49 AM
  • Updated: 10/24/2013 10:05 AM
  • Views: 7318
  • 0 Ratings

Summary

You want to add, change read or remove values from an existing XML file using Columbus script

More Information

You can either use the TEXTFILE command (which can make troubles under certain circumstances) or use VBSCRIPT within the Columbus script language.

The following samples illustrate how XML files can be modified using MSXML with VBSCRIPT within Columbus script:

Base XML FIle:

<?xml version="1.0" encoding="utf-8"?>
<MyXMLDocument>
	<MyFirstElement id="1" text="First">
	  <MyFirstSubElement>SubElement 1.1</MyFirstSubElement>
	</MyFirstElement>
	<MySecondElement id="2" text="Second">Element 2</MySecondElement>
</MyXMLDocument>


Columbus scripts:

Add Element:
Add an element "MySecondSubElement" into "MyFirstElement" and set the content to "SubElement 1.2"

BEGIN SCRIPT 'VBScript' 'AddElement' '/MyXMLDocument/MyFirstElement' 'MySecondSubElement' 'SubElement 1.2' '%_PkgCache%\MyXMLDocument.xml'
Sub AddElement (xmlPath, xmlNode, xmlValue, xmlFile)
  On Error Resume Next
  Set objXmlDoc = CreateObject("Microsoft.XMLDOM")
  objXmlDoc.Load(xmlFile)
  Set objXML = objXmlDoc.selectSingleNode(xmlPath)
  Set newNode = objXML.appendChild(objXmlDoc.createElement(xmlNode))
  newNode.Text = xmlValue
  objXmlDoc.Save(xmlFile)
End Sub
END SCRIPT

Result:

  <MyFirstElement id="1" text="First">
    <MyFirstSubElement>SubElement 1.1</MyFirstSubElement>
    <MySecondSubElement>SubElement 1.2</MySecondSubElement>
  </MyFirstElement>



Add Attribute:
Add an attribute "text" to "MyFirstSubElement" and set the content to "First Sub (New)"

BEGIN SCRIPT 'VBScript' 'AddAttribute' '/MyXMLDocument/MyFirstElement/MyFirstSubElement' 'text' 'First Sub (New)' '%_PkgCache%\MyXMLDocument.xml'
Sub AddElement (xmlPath, xmlAttrib, xmlValue, xmlFile)
  On Error Resume Next
  Set objXmlDoc = CreateObject("Microsoft.XMLDOM")
  objXmlDoc.Load(xmlFile)
  Set objXML = objXmlDoc.selectSingleNode(xmlPath)
  objXML.setAttribute xmlAttrib, xmlValue
  objXmlDoc.Save(xmlFile)
End Sub
END SCRIPT

Result:

<MyFirstElement id="1" text="First">
  <MyFirstSubElement text="First Sub (New)">SubElement 1.1</MyFirstSubElement>
</MyFirstElement>

Modify Value (Element):
Change the content of MyFirstElement to SubElement 1.1 (Changed)

BEGIN SCRIPT 'VBScript' 'ModifyValue' '/MyXMLDocument/MyFirstElement/MyFirstSubElement' 'SubElement 1.1 (Changed)' '%_PkgCache%\MyXMLDocument.xml'
Sub ModifyValue (xmlPath, xmlValue, xmlFile)
  On Error Resume Next
  Set objXmlDoc = CreateObject("Microsoft.XMLDOM")
  objXmlDoc.Load(xmlFile)
  Set objXML = objXmlDoc.selectSingleNode(xmlPath)
  objXML.Text = xmlValue
  objXmlDoc.Save(xmlFile)
End Sub
END SCRIPT

Result:

<MyFirstElement id="1" text="First">
  <MyFirstSubElement>SubElement 1.1 (Changed)</MyFirstSubElement>
</MyFirstElement>


Modify Value (Attribute):
Change the value of the "text" attribute of the element "MyFirstElement"

BEGIN SCRIPT 'VBScript' 'ModifyValue' '/MyXMLDocument/MyFirstElement/@text' 'First (Changed)' '%_PkgCache%\MyXMLDocument.xml'
Sub ModifyValue (xmlPath, xmlValue, xmlFile)
  On Error Resume Next
  Set objXmlDoc = CreateObject("Microsoft.XMLDOM")
  objXmlDoc.Load(xmlFile)
  Set objXML = objXmlDoc.selectSingleNode(xmlPath)
  objXML.Text = xmlValue
  objXmlDoc.Save(xmlFile)
End Sub
END SCRIPT

Result:

<MyFirstElement id="1" text="First (Changed)">
  <MyFirstSubElement>SubElement 1.1</MyFirstSubElement>
</MyFirstElement>

Read Value (Element):
Read the content of the element "MySecondElement" and use it in the Columbus script

BEGIN SCRIPT 'VBScript' 'ReadValue' '/MyXMLDocument/MySecondElement' '%_PkgCache%\MyXMLDocument.xml'
Function ReadValue (xmlPath, xmlFile)
  On Error Resume Next
  Set objXmlDoc = CreateObject("Microsoft.XMLDOM")
  objXmlDoc.Load(xmlFile)
  Set objXML = objXmlDoc.selectSingleNode(xmlPath)
  ReadValue = objXML.Text
End Function
END SCRIPT
Log 'The content of the XML tag "/MyXMLDocument/MySecondElement" is "%_ReturnValueScript%"'

brainware.log:
24.10.2013 08:56:26 - C7Service: The content of the XML tag "/MyXMLDocument/MySecondElement" is "Element 2"



Read Value (Attribute):
Read the content of the "text" attribute of the element  "MySecondElement" and use it in the Columbus script

BEGIN SCRIPT 'VBScript' 'ReadValue' '/MyXMLDocument/MySecondElement/@text' '%_PkgCache%\MyXMLDocument.xml'
Function ReadValue (xmlPath, xmlFile)
  On Error Resume Next
  Set objXmlDoc = CreateObject("Microsoft.XMLDOM")
  objXmlDoc.Load(xmlFile)
  Set objXML = objXmlDoc.selectSingleNode(xmlPath)
  ReadValue = objXML.Text
End Function
END SCRIPT
Log 'The content of the XML attribute "text" of the tag "/MyXMLDocument/MySecondElement" is "%_ReturnValueScript%"'

brainware.log:
24.10.2013 08:56:26 - C7Service: The content of the XML attribute "text" of the tag "/MyXMLDocument/MySecondElement" is "Second"



Remove Element:
Remove the element MySecondElement

BEGIN SCRIPT 'VBScript' 'RemoveElement' '/MyXMLDocument/MySecondElement' '%_PkgCache%\MyXMLDocument.xml'
Sub RemoveElement (xmlPath, xmlFile)
  On Error Resume Next
  Set objXmlDoc = CreateObject("Microsoft.XMLDOM")
  objXmlDoc.Load(xmlFile)
  Set objXML = objXmlDoc.selectSingleNode(xmlPath)
  objXML.parentNode.removeChild objXML
  objXmlDoc.Save(xmlFile)
End Sub
END SCRIPT

Result:

<MyXMLDocument>
  <MyFirstElement id="1" text="First">
    <MyFirstSubElement>SubElement 1.1</MyFirstSubElement>
  </MyFirstElement>
</MyXMLDocument>


Remove Attribute:
Remove the "id" attribute from the element MyFirstElement

BEGIN SCRIPT 'VBScript' 'RemoveAttribute' '/MyXMLDocument/MyFirstElement' 'id' '%_PkgCache%\MyXMLDocument.xml'
Sub RemoveAttribute (xmlPath, xmlAttr, xmlFile)
  On Error Resume Next
  Set objXmlDoc = CreateObject("Microsoft.XMLDOM")
  objXmlDoc.Load(xmlFile)
  Set objXML = objXmlDoc.selectSingleNode(xmlPath)
  objXML.removeAttribute xmlAttr
  objXmlDoc.Save(xmlFile)
End Sub
END SCRIPT

Result:

<MyFirstElement text="First">

Keywords

XML, TEXTFILE

Products

C6, C7

Feedback

Did you find this article helpful?

© 2000-2018 Brainware Consulting & Development AG     Impressum / Legal Notice  |  Datenschutz / Privacy Policy