473,396 Members | 1,914 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 13801
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Jake | last post by:
Hello, I have a file, (either .jpg, .doc, etc.) how can I insert this file into sql server and retrieve it, instead of storing it in the folder. Thanks in advance. Jake
3
by: davidfahy | last post by:
Hi all, Let say I have the following XML file: <Books> <book id = "1"> <title>ASP.NET</title> <isbn>121212</isbn> <price>32.65</price> </book>
6
by: Alvaro G. Vicario | last post by:
The code below inserts a <spantag around the image. It works fine in Firefox or Opera, but has a weird effect in Internet Explorer: browser keeps loading the image forever... Even though the...
0
by: jayanshsgh | last post by:
Hi We have an BizTalk interface which sends and recieve XML between two different Application. Generally after we reboot our servers during maintenance wind we used to get the error Latest...
10
by: electric_r | last post by:
Hallo, I have 2 questions: with php and mysql : 1) How can I insert a text file into a TEXT field of a table ? 2) How can I insert a microsoft word file into a BLOB field of a table ?
1
by: kundan0607 | last post by:
Hi all, How to insert csv file into clob column.
10
by: Aditya | last post by:
Hi All, I would like to know how it is possible to insert a node in a linked list without using a temp_pointer. If the element is the first element then there is no problem but if it is in...
10
by: dreamy | last post by:
Can i ask how to insert a file into mysql? what the different between basename, getElementsByTagName, DOMdocument and $_FILES? how to apply it? thz.
1
by: dougancil | last post by:
I have a web page written in asp.net that is basically an upload button that does a bulk insert into a sql table. When a user presses an upload button, a .txt file is uploaded to a directory on...
1
by: gm000 | last post by:
hi i m trying to insert .csv file in ms sqlserver but its not working my cvs file data formate below is "12.204.8.120","12.204.8.151","214698104","214698135","VI","Virgin Islands,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.