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
VPrakashVPrakash 

NEED HELP: Parsing XML from Sobject field and save the parsed data into a related object fields.

Hello,

 

 I need to parse XML string available in one of the Sobject field and save the parsed data(from different nodes) into corresponding related(Master-Detail) object fields.

_Prasu__Prasu_

You can try XML Dom Parser or the FAST XML Dom both are avilable in the code share tab.

VPrakashVPrakash

Hi Prasanna,

 

Thank you for the reply,

 

i used the DOM class  and I was able to parse the the first node and I am stuck on how to loop through the xml string for next nodes. Here is my class.Please go thru it and suggest  me the changesto be made

 

public with sharing class xmltosobject{
    public string str='';
    public void readxml(ID TestObjectID){
        TestObject__c TObj = [SELECT xml__c  FROM TestObject__c where id= : TestObjectID];
        str= TObj.XML__c;
        System.debug(str);        
        XMLdom xdom = new XMLDom(str);
        system.debug(xdom.getElementsByTagName('book')[0].nodeValue);
        book__c books=new book__c();
        books.TestObject__c=TestObjectID;
        books.Author__c= xdom.getElementsByTagName('author')[0].nodeValue;
        books.name=  xdom.getElementsByTagName('book')[0].nodeValue;
        insert books;
        }
}

 

The XML String is as follows:

 

<books><book author="Manoj">Foo bar</book> <author>Venu</author>
<book author="Mysti">Baz</book><author>Prakash</author></books>

 

I can able to save the first record. But I am not able to figure out how to loop through the XML string and save the next node.