0

I am actually updating a Legacy application in VB 6.0 and I need to add an element to a XML declared as IXMLDOMElement. My XML object's content is actually as follows;

<ChoiceLists>
 <Test Default="*">
  <Choice Value="1" Description="Hours"/>
  <Choice Value="2" Description="Days"/>
 </Test>
</ChoiceLists>

And now I have a query which is already returning me a XML formatted result (as string), as

<Test2 Default="*">
  <Choice Value="276" Description="#276"/>
  <Choice Value="177" Description="#177"/>
  <Choice Value="0000" Description="#0000"/>
  <Choice Value="176" Description="#176"/>
</Test2>

which I need to integrate in my XML, that is in the root node <ChoiceLists>.

Can anyone tell me how do I add this string into my XML please? I've been trying the different functions of the IXMLDOMElement object but in vain.

Thanks

Nadeem_MK
  • 7,533
  • 7
  • 50
  • 61
  • Once you have the two bits of XML in a `IXmlDomElement`, you just need to do `element.AppendChild(newChildElement)`. – Deanna Sep 16 '14 at 12:05
  • for this, `newChildElement` should be of type `IXMLDOMElement`, but mine is in string. I want it to fit in between the root node `` – Nadeem_MK Sep 16 '14 at 12:08
  • How did you load the other XML into a DOM object? It's the same method. – Deanna Sep 16 '14 at 12:11
  • Initially what was done, is that a new element is created 'createElement' and the attributes are being set. Now since I already have the fully-built xml (but in string format), how do I integrate it in the existing xmlDomElement – Nadeem_MK Sep 16 '14 at 12:14
  • It's normally better to create and pass it around as XML DOM elements rather than strings. – Deanna Sep 16 '14 at 12:26

1 Answers1

2

You can make use of the IXMLDOMNode.appendChild() method to add one element (and children) to another element. If you have a raw string that you need to convert, you can load that into a new DOMDocument using IXMLDOMDocument.loadXML()

Dim TargetDocument As IXMLDOMDocument
Dim TargetElement As IXMLDOMElement
Dim NewDocument As IXMLDOMDocument
Dim NewElement As IXMLDOMElement

'Load your target document here
Set TargetDocument = New DOMDocument
TargetDocument.Load "P:\iCatcher Console\XML\feedlist.xml"

'Get a reference to the element we want to append to (I'm assuming it's the document element)
Set TargetElement = TargetDocument.DocumentElement

'Create a new documents to parse the XML string
Set NewDocument = New DOMDocument
NewDocument.loadXML NewXMLString

'The root of this document will be the outer element in the string so get a reference to that
Set NewElement = NewDocument.DocumentElement

'Append the new element to the target's children
TargetElement.appendChild NewElement

The resulting XML will now be something like:

<ChoiceLists>
 <Test Default="*">
  <Choice Value="1" Description="Hours"/>
  <Choice Value="2" Description="Days"/>
 </Test>
 <Test2 Default="*">
  <Choice Value="276" Description="#276"/>
  <Choice Value="177" Description="#177"/>
  <Choice Value="0000" Description="#0000"/>
  <Choice Value="176" Description="#176"/>
 </Test2>
</ChoiceLists>
Deanna
  • 23,876
  • 7
  • 71
  • 156
  • Error on appending child `TargetElement.appendChild(NewElement)` - `Run-time error '5' - Invalid procedure call or argument` :( – Nadeem_MK Sep 16 '14 at 13:00
  • Sorry, there were a few typos. – Deanna Sep 16 '14 at 14:06
  • Yet another error: `Run-time error '438' Object doesn't support this property or method.` I guess it's because of the AppendChild, which accept an IXMLDomNode – Nadeem_MK Sep 16 '14 at 14:56
  • It works for me. Double check it's the `TargetElement` you passing it to, not `TargetDocument`. – Deanna Sep 16 '14 at 15:44
  • It does work using the code I gave. You'll need to show the line that's failing (and the type of the variables used) – Deanna Sep 16 '14 at 16:13
  • I did almost the same thing, whereby my TargetDocument is the existing XML of type `IXMLDOMElement`. I succeeded in converting my string to `IXMLDOMDocument` as in your proposed solution. Only for the last line `TargetElement.appendChild NewElement` to end with error. – Nadeem_MK Sep 17 '14 at 04:57
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/61392/discussion-between-deanna-and-nadeem-mk). – Deanna Sep 17 '14 at 08:24