Insert a new node into an XML file

T

Tony B

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
 
C

Chris Barber

For a single element then look at:
http://www.devguru.com/Technologies/xmldom/quickref/document_createElement.html
http://www.devguru.com/Technologies/xmldom/quickref/node_appendChild.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/xmldom/quickref/obj_documentFragment.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


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
 
B

Bob Barrows [MVP]

Tony said:
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
 
T

Tony B

Bob Barrows said:
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
 
C

Chris Barber

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.


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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top