0

I am writing APEX to generate DOM document. I want to generate <TOTALVOLUME units="m3">1.0</TOTALVOLUME>. Parent node for TOTALVOLUME is consignmentBody and I am trying Below code to generate the required.

consignmentBody.addChildElement('TOTALVOLUME', null, null).setAttribute('units', 'm3'); 
    consignmentBody.addChildElement('TOTALVOLUME', null, null).addTextNode('1.0');

I know that I am using 2 addchildelement, Kindly help me to get the required as I am not able to generate with only one childelement.

  • Want to generate <TOTALVOLUME units="m3">1.0</TOTALVOLUME> – Anoop Aug 6 '19 at 5:08
2

You can use setAttribute to create attribute on XML node. See documentation for XMLNode.

Example 1:

Dom.Document doc = new Dom.Document();
Dom.Xmlnode rootNode = doc.createRootElement('TestReport', null, null);

Dom.Xmlnode childNode = rootNode.addChildElement('detail', null, null);               
childNode.addTextNode('1.0') ;             
childNode.setAttribute('Amount','1000');  

String xmlString = doc.toXmlString();
System.debug('xmlString =' + xmlString);

Your code can be like this

Dom.Xmlnode childNode = rootNode.addChildElement('TOTALVOLUME', null, null);               
childNode.setAttribute('units','m3');
childNode.addTextNode('1.0') ;      
  • Thanks for your valuable time Dhanik. – Anoop Aug 6 '19 at 7:04

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.