Wednesday, September 29, 2010

VBScript - XML Object

Create an XML file
Inner text and Inner xml of XML file
Details of a single node

'Create an XML file
Set oXMLDoc = CreateObject("Microsoft.XMLDOM")
Set oRoot = oXMLDoc.createElement("Employees")
oXMLDoc.appendChild oRoot

Set oRecord = oXMLDoc.createElement("Employee")
oRoot.appendChild oRecord

Set oSSN = oXMLDoc.createElement("SSN")
oSSN.Text = "123456789"
oRecord.appendChild oSSN

Set oName = oXMLDoc.createElement("Name")
oName.Text = "Thanvi"
oRecord.appendChild oName

Set oDOB = oXMLDoc.createElement("DOB")
oDOB.Text = "2009-19-06"
oRecord.appendChild oDOB

Set oSalary = oXMLDoc.createElement("Salary")
oSalary.Text = 1000000
oRecord.appendChild oSalary

Set oIntro = oXMLDoc.createProcessingInstruction("xml","version='1.0'")
oXMLDoc.insertBefore oIntro,oXMLDoc.childNodes(0)
oXMLDoc.Save "C:\Employees.xml"


'Get the Inner text and Inner xml of the XML file
Set oXML = CreateObject("Microsoft.XMLDOM")
oXML.async = "false"
oXML.load("C:\Employees.xml")
msgbox "XML of the file "&oXML.xml
msgbox "Inner text of the xml file "&oXML.text

'Get the details of a single node
Set oXML = CreateObject("Microsoft.XMLDOM")
oXML.async = "false"
oXML.load("C:\Employees.xml")
Set MyNode = oXML.SelectSingleNode("/Employees/Employee/Name")
Msgbox "XML of the single Node "&MyNode.xml
Msgbox "Inner Text of the single Node "&MyNode.text
Msgbox "Type of the Node "&MyNode.nodeType

'Set the Attribute for the XML
Set oXMLDoc = CreateObject("Microsoft.XMLDOM")
Set oRoot = oXMLDoc.createElement("suite")
oRoot.setAttribute "name","suite"
oRoot.setAttribute "parallel","classes"
oRoot.setAttribute "thread-count","3"
oXMLDoc.appendChild oRoot
Set oRecord = oXMLDoc.createElement("test")
oRecord.setAttribute "name", "test"
oRoot.appendChild oRecord
Set oClasses = oXMLDoc.createElement("classes")
oRecord.appendChild oClasses
Set oName = oXMLDoc.createElement("class")
oName.Text = "Authorization"
oClasses.appendChild oName
Set oIntro = oXMLDoc.createProcessingInstruction("xml","version='1.0'")
oXMLDoc.insertBefore oIntro,oXMLDoc.childNodes(0)
oXMLDoc.Save "G:\TestNG.xml"

No comments:

Post a Comment