Link to home
Start Free TrialLog in
Avatar of mmonacel
mmonacel

asked on

replaceChild not working while looping through IXMLDOMNodeList??

Hi there,

using:
VB 6
MSXML 3.0 parser

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

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:

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

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)

Next

Thanks so much in advance.  High point values b/c of urgency!
-Mike
ASKER CERTIFIED SOLUTION
Avatar of b1xml2
b1xml2
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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

<Record>
<Segment name="Child"><Element/></Segment>
<Segment name="Parent2"/>
</Record>
Avatar of mmonacel
mmonacel

ASKER

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>
      <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>
            <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>
      </Segment>
      <Segment name="FILLER" offset="84" length="1517"/>
</Record>

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.  
That did the trick!  Thanks!