0

I need to copy one xml and need to generate another xml file. The purpose of creating another xml file is to have modified data. I'm using below code

 public static void parseXMl(String strResp){
    system.debug('Akshat XML COntent ' + strResp);
    Dom.Document doc = new Dom.Document();  
    DOM.Document docToGenerate = new DOM.Document();
    doc.load(strResp);  
    Dom.XMLNode rootElement = doc.getRootElement();  
    docToGenerate.createRootElement(rootElement.getName(), null, null);
    for ( Dom.XMLNode childElement : rootElement.getChildElements() ) {
        //root elements of each array
       
        docToGenerate.addChildElement(childElement.getName(), null, null);   
        for (Dom.XMLNode detailElement : childElement.getChildElements()){
            //childs elements of each array
            docToGenerate.addChildElement(childElement.getName(), null, null).addTextNode(detailElement.getText()); 
           
            for (Dom.XMLNode detailElement1 : detailElement.getChildElements()){
                
            docToGenerate.addChildElement(detailElement.getName(), null, null).addTextNode(detailElement1.getText()); 
               
            }
        } 
    }
}  

In docToGenerate I want new xml file. This code is wrong as its throwing me error for addChildElement and other methods I used on docToGenerate variable. I'm new to this, I'm able to parse existing xml but not sure how I can create new xml

Thanks in Advance

0

1 Answer 1

1

Voila! I achieved this requirement with below code

public static void parseXMl(String strResp){
    String rootElementToGenerate = '';
    Dom.Document doc = new Dom.Document();  
    DOM.Document docToGenerate = new DOM.Document();
    doc.load(strResp);  
    Dom.XMLNode rootElement = doc.getRootElement(); 
    rootElementToGenerate =  rootElement.getName();
    dom.XmlNode rootNodeElementToGenerate = docToGenerate.createRootElement(rootElement.getName(), null, null);
    for ( Dom.XMLNode childElement : rootElement.getChildElements() ) {
        String childElementToGenerate = childElement.getName();
        dom.XMLNode childNodeElementToGenerate = rootNodeElementToGenerate.addChildElement(childElementToGenerate, null, null);   
        for (Dom.XMLNode detailElement : childElement.getChildElements()){
            
            String childElementToGenerate1 = detailElement.getName();
            //childs elements of each array
            dom.XMLNode childNodeElementToGenerate1 = childNodeElementToGenerate.addChildElement(childElementToGenerate1, null, null).addTextNode(detailElement.getText()); 
            for (Dom.XMLNode detailElement1 : detailElement.getChildElements()){
            String childElementToGenerate2 = detailElement1.getName();
            dom.XMLNode childNodeElementToGenerate2 = childNodeElementToGenerate1.addChildElement(childElementToGenerate2, null, null).addTextNode(detailElement1.getText()); 
            //docToGenerate.addChildElement(detailElement.getName(), null, null).addTextNode(detailElement1.getText()); 
            }
        } 
    }
}

and docToGenerate.toXmlString() will give you newly generated xml.

Posting answer here in case if its helpful for anyone!

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .