[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now


replaceChild not working while looping through IXMLDOMNodeList??

Posted on 2005-04-18
Medium Priority
Last Modified: 2012-05-05
Hi there,

VB 6
MSXML 3.0 parser

XML structure:
  <Segment name='Parent'>
     <Segment name='Child'>
  <Segment name='Parent2'/>

I have a node list of "Parent" segments.  If they have a "Child" segment, I need to replace the entire current Parent node with it's child so the new record structure would look like:

  <Segment name='Child'>
  <Segment name='Parent2'/>

The code below (and all sorts variations therein has not been able to do this).  I just keep ending up with the same structure for oNode.  I tried all sorts of things with setting oChild to a complete new DOM doc, etc.  Please help!!!!

Dim oDoc As DOMDocument
Dim oNL  As IXMLDOMNodeList
Dim oNode   As IXMLDOMNode
Dim oChild  As IXMLDOMNode
Set oDoc = New DOMDocument
Call oDoc.loadXML("<Record><Segment name='Parent'><Segment name='Child'><Element/></Segment></Segment><Segment name='Parent2'/></Record>")
Set oNL = oDoc.selectNodes("Record/Segment")
For Each oNode In oNL

  Set oChild = oNode.firstChild.cloneNode(True)
  Call oNode.parentNode.replaceChild(oChild, oNode)


Thanks so much in advance.  High point values b/c of urgency!
Question by:mmonacel
  • 2
  • 2
LVL 23

Accepted Solution

b1xml2 earned 2000 total points
ID: 13808154
Dim oDoc As New MSXML2.DOMDocument40
Set oDoc.async = False
oDoc.loadXML("<Record><Segment name='Parent'><Segment name='Child'><Element/></Segment></Segment><Segment name='Parent2'/></Record>")
Dim oNode As IXMLDOMNode
Set oNode = oDoc.selectSingleNode("//Segment[Segment]")
Dim oClone As IXMLDOMNode
While Not oNode Is Nothing
      Set oClone = oNode.selectSingleNode("Segment").cloneNode(True)
      oNode.parentNode.replaceChild(oClone, oNode)
      Set oNode = oDoc.selectSingleNode("//Segment[Segment]")
LVL 23

Expert Comment

ID: 13808177
you should not get a reference to the node list and then attempt to change the contents of the node list ....

so we do it one node at a time,

the result is

<Segment name="Child"><Element/></Segment>
<Segment name="Parent2"/>

Author Comment

ID: 13809003
Hmmm... this is tough b/c what I'm doing here (not shown in my simple example above) is essentially parsing out a fixed length file.  The XML I have defines the different segments with offsets and length attributes.  So what I need to do is for each record in the RecordList, loop, then loop through each segment in the record, and if that has a segment, etc.... and then fill in all the particular element and segment values into the XML.  This is all well and good until I have a situation where I have what's called a redefine (multiple segments templates under a segment which redefine a particular parent).  Example below (CHANGE-INFO is one...):

<Record name="CHANGE-RECORD" offset="1" length="1600">
      <Segment name="CHANGE-RECORD-KEY" offset="1" length="25">
            <Element name="KEY-MAIL-ID" offset="1" length="2" value=""/>
            <Element name="KEY-RECORD-TYPE" offset="3" length="2" value=""/>
            <Element name="KEY-POLICY-NO" offset="5" length="8" value=""/>
            <Element name="KEY-STATE" offset="13" length="2" value=""/>
            <Element name="FILLER" offset="15" length="11" value=""/>
      <Segment name="CHANGE-INFO" offset="26" length="58" redefine="true">
            <Segment name="CHANGE-INFO-REDEF1" offset="26" length="58">
                  <Element name="CHANGE-DATE" offset="26" length="10" value=""/>
                  <Element name="CHANGE-DESCRIPTION" offset="36" length="22" value=""/>
            <Segment name="CHANGE-INFO-REDEF2" offset="26" length="58">
                  <Element name="CHANGE-DATE" offset="26" length="18" value=""/>
                  <Element name="CHANGE-DESCRIPTION" offset="44" length="40" value=""/>
      <Segment name="FILLER" offset="84" length="1517"/>

My only concern with using the node clone - replace approach while processing ALL nodes would be very inefficient (since the document itself will be fairly large).  I guess I could do it though AFTER everything is filled in and just do the replacements then... I'll give it a shot.  In the meantime, if anyone thinks of a better approach to doing this, let me know.  

Author Comment

ID: 13809260
That did the trick!  Thanks!  

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The Client Need Led Us to RSS I recently had an investment company ask me how they might notify their constituents about their newsworthy publications.  Probably you would think "Facebook" or "Twitter" but this is an interesting client.  Their cons…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question