replaceChild not working while looping through IXMLDOMNodeList??

Posted on 2005-04-18
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
    LVL 23

    Accepted Solution

    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

    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

    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

    That did the trick!  Thanks!  

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Threat Intelligence Starter Resources

    Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

    Suggested Solutions

    Title # Comments Views Activity
    XML and javascript 14 75
    Unmarshalling Xml using JAXB 1 37
    Fetch XML Unions? 3 249
    installing and using WTP plugin eclipse MARS 3 65
    Introduction In my previous article ( I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
    Browsing the questions asked to the Experts of this forum, you will be amazed to see how many times people are headaching about monster regular expressions (regex) to select that specific part of some HTML or XML file they want to extract. The examp…
    Need more eyes on your posted question? Go ahead and follow the quick steps in this video to learn how to Request Attention to your question. *Log into your Experts Exchange account *Find the question you want to Request Attention for *Go to the e…
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

    759 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

    Need Help in Real-Time?

    Connect with top rated Experts

    10 Experts available now in Live!

    Get 1:1 Help Now