Você está na página 1de 3

<!

--
<?xml version="1.0" ?>
<!-- May need to remove this header if parser is choking on <, >, & within scrip
t code. Less strict that way -->
-->
<!--
xmlEx.wsf <!-- change the file extension and run in Windows console: cscript xm
lEx.wsf -->
codecorvid, March 2011
Demonstrates creating an xml file and structure in VBScript, then reading and na
vigating the file
URL: http://www.tek-tips.com/viewthread.cfm?qid=1564301&page=23
URL: http://www.devguru.com/technologies/xmldom/quickref/obj_document.html
URL: http://www.devguru.com/technologies/xmldom/quickref/obj_namedNodeMap.html
-->
<job>
<!-- <script language="VBScript" src="FILENAME.vbs"/> -->
<script language="VBScript">
' For some reason declaring literal strings containing tags with
in the script tags gave
'WSH heartburn, despite the fact that such was implemented in nu
merous online examples
' See comment at top of this document
' ** Create and structure a new XML file, overwite if exists **
' Create XML file object using the DOM grammar
Dim xFile
Set xFile = CreateObject("MSXML2.DOMDocument.3.0") ' Create M$ x
ml DOM object
xFile.async = False
' This property determines whether asynchronous downloading of a
n XML file is permitted.
' The default is True, meaning that the load method returns cont
rol to the caller
'before the download is complete
' Create the root element
Set objRoot = xFile.createElement("Top") ' You must create a no
de with the document object
'methods before it can be put anywhere
xFile.appendChild objRoot ' Add the top element to the document
' Since the document itself is treated as a node in the DOM, all
nodes within the document
'are child nodes of the document
Set objAttr = xFile.createAttribute("Property") ' Create an attr
ibute to be assigned to a node
objAttr.Text = "TEST_VALUE" ' Give the created attribute a value
objRoot.attributes.setNamedItem objAttr ' Assign the attribute t
o the node
' Create a child element
Set objChild = xFile.createElement("Element")
objRoot.appendChild objChild
Set objAttr = xFile.createAttribute("Attribute")
objAttr.Text = "TEST_VALUE_2"
objChild.attributes.setNamedItem objAttr
' Create another child element, this one is a Text Node
Set objText = xFile.createTextNode("DATA") ' This text will appe
ar between the parent tags "Element"
objChild.appendChild objText ' Assign this text as a child of th
e node above
xFile.save "world.xml"
' Note this saved the file in the same directory as the script b
y default
' ** Now attempt to load the document that was created **
Set xRead = CreateObject("MSXML2.DOMDocument.3.0")
xRead.load "world.xml"
Function nodeType_lookup(typeNum)
' A function to identify M$ numbered node types with tex
t
' URL: http://msdn.microsoft.com/en-us/library/ms753745(
VS.85).aspx
Dim rtnType
Select Case typeNum
Case 1
rtnType = "NODE_ELEMENT"
Case 2
rtnType = "NODE_ATTRIBUTE"
Case 3
rtnType = "NODE_TEXT"
Case 4
rtnType = "NODE_CDATA_SECTION"
Case 5
rtnType = "NODE_ENTITY_REFERENCE"
Case 6
rtnType = "NODE_ENTITY"
Case 7
rtnType = "NODE_PROCESSING_INSTRUCTION"
Case 8
rtnType = "NODE_COMMENT"
Case 9
rtnType = "NODE_DOCUMENT"
Case 10
rtnType = "NODE_DOCUMENT_TYPE"
Case 11
rtnType = "NODE_DOCUMENT_FRAGMENT"
Case 12
rtnType = "NODE_NOTATION"
Case Else
Wscript.echo "nodeType_lookup: Invalid I
D # passed, " & typeNum
rtnType = "ERROR"
End Select
nodeType_lookup = rtnType
End Function
WScript.Echo xRead.nodeName ' Print the name of the document nod
e
WScript.Echo nodeType_lookup(xRead.nodeType) ' Retrieve and look
up node type
WScript.Echo
WScript.Echo xRead.documentElement.nodeName ' Print the name of
the root node
WScript.Echo nodeType_lookup(xRead.documentElement.nodeType)
WScript.Echo xRead.documentElement.attributes.length ' Print tot
al number of attributes
Dim attName
attName = xRead.documentElement.attributes.item(0).name ' Fetch
and store name of first attribute
WScript.Echo attName
WScript.Echo xRead.documentElement.attributes.getNamedItem(attNa
me).text ' Lookup by name, print text
WScript.Echo xRead.documentElement.attributes.item(0).text ' Pri
nt text of first attribute
WScript.Echo
WScript.Echo xRead.documentElement.firstChild.nodeName ' Print t
he name of the child node
WScript.Echo nodeType_lookup(xRead.documentElement.firstChild.no
deType)
WScript.Echo xRead.documentElement.firstChild.text
</script>
</job>

Você também pode gostar