We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

removing children via Microsoft.XMLDOM

BigMonkeyHead
on
Medium Priority
273 Views
Last Modified: 2012-08-14
I'm creating an XML doc with the Microsoft.XMLDOM object.  As I work my way thru the building doc, I create <level1Child> nodes (per the example below).  Then I create <sample> nodes, and finally <more> nodes.  If the data for the <more> nodes does not meet a certain criteria, I don't need to add the <more> node.  Due to the nature of my code, I've already added the <level1Child> node and the <sample> node.

The problem is, sometimes after working my way thru, I may have some <level1Child> nodes that do not have any <more> nodes underneath them - see the 2nd <level1Child> as an example.  I need a quick-n-dirty way to look at the document once I'm done building it, and examine it to see which <level1Child> nodes do not have any <more> nodes underneath them.  For each one that doesn't, I need to remove that node and any <sample> nodes.  

<root>
  <level1>
     <level1Child>
        <sample>
          <more>data 1</more>
          <more>data 2</more>
          <more>data 3</more>
        </sample>
        <sample>
          <more>data 4</more>
          <more>data 5</more>
        </sample>
        <sample>
          <more>data 6</more>
        </sample>
     </level1Child>
     <level1Child>
       <sample/>
     </level1Child>
     <level1Child>
         <sample>
          <more>data 7</more>
          <more>data 8</more>
        </sample>
     </level1Child>
  </level1>
</root>
Comment
Watch Question

Senior Systems and Integration Developer
CERTIFIED EXPERT
Commented:

   '// Assuming an XMLDOM object named oDom

    Set oNodes = oDom.selectNodes("//level1Child[count(sample/more)=0]")
    For Each node In oNodes
       node.parentNode.RemoveChild node
    Next


This might not be 100% cos i'm doing it from memory :o)

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

Commented:
Thanks - that's pretty much what I was looking for.  :)
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.