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
indyindy 

Parsing and updating xml using Apex.

Hi Friends,

 

My requirement is to create xml initially and append (update) the xml over a period of time.

 

When customer signed a contract, the contract details are appended to text area field in the form of below xml.

i.e 

<?xml version="1.0" encoding="UTF-8"?>

<Contracts>

     <Contract ContractID="Original Contract" StartDate="2011-03-07" EndDate="2012-12-31">

          <ContractRecord TYPE="Silver" AccessoriesIncluded="true" AccessoriesNotes="test notes..."></ContractRecord>

     </Contract>

</Contracts>

 

Once the contract gets expired (End Date reaches), customer will renew the contract then the xml format would be the original contract plus renew contract.

 

<?xml version="1.0" encoding="UTF-8"?>

     <Contracts>

              <Contract ContractID="Original Contract" StartDate="2011-03-07" EndDate="2012-12-31">

                    <ContractRecord TYPE="Silver" AccessoriesIncluded="true" AccessoriesNotes="test notes...">

                    </ContractRecord>

               </Contract>

             

              <Contract ContractID="Renew Contract" StartDate="2013-01-01" EndDate="2014-12-31">

                       <ContractRecord TYPE="Silver" AccessoriesIncluded="true" AccessoriesNotes="test notes...">

                       </ContractRecord>

               </Contract>

       </Contracts>

 

I can achieve the 'Original Contract' xml creation using xmlStreamWriter class. The concern is how to append

  the 'Renew Contract' to the original contract.

 

The two options I am aware of:

 

1.    Read each xml element of original contract and write to a new xml stream writer and append the                                                        

       current renew tag elements and close the parent element. (i.e. </Contracts>)

 

2.  Read each xml element of original contract and populate the data to a wrapper class, then create a new  xml stream  

     writer  and append the original contract elements from wrapper class and current contract renewal elements and

     close the parent element(</Contracts>).

 

As I am processing large data volume, I need a better (Optimize) way to append the renew contract details to original contract.

 

Your immediate response is highly appreciated.

 

Note:  Is there a way to read the end element (</Contracts>) and append the new contract details and close the end element.

 

Thanks,

Indy