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