Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Creating a node using vbScript

Status
Not open for further replies.

devildogv23

IS-IT--Management
Jun 26, 2006
2
US
I am trying to extend a simple example and can't get it to place the node in the correct place, nor can I get the delete node to work. Any help would be appreciated..

sample that works:
<customers>
<customer CustomerID="ALFKI" CompanyName="Alfreds Futterkiste" />
<customer CustomerID="ANATR" CompanyName="Ana Trujillo Emparedados y helados" />
<customer CustomerID="ANTON" CompanyName="Antonio Moreno Taqueria" />
</customers>

As soon as this sample is down another layer, I can't get the same results.

Sample XML file that doesn't work:

<XMI>
<customers>
<customer CustomerID="ALFKI" CompanyName="Alfreds Futterkiste" />
<customer CustomerID="ANATR" CompanyName="Ana Trujillo Emparedados y helados" />
<customer CustomerID="ANTON" CompanyName="Antonio Moreno Taqueria" />
</customers>
</XMI>



Code:

<html>
<head>
<link rel="stylesheet" type="text/css" href="Customers.css" >
</head>

<body>

<center>

<div class="pagetitle ">Original XML Values</div><br>
<textarea cols=110 id=txtOrig name=txtOrig rows=10></textarea>
<br><br>

<div class="pagetitle ">New XML Values</div><br>
<textarea cols=111 id="txtNew" name=txtNew rows=11></textarea>
</center>

<input type="button" value="Delete" onclick="doDelete()" id="btnDelete" name="btnDelete"></input>
<input type="button" value="Update" onclick="doUpdate()" ID="btnUpdate" NAME="btnUpdate"></input>
<input type="button" value="Insert" onclick="doInsert()" ID="Button1" NAME" NAME="btnInsert"></input>

</body>
</html>

<script language="VBscript">


function doDelete()

dim objNode, objDOM, strXPath

'Create an XML DOM object from the XML file
Set objDOM = CreateObject("Microsoft.XMLDOM")
objDOM.async = false
objDOM.load("Customers.xml")


'Display the original XML values
txtOrig.value = objDOM.xml


' Create an instance of the node that you with do delete
' XPath is used to specify the location of the node. This demo interprets to
' selecting the node where the customerID attribute = "ANTON"
strXPath = "XMI/customers/customer[@CustomerID='ANTON']"
set objNode = objDOM.selectSingleNode(strXPath)

'Now, remove the selected node from the XML DOM object
objDOM.documentElement.removeChild(objNode)

'Display the new contents
txtNew.value = objDOM.xml

end function


function doUpdate()

dim objNode, objDOM, strXPath

'Create an XML DOM object from the XML file
Set objDOM = CreateObject("Microsoft.XMLDOM")
objDOM.async = false
objDOM.load("Customers.xml")


'Display the original XML values
txtOrig.value = objDOM.xml


'Below are some examples of how to update the contents of an XML Element
' Create an instance of the node that you with do delete
' XPath is used to specify the location of the node. This demo interprets to
' selecting the node where the customerID attribute = "ANTON"
strXPath = "XMI/customers/customer[@CustomerID='ANTON']/@CompanyName"
objDOM.selectSingleNode(strXPath).text = "new value 1"

objDOM.selectSingleNode( "XMI/customers/customer[@CustomerID='ANATR']/@CompanyName").text = "new value 2"
objDOM.selectSingleNode( "XMI/customers/customer[0]/@CompanyName").text = "new value 3"


'Display the new contents
txtNew.value = objDOM.xml

end function


function doInsert()

dim objNode, objDOM

'Create an XML DOM object from the XML file
Set objDOM = CreateObject("Microsoft.XMLDOM")
objDOM.async = false
objDOM.load("Customers.xml")

'Display the original XML values
txtOrig.value = objDOM.xml

Set objRoot = objDom.documentElement.selectSingleNode("XMI/customers")

'Create a new Node object
Set objNode = objRoot.createNode(1,"customer","")

'Add the objects to the newly created Node
AddNodeAttribute objDOM, objNode, "CustomerID", "XMLP"
AddNodeAttribute objDOM, objNode, "CompanyName", "XMLPitstop.com"



'Display the new contents
txtNew.value = objDOM.xml

end function

Sub AddDomElement(objDom, strName, strValue)

Dim objNode

'Add new node
Set objNode = objDom.createElement(strName)
objNode.Text = strValue
objDom.documentElement.appendChild objNode
End Sub

Sub AddNodeAttribute(objDom, objNode, strName, strValue)

Dim objAttrib

Set objAttrib = objDOM.createAttribute(strName)
objAttrib.Text =strValue
objNode.Attributes.setNamedItem objAttrib
objDOM.documentElement.appendChild objNode
End Sub
</script>



 
This is the correct doInsert Function that works on the 1st file, but not the second.

function doInsert()

dim objNode, objDOM

'Create an XML DOM object from the XML file
Set objDOM = CreateObject("Microsoft.XMLDOM")
objDOM.async = false
objDOM.load("Customers.xml")

'Display the original XML values
txtOrig.value = objDOM.xml

'Create a new Node object
Set objNode = objDOM.createElement("customer")

'Add the objects to the newly created Node
AddNodeAttribute objDOM, objNode, "CustomerID", "XMLP"
AddNodeAttribute objDOM, objNode, "CompanyName", "XMLPitstop.com"


'Display the new contents
txtNew.value = objDOM.xml

end function
 
I only take a look in doInsert function.
[tt]
function doInsert()

dim objNode, objDOM

'Create an XML DOM object from the XML file
Set objDOM = CreateObject("Microsoft.XMLDOM")
objDOM.async = false
objDOM.load("Customers.xml")

'Display the original XML values
txtOrig.value = objDOM.xml

'[green]must lead by slash here[/green]
Set objRoot = objDom.documentElement.selectSingleNode("[red]/[/red]XMI/customers")

'Create a new Node object
[red]'[/red]Set objNode = objRoot.createNode(1,"customer","")
[blue]set objNode=objDOM.createElement("customer")[/blue]

'Add the objects to the newly created Node
AddNodeAttribute objDOM, objNode, "CustomerID", "XMLP"
AddNodeAttribute objDOM, objNode, "CompanyName", "XMLPitstop.com"

[green]'insert it[/green]
[blue]objRoot.appendChild objNode[/blue]

'Display the new contents
txtNew.value = objDOM.xml

end function
[/tt]
This is the quick fix of doInsert(). I would guess the other functions can be amended similarly. I have not looked into it.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top