2

I have an xml , and I tried to remove an entire child nodes using the xml DOM process. Even i use the remove child method , the result says true but if i print the dom it still holds that child element. Below is the XML structure

        <?xml version="1.0" encoding="UTF-8"?>
    <CustomResponse xmlns="http://p3.secure.asp/2006/04/data">
        <businessTypes>
            <fullName>Standard Process</fullName>
            <active>true</active>
            <classifiedvalues>
                <name>Business Area</name>
                <values>
                    <segName>ESK</segname>
                    <default>false</default>
                </values>
                <values>
                    <segname>TIS</segname>
                    <default>True</default>
                </values>
            </classifiedvalues>
 <classifiedvalues>
                <name>Business Source</name>
                <values>
                    <segName>Online</segname>
                    <default>false</default>
                </values>
                <values>
                    <segname>Retail</segname>
                    <default>True</default>
                </values>
            </classifiedvalues>
        </businessTypes>
    </CustomResponse>

I wanted to remove the entire classifiedvalues from the xml

Below is the apex code , I was trying

    FileData = the above xml

Dom.Document doc = new Dom.Document();
  doc.load(fileData.toString());
  Dom.XMLNode root = doc.getRootElement();  
Dom.XMLNode l1 = root.getChildElement('businessTypes', 'http://p3.secure.asp/2006/04/data');
Dom.XMLNode l2 = l1.getChildElement('classifiedvalues','http://p3.secure.asp/2006/04/data');
Boolean remove = root.removeChild(l2);
system.debug('#####'+remove);
system.debug('%%%%%'+l2);
system.debug('&&&&&'+doc.toxmlstring());

Any help How to remove that. I have seen some article but that is mostly with a specific attributes but not for the entire node.

Thank You !

2

This works:

Boolean remove = l2.getParent().removeChild(l2);

To remove all classifiedvalues nodes:

for (Dom.XmlNode child : l1.getChildren()) {
    if (child.getName() == 'classifiedvalues') {
        child.getParent().removeChild(child);
    }
}

I think this is not well documented, child needs to be removed from immediate parent.

  • Thanks for the information. I tried that but it actually removed only the first node. How do i remove all the child node with <classifiedvalues> in the entire xml. I tried with getchildelements under the for loop to iterate but it didn't work. Any help on that . Thanks – Quddus Feb 11 at 14:43
0

I tried with this below, hope this is the right way

for(Dom.XMLNode node1 :l1.getChildElements()){
Dom.XMLNode l2= l1.getChildElement('classifiedvalues','http://p3.secure.asp/2006/04/data'); 
If(l2 != null){
    Boolean remove = node1.getParent().removeChild(l2);
}
  • This seems like Murali's answer below - you might consider accepting that answer to show that it solved your issue, if that is the case. – David Reed Feb 11 at 15:18
  • Yes ofcourse. The second answer is basically to clear the loop xml tag – Quddus Feb 12 at 20:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.