Sample code for 30+ languages & platforms
PowerBuilder

Removing / Deleting Child Nodes from XML

See more XML Examples

Demonstrates various methods for removing child nodes from an XML document.

The input XML, available at http://www.chilkatsoft.com/data/fruit.xml, is this:

<root>
    <fruit color="red">apple</fruit>
    <fruit color="green">pear</fruit>
    <veg color="orange">carrot</veg>
    <meat animal="cow">beef</meat>
    <xyz>
        <fruit color="blue">blueberry</fruit>
        <veg color="green">broccoli</veg>
    </xyz>
    <fruit color="purple">grape</fruit>
    <cheese color="yellow">cheddar</cheese>
</root>

Chilkat PowerBuilder Downloads

PowerBuilder
integer li_rc
long li_Success
oleobject loo_Xml
oleobject loo_Xyz

li_Success = 0

loo_Xml = create oleobject
li_rc = loo_Xml.ConnectToNewObject("Chilkat.Xml")
if li_rc < 0 then
    destroy loo_Xml
    MessageBox("Error","Connecting to COM object failed")
    return
end if

//  The sample input XML is available at http://www.chilkatsoft.com/data/fruit.xml
li_Success = loo_Xml.LoadXmlFile("fruit.xml")
if li_Success <> 1 then
    Write-Debug loo_Xml.LastErrorText
    destroy loo_Xml
    return
end if

//  The RemoveChild method removes (discards) all direct
//  children having the specified tag:
loo_Xml.RemoveChild("fruit")

//  Show the resulting XML:
Write-Debug "Result with all direct children having a tag equal to ~"fruit~" removed:"
Write-Debug loo_Xml.GetXml()

//  The XML with the "fruit" direct children removed is shown below:
//  Notice that the "fruit" node beneath "xyz" was not removed.
//  This correct because it was not a direct child of the calling node.

//  <root>
//      <veg color="orange">carrot</veg>
//      <meat animal="cow">beef</meat>
//      <xyz>
//          <fruit color="blue">blueberry</fruit>
//          <veg color="green">broccoli</veg>
//      </xyz>
//      <cheese color="yellow">cheddar</cheese>
//  </root>

//  --------------------------------------------------------------------------
//  Restore the original XML:
li_Success = loo_Xml.LoadXmlFile("fruit.xml")

//  The RemoveChildWithContent method removes the child
//  having the exact content specified, regardless of the tag.  
//  For example:
loo_Xml.RemoveChildWithContent("pear")

//  Show the resulting XML:
Write-Debug "Result with the node containing ~"pear~" removed:"
Write-Debug loo_Xml.GetXml()

//  --------------------------------------------------------------------------
//  Restore the original XML:
li_Success = loo_Xml.LoadXmlFile("fruit.xml")

//  The RemoveChildByIndex method removes the Nth direct
//  child.  Indexing begins at 0.  The "xyz" child is at index 4:
loo_Xml.RemoveChildByIndex(4)

//  Show the resulting XML:
//  Notice that the entire "xyz" subtree is removed.
Write-Debug "Result with the node at index 4 removed:"
Write-Debug loo_Xml.GetXml()

//  --------------------------------------------------------------------------
//  Restore the original XML:
li_Success = loo_Xml.LoadXmlFile("fruit.xml")

//  Navigate to the node with tag "xyz";
loo_Xyz = loo_Xml.FindChild("xyz")

//  Remove the "xyz" subtree making it it's own XML document
//  with the "xyz" node at the root:
loo_Xyz.RemoveFromTree()

//  Show both XML documents:
Write-Debug loo_Xyz.GetXml()
Write-Debug loo_Xml.GetXml()

//  Also, the TreeId property is an integer value assigned
//  to nodes in an XML document.  All nodes belonging to
//  the same XML document will have the same TreeId.
//  Notice that the "xyz" node now has a different TreeId:
Write-Debug "xyz TreeId = " + string(loo_Xyz.TreeId)
Write-Debug "xml TreeId = " + string(loo_Xml.TreeId)

destroy loo_Xyz


destroy loo_Xml