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
Dinesh KhandelwalDinesh Khandelwal 

Building a XML based on Schema

Hi all, I want to generate a XML based on Schema, which is available as a string input.

Sample schema is as below but it will not be fixed always,
<?xml version="1.0" standalone="yes"?>
<xs:schema id="NewDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
  <xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:MainDataTable="INPUT_MAIN" msdata:UseCurrentLocale="true">
    <xs:complexType>
      <xs:choice minOccurs="0" maxOccurs="unbounded">
        <xs:element name="INPUT_MAIN">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="PROCESS_ID" type="xs:int" />
              <xs:element name="ORDER_ID" type="xs:int" />
              <xs:element name="ORDER_NO" type="xs:string" minOccurs="0" />
              <xs:element name="ORDER_DESCRIPTION" type="xs:string" minOccurs="0" />
              <xs:element name="ORDER_SHORT_DESCRIPTION" type="xs:string" minOccurs="0" />
              <xs:element name="REMARKS" type="xs:string" minOccurs="0" />
              <xs:element name="START_DATE" type="xs:string" minOccurs="0" />
              <xs:element name="END_DATE" type="xs:string" minOccurs="0" />
              <xs:element name="CUSTOMER" type="xs:string" minOccurs="0" />
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:choice>
    </xs:complexType>
    <xs:unique name="Constraint1" msdata:PrimaryKey="true">
      <xs:selector xpath=".//INPUT_MAIN" />
      <xs:field xpath="PROCESS_ID" />
    </xs:unique>
  </xs:element>
</xs:schema>

How can i generate the XML which should look like below
<?xml version="1.0" encoding="UTF-8"?>
   <NewDataSet>
         <INPUT_MAIN>
               <PROCESS_ID>9</PROCESS_ID>
               <ORDER_ID>9</ORDER_ID>
               <ORDER_NO>test string</ORDER_NO>
               <ORDER_DESCRIPTION>test string</ORDER_DESCRIPTION>
               <ORDER_SHORT_DESCRIPTION>test string</ORDER_SHORT_DESCRIPTION>
               <REMARKS>test string</REMARKS>
               <START_DATE>test string</START_DATE>
               <END_DATE>test string</END_DATE>
               <CUSTOMER>test string</CUSTOMER>
         </INPUT_MAIN>
   </NewDataSet>

and the values will be fetched from order custom object.

The main problem is to build the XML.. any idea how can we achieve this.

Thanks
dinesh
Boris BachovskiBoris Bachovski
You should use the 'XMLStreamWriter' class as documented here (https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_classes_xml_XmlStream_writer.htm)

Also there is a similar question posted on this forum, check out the answer (https://developer.salesforce.com/forums?id=906F00000008ybEIAQ) which uses the above class.
Dinesh KhandelwalDinesh Khandelwal
Boris, Thanks a lot for your reply, I know for XMl operation we can use XMLStreamWriter or DOM. But my question is not that, my basic problem is to generate the XML on fly based on Schema.. this schema will not be fixed always. As an example there are 9 order fields in schema, it can be any number 4 to 40.. and I have to read the schema and build XML accordingly.

So, is there any way to generate an XML based on schema as we have in Java.
Boris BachovskiBoris Bachovski
I think you might need to bulild that manually using a String. As far as I know there is no automation around this case.