function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Anu Raj.ax1269Anu Raj.ax1269 

Writing into XML file from Apex code

Hi,

 

           I have created a xml file and uploaded into document. I am reading the xml file from my apex code. I am able to add more nodes into the xml file by using addressNode.addChildElement and also tried writer.writeStartElement() also to get the xml file Update. But i am able to see the list get update which i am using to view the node values of xml, when i used addressNode.addChildElement(). But the xml file is not getting update. Does any one do have any idea to update xml file from apex code.

MY code

public class readXmldemo{

    public Document doc {get;set;}
    public XmlStreamReader reader {get; set;}
    public String xmlbody {get;set;}
    public readXmldemo(){

    doc = [select id, Name, body, url from Document WHERE Name = 'XMLFileDemo'];
    system.debug('Document after SOQL ' + doc);

    //system.debug('Get the elemets form XML file ' + reader.getEventType());
    xmlbody = (doc.body).tostring();
    system.debug('XML body contaion '+ xmlbody  );

    Dom.Document dom_doc = new Dom.Document();
    dom_doc.load(xmlbody);
    system.debug('XML body in Dom Doc ' + dom_doc );

    //Retrieve the root element for this document.
    Dom.XMLNode SalesforceObjecet = dom_doc.getRootElement(); 
    Dom.XMLNode ObjectNameSF = SalesforceObjecet.getChildElements()[0];

    string fieldName = ''; 
    string objecetName = '' ;   

    // This is used for insert into XML file but list is only getting 
    ObjectNameSF.addChildElement('field6', null, null).addTextNode('AccountField_FromApex');
    ObjectNameSF.addChildElement('csv6', null, null).addTextNode('csvField_FromApex'); 

    // This is used for writing into XML file but list is only getting 
    XmlStreamWriter writer = new XmlStreamWriter();    
    writer.writeStartDocument('utf-8', '1.0');        

    writer.writeStartElement(null, 'object', null); 
    writer.writeStartElement(null, 'field6', null);
    writer.writeCharacters('AccountField_FromApex');
    writer.writeEndElement();   

    writer.writeStartElement(null, 'csv6', null);
    writer.writeCharacters('csvField_FromApex');
    writer.writeEndElement();  

    writer.writeEndElement();    
    system.debug('Written xml file ' +writer.getXmlString());    
    writer.close(); 

    for(Dom.XMLNode child : SalesforceObjecet.getChildElements()) {           
            objecetName = child.getText();
            system.debug('Object Name in XML field  ' + objecetName );

            for(Dom.XMLNode subchild : child.getChildElements()) {
                fieldName = subchild.getText();
                system.debug('Filed Name in XML field  ' + fieldName );
            }
    } 



    system.debug('Doc to xml string ' + doc.toXmlString());


}
}

 please help if anybody do have any idea.

 

Thanks 

Anu