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
ashok  45ashok 45 

Remove Null Values from Tags of XML data

Hi All,

this is my sample response. here i want to capture <Allows> start tag to </Allows> end tag into record . In my InsertrecordMethod
am able to pass but it is inserting null values like this
    XMLNode[ELEMENT,Allows,null,null,null,[XMLNode[ELEMENT,ALLOW,null,null,null,[XMLNode[ELEMENT,ExtensionData,null,null,null,null,null,], ....... 

    here I dont want to allow null values. how can we avoid null values?? Can any one help me in this.

 /* String xml= ' <?xml version="1.0" encoding="utf-16"?>
<GETmethod xmlns:xsi="" xmlns:xsd="">
  <Extend />
      <SampleData />
          <BID />
          <State />
          <BID />
          <State />
</GetMethod>'; */

 String xml= '<?xml version="1.0" encoding="utf-8"?> <GetMethod xmlns:xsi="" xmlns:xsd=""> <Extend /> <Successful>true</Successful> <CustAllowed> <CustomerAllow> <SampleData /> <CurrentDate>10/14/2015</CurrentDate> <DocID>296</DocID> <DType>BP</DType> <Allows> <ALLOW> <BID /> <sampleDesc>exist1</sampleDesc> <sampleID>123456</sampleID> <DocID>111</DocID> <State /> </ALLOW> <ALLOW> <BID /> <sampleDesc>exist1</sampleDesc> <sampleID>123456</sampleID> <DocID>222</DocID> <State /> </ALLOW> <ALLOW> <BID /> <sampleDesc>exist1</sampleDesc> <sampleID>123454</sampleID> <DocID>333</DocID> <State /> </ALLOW> </Allows> </CustomerAllow> </CustAllowed> </GetMethod>';
       Dom.Document doc = new Dom.Document();
        //Retrieve the root element for this document.
        Dom.XMLNode root = doc.getRootElement();
        String strSuccessful= root.getChildElement('Successful', null).getText();
        Dom.XMLNode ycharge=root.getChildElement('CustAllowed',null).getChildElement('CustomerAllow',null);
        String strActivityDatey = ycharge.getChildElement('CurrentDate', null).getText();
        String strCIDy = ycharge.getChildElement('DocID',null).getText();
        String strCIDTypey = ycharge.getChildElement('DType',null).getText();
        Dom.XMLNode ycharge1=ycharge.getChildElement('Allows',null);
        // Here I am passing ycharge1 into InsertrecordMethod
        //Here is the my method
         Public static void InsertrecordMethod(Dom.XMLNode ycharge1)
          WebLog__c w=new WebLog__c();
          Insert w; // Here am able to insert response into my custom object but problem is it is inserting null values. i dont want to insert null values in tags
Andy BoettcherAndy Boettcher
Working with incoming XML in Salesforce is pretty tricky.  Honestly either I'd focus my efforts on cleaning up your source system (remember, GARBAGE IN, GARBAGE OUT!) or worst case treat the incoming XML as a string and strip out any "null," strings in there.