469,304 Members | 2,018 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,304 developers. It's quick & easy.

Insert a new node into an XML file

Hi
I need to insert a new node into an exisiting XML file using VBScript. The
file has the following structure:
<TRANSACTION>
<ORDER>
<BILLING>...
</BILLING>
<ORDER_TOTALS>...
</ORDER_TOTALS>
<ITEMS>
<ITEM>
<PURCHASE_ID>123456</PURCHASE_ID>
<SKU>6666</SKU>
<QUANTITY>295</QUANTITY>
<UNITPRICE>0.01</UNITPRICE>
<SUB_TOTAL>2.95</SUB_TOTAL>
<DESCRIPTION></DESCRIPTION>
<SHIPPING/>
</ITEM>
</ITEMS>
</ORDER>
</TRANSACTION>

I need to add another item to the above XML, so it will go into
<TRANSACTION> <ORDER> <ITEMS>. Also it will always have at least one item
already. I'm really struggling with this problem so any help will be
appreciated.

Thanks
Tony

Jul 19 '05 #1
5 13404
For a single element then look at:
http://www.devguru.com/Technologies/...teElement.html
http://www.devguru.com/Technologies/...pendChild.html

For a complex element (eg. has children) then take a look at
documentFragment functionality to create an arbitrary child
node with content supplied from an XML string.
http://www.devguru.com/Technologies/...tFragment.html

The stuff below (although VB) should be easily convertible to VBScript.

<snippet from previous post>
I have a VB Class that has a function to create and return a
documentFragment from an XML string (you just use the
returned object as a node and use appendChild or insertBefore etc. to append
it to your XML document.

This is the code:

Public Function CreateDocFragmentFromXMLString(ByVal pstrXMLString As
String) As IXMLDOMDocumentFragment

Dim pobjXML As DOMDocument40
Dim pobjDOMFragment As IXMLDOMDocumentFragment
Set pobjXML = New DOMDocument40
pobjXML.async = False
pobjXML.loadXML (pstrXMLString)
Set pobjDOMFragment = pobjXML.createDocumentFragment
pobjDOMFragment.appendChild pobjXML.documentElement.cloneNode(True)
Set CreateDocFragmentFromXMLString = pobjDOMFragment
Set pobjDOMFragment = Nothing
Set pobjXML = Nothing

End Function

You should be able to code this as VBScript.

So you could do:

pstrXML = "<mynode someattribute=""attributevalue"">" & _
"<mysubnode>mysubnodevalue</mysubnode>" & _
"</mynode>"
'Set pobjNode to be a node in your XML document!
pobjNode.appendChild CreateDocFragmentFromXMLString(pstrXML)

Hope this helps.

Here's an example of replacing a node with new XML content:

Public Sub NodeReplaceWithNewContent(ByRef pobjNode As IXMLDOMNode, ByVal
pstrXML As String)

Dim pobjParentNode As IXMLDOMNode
Dim pobjNewNode As IXMLDOMDocumentFragment
Set pobjParentNode = pobjNode.parentNode
Set pobjNewNode = CreateDocFragmentFromXMLString(pstrXML)
'Insert the new and then remove the existing.
pobjParentNode.replaceChild pobjNewNode, pobjNode

End Function

</snippet>

Chris Barber
"Tony B" <To**@me.nospam> wrote in message
news:uA**************@TK2MSFTNGP12.phx.gbl...
Hi
I need to insert a new node into an exisiting XML file using VBScript. The
file has the following structure:
<TRANSACTION>
<ORDER>
<BILLING>...
</BILLING>
<ORDER_TOTALS>...
</ORDER_TOTALS>
<ITEMS>
<ITEM>
<PURCHASE_ID>123456</PURCHASE_ID>
<SKU>6666</SKU>
<QUANTITY>295</QUANTITY>
<UNITPRICE>0.01</UNITPRICE>
<SUB_TOTAL>2.95</SUB_TOTAL>
<DESCRIPTION></DESCRIPTION>
<SHIPPING/>
</ITEM>
</ITEMS>
</ORDER>
</TRANSACTION>

I need to add another item to the above XML, so it will go into
<TRANSACTION> <ORDER> <ITEMS>. Also it will always have at least one item
already. I'm really struggling with this problem so any help will be
appreciated.

Thanks
Tony


Jul 19 '05 #2
Tony B wrote:
Hi
I need to insert a new node into an exisiting XML file using
VBScript. The file has the following structure:
<TRANSACTION>
<ORDER>
<BILLING>...
</BILLING>
<ORDER_TOTALS>...
</ORDER_TOTALS>
<ITEMS>
<ITEM>
<PURCHASE_ID>123456</PURCHASE_ID>
<SKU>6666</SKU>
<QUANTITY>295</QUANTITY>
<UNITPRICE>0.01</UNITPRICE>
<SUB_TOTAL>2.95</SUB_TOTAL>
<DESCRIPTION></DESCRIPTION>
<SHIPPING/>
</ITEM>
</ITEMS>
</ORDER>
</TRANSACTION>

I need to add another item to the above XML, so it will go into
<TRANSACTION> <ORDER> <ITEMS>. Also it will always have at least one
item already. I'm really struggling with this problem so any help
will be appreciated.

Thanks
Tony


dim xmldoc, oItemsNode, oNewItemNode, oNode
set xmldoc=server.createobject("msxml2.domdocument")
xmldoc.load "filename.xml"
set oItemsNode = xmldoc.selectsinglenode("TRANSACTION/ORDER/ITEMS")
set oNewItemNode=xmldoc.createelement("ITEM")
oItemsNode.appendchild oNewItemNode
set oNode=xmldoc.createelement("PURCHASE_ID")
oNode.text = <whatever>
oNewItemNode.appendchild oNode
'repeat the previous 3 steps for the rest of the subnodes

HTH,
Bob Barrows
--
Microsoft MVP -- ASP/ASP.NET
Please reply to the newsgroup. The email account listed in my From
header is my spam trap, so I don't check it very often. You will get a
quicker response by posting to the newsgroup.
Jul 19 '05 #3

"Bob Barrows [MVP]" <re******@NOyahoo.SPAMcom> wrote in message
news:OL**************@tk2msftngp13.phx.gbl...
dim xmldoc, oItemsNode, oNewItemNode, oNode
set xmldoc=server.createobject("msxml2.domdocument")
xmldoc.load "filename.xml"
set oItemsNode = xmldoc.selectsinglenode("TRANSACTION/ORDER/ITEMS")
set oNewItemNode=xmldoc.createelement("ITEM")
oItemsNode.appendchild oNewItemNode
set oNode=xmldoc.createelement("PURCHASE_ID")
oNode.text = <whatever>
oNewItemNode.appendchild oNode
'repeat the previous 3 steps for the rest of the subnodes

HTH,
Bob Barrows


That does indeed help, thank you.

Regards
Tony
Jul 19 '05 #4
Thank you for all the information Chris, very helpful.

Regards
Tony
Jul 19 '05 #5
I felt a bit bad about not providing a concrete example so here goes
(apologies for any typos):

'Append new XML content to the end of the ITEMS section.
Dim pobjXML
Dim pobjNode
Dim pobjNewNode
Dim pstrXML

'Create the XML document and load the existing content.
pobjXML.async = False
pobjXML.load "xmldoc.xml" 'Your document wherever it may be.

'Get the existing orders now
Set pobjNode = pobjXML.selectSingleNode("/TRANSACTION/ORDER/ITEMS")

'Create the new node to append.
pstrXML = "<ITEM>" & _
"<PURCHASE_ID>123456</PURCHASE_ID>" & _
"<SKU>6666</SKU>" & _
"<QUANTITY>295</QUANTITY>" & _
"<UNITPRICE>0.01</UNITPRICE>" & _
"<SUB_TOTAL>2.95</SUB_TOTAL>" & _
"<DESCRIPTION></DESCRIPTION>" & _
"<SHIPPING/>" & _
"</ITEM>"
Set pobjNewNode = CreateDocFragmentFromXMLString(pstrXML)

'Set pobjNode to be a node in your XML document!
Set pobjNewNode = pobjNode.appendChild(pobjNewNode)

'Now pobjNode is a reference to the new node in the document.

'Save the document (if applicable)
pobjXML.save pobjXML.url

'Function to return a new node with deep XML content.
Public Function CreateDocFragmentFromXMLString(pstrXMLString)

Dim pobjXML
Dim pobjDOMFragment
Set pobjXML = CreateObject("Msxml2.DOMDocument.4.0")
pobjXML.async = False
pobjXML.loadXML pstrXMLString
Set pobjDOMFragment = pobjXML.createDocumentFragment
pobjDOMFragment.appendChild pobjXML.documentElement.cloneNode(True)
Set CreateDocFragmentFromXMLString = pobjDOMFragment
Set pobjDOMFragment = Nothing
Set pobjXML = Nothing

End Function

Hope this helps.
"Tony B" <To**@me.nospam> wrote in message
news:uA**************@TK2MSFTNGP12.phx.gbl...
Hi
I need to insert a new node into an exisiting XML file using VBScript. The
file has the following structure:
<TRANSACTION>
<ORDER>
<BILLING>...
</BILLING>
<ORDER_TOTALS>...
</ORDER_TOTALS>
<ITEMS>
<ITEM>
<PURCHASE_ID>123456</PURCHASE_ID>
<SKU>6666</SKU>
<QUANTITY>295</QUANTITY>
<UNITPRICE>0.01</UNITPRICE>
<SUB_TOTAL>2.95</SUB_TOTAL>
<DESCRIPTION></DESCRIPTION>
<SHIPPING/>
</ITEM>
</ITEMS>
</ORDER>
</TRANSACTION>

I need to add another item to the above XML, so it will go into
<TRANSACTION> <ORDER> <ITEMS>. Also it will always have at least one item
already. I'm really struggling with this problem so any help will be
appreciated.

Thanks
Tony


Jul 19 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by davidfahy | last post: by
6 posts views Thread by Alvaro G. Vicario | last post: by
reply views Thread by jayanshsgh | last post: by
10 posts views Thread by electric_r | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.