473,569 Members | 2,577 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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>12 3456</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 13816
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
documentFragmen t 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
documentFragmen t 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 CreateDocFragme ntFromXMLString (ByVal pstrXMLString As
String) As IXMLDOMDocument Fragment

Dim pobjXML As DOMDocument40
Dim pobjDOMFragment As IXMLDOMDocument Fragment
Set pobjXML = New DOMDocument40
pobjXML.async = False
pobjXML.loadXML (pstrXMLString)
Set pobjDOMFragment = pobjXML.createD ocumentFragment
pobjDOMFragment .appendChild pobjXML.documen tElement.cloneN ode(True)
Set CreateDocFragme ntFromXMLString = 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>mys ubnodevalue</mysubnode>" & _
"</mynode>"
'Set pobjNode to be a node in your XML document!
pobjNode.append Child CreateDocFragme ntFromXMLString (pstrXML)

Hope this helps.

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

Public Sub NodeReplaceWith NewContent(ByRe f pobjNode As IXMLDOMNode, ByVal
pstrXML As String)

Dim pobjParentNode As IXMLDOMNode
Dim pobjNewNode As IXMLDOMDocument Fragment
Set pobjParentNode = pobjNode.parent Node
Set pobjNewNode = CreateDocFragme ntFromXMLString (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******** ******@TK2MSFTN GP12.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>12 3456</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>12 3456</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.c reateobject("ms xml2.domdocumen t")
xmldoc.load "filename.x ml"
set oItemsNode = xmldoc.selectsi nglenode("TRANS ACTION/ORDER/ITEMS")
set oNewItemNode=xm ldoc.createelem ent("ITEM")
oItemsNode.appe ndchild oNewItemNode
set oNode=xmldoc.cr eateelement("PU RCHASE_ID")
oNode.text = <whatever>
oNewItemNode.ap pendchild 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******@NOyah oo.SPAMcom> wrote in message
news:OL******** ******@tk2msftn gp13.phx.gbl...
dim xmldoc, oItemsNode, oNewItemNode, oNode
set xmldoc=server.c reateobject("ms xml2.domdocumen t")
xmldoc.load "filename.x ml"
set oItemsNode = xmldoc.selectsi nglenode("TRANS ACTION/ORDER/ITEMS")
set oNewItemNode=xm ldoc.createelem ent("ITEM")
oItemsNode.appe ndchild oNewItemNode
set oNode=xmldoc.cr eateelement("PU RCHASE_ID")
oNode.text = <whatever>
oNewItemNode.ap pendchild 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.selectS ingleNode("/TRANSACTION/ORDER/ITEMS")

'Create the new node to append.
pstrXML = "<ITEM>" & _
"<PURCHASE_ID>1 23456</PURCHASE_ID>" & _
"<SKU>6666</SKU>" & _
"<QUANTITY> 295</QUANTITY>" & _
"<UNITPRICE>0.0 1</UNITPRICE>" & _
"<SUB_TOTAL>2.9 5</SUB_TOTAL>" & _
"<DESCRIPTI ON></DESCRIPTION>" & _
"<SHIPPING/>" & _
"</ITEM>"
Set pobjNewNode = CreateDocFragme ntFromXMLString (pstrXML)

'Set pobjNode to be a node in your XML document!
Set pobjNewNode = pobjNode.append Child(pobjNewNo de)

'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 CreateDocFragme ntFromXMLString (pstrXMLString)

Dim pobjXML
Dim pobjDOMFragment
Set pobjXML = CreateObject("M sxml2.DOMDocume nt.4.0")
pobjXML.async = False
pobjXML.loadXML pstrXMLString
Set pobjDOMFragment = pobjXML.createD ocumentFragment
pobjDOMFragment .appendChild pobjXML.documen tElement.cloneN ode(True)
Set CreateDocFragme ntFromXMLString = pobjDOMFragment
Set pobjDOMFragment = Nothing
Set pobjXML = Nothing

End Function

Hope this helps.
"Tony B" <To**@me.nospam > wrote in message
news:uA******** ******@TK2MSFTN GP12.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>12 3456</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
1542
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
6869
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
2733
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 picture *is* displayed, status bar and throbber won't stop showing activity. Apart from that, scripts work even in IE. Is it an IE bug or is there...
0
2071
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 Error: The adapter failed to transmit message going to send port . It will be retransmitted after the retry interval specified for this Send Port....
10
6458
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
4895
by: kundan0607 | last post by:
Hi all, How to insert csv file into clob column.
10
10487
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 between then what is the best solution.
10
4544
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
2903
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 the web server, and also inserted into a sql server on my network. I can get the file to upload to the correct destination but when the server tries to...
1
2360
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, U.S." "12.204.8.152","12.204.8.159","214698136","214698143","PR","Puerto
0
7711
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7628
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7938
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
5519
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5245
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3679
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3667
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2128
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
970
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.