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
sami amisami ami 

XML Parsing Urgent help!!!

Hi,

 

I am having a response in XML format.I am trying to get hold of particular fied value in my response XML and save in my field on salesforce object.

I am getting my body by response.getbody();

So I am trying to parse this XML file and get my ID value and fill my field..How can I do this??Any help??

 

 


<?XML version="1.0" encoding="UTF-8"?>
<case>
<id>00000000ABBl</id>
<subject>test</subject>
<Description>test</Description>
</case>
Best Answer chosen by Admin (Salesforce Developers) 

All Answers

Pat PattersonPat Patterson

Hi Ramesh,

 

Use Dom.Document and Dom.XmlNode - there is an example in the docs.

 

Cheers,

 

Pat

sami amisami ami
Yes i am using Dom.Document but i am not clear on XMLNode and thats were i am struck at.
Pat PattersonPat Patterson

Follow the link I just posted to the example in the docs.

sami amisami ami
Ok...I understood till here.
String xml = res.getBody();
Dom.Document doc = new Dom.Document();
doc.load(xml);
Dom.XMLNode root= doc.getRootElement();
Here i am getting my Root like this:
XMLNode[ELEMENT,case,null,null,null,[XMLNode[ELEMENT,id,null,null,null,[XMLNode[TEXT,null,null,null,null,null,00000000ABBl,]],null,]
could someone help me in understanding this? and how can i just take 00000000ABBl ?

Thank you!!
Amritesh SinghAmritesh Singh

Try this way to parse XML : very easy 

 

String strXml= res.getBody();

XmlDom doc = new XmlDom(strXml);
XmlDom.Element[] HeaderElements = doc.getElementsByTagName('Case');

if (HeaderElements != null)
{
for (XmlDom.Element HeaderElement : HeaderElements)
ReadXml(HeaderElement));
}

public static string ReadXml(XmlDom.Element HeaderElement)
{
string cID,subject,Descrp;

cID=HeaderElement.getValue('id');
subject=HeaderElement.getValue('subject');
Descrp =HeaderElement.getValue('Description');
}

sami amisami ami
yeah..Thank you :-)
Ramakrishnan AyyanarRamakrishnan Ayyanar
I created one simple XML Parsing.I have Used one VF page and Apex Class.It's parsing correctly ...

VF Page:

<apex:page Controller="Xmlparsar">

<apex:form >
        <apex:pageBlock >
            <apex:pageBlockButtons location="bottom">
                <apex:commandButton value="Parse Xml" action="{!Parsexml}" />  
                <apex:commandButton value="ParseXML File" action="{!Parsexmlfile}"/>
            </apex:pageBlockButtons>
            <apex:inputTextArea value="{!xmlstring}" style="width:336px;height:260px;"/> &nbsp;&nbsp;
            <apex:inputTextArea value="{!outxmlstring}" style="width:336px;height:260px;" id="response"/><br/>

            <apex:outputLabel value="Select Xml File" for="file"/>
            <apex:inputFile fileName="{!fileName}" value="{!body}"/>
        </apex:pageBlock>
    </apex:form>
  
</apex:page>

Apex Class:

public  class Xmlparsar
{
    //xml string
    public String xmlstring{get;set;}
  
    //display xml string
    public String outxmlstring{get;set;}
  
    //rootelement
    public String rootElement{get;set;}
  
    //
    public String filename{get;set;}
  
    public blob body{get;set;}
     
    //constructor
    public Xmlparsar()
    {
   
    }
  
   
//Parsing xml what you entered in the left text area
    public pagereference Parsexml()
    {
       DOM.Document xmlDOC = new DOM.Document();
       xmlDOC.load(xmlstring);
       DOM.XMLNode rootElement = xmlDOC.getRootElement();
       outxmlstring=String.valueof(xmlDOC.getRootElement().getName());
       for(DOM.XMLNode xmlnodeobj:xmlDOC.getRootElement().getChildElements())
       //.getChildren())
       {       
        
        
          loadChilds(xmlnodeobj);        
       }     
       return null;
    }
  
    //loading the child elements
    private void loadChilds(DOM.XMLNode xmlnode)
    {
        for(Dom.XMLNode child : xmlnode.getChildElements())
        {
          if(child.getText()!= null)
          {
          outxmlstring+='\n'+child.getName()+': '+child.getText();
      
          }
          loadChilds(child);      
        }
    }
  
  
//This is for parsing xml file what you selected
  public pagereference Parsexmlfile()
  {
       DOM.Document xmlDOC = new DOM.Document();
       xmlstring=body.tostring();        
       xmlDOC.load(xmlstring);
       DOM.XMLNode rootElement = xmlDOC.getRootElement();
       outxmlstring=String.valueof(xmlDOC.getRootElement().getName());//gives you root element Name
       for(DOM.XMLNode xmlnodeobj:xmlDOC.getRootElement().getChildElements())
       {       
                 
          loadChilds(xmlnodeobj);
       }     
      return null;
    }
}
Ramakrishnan AyyanarRamakrishnan Ayyanar
I created one simple XML Parsing.I have Used one VF page and Apex Class.It's parsing correctly ...

VF Page:

<apex:page Controller="Xmlparsar">

<apex:form >
        <apex:pageBlock >
            <apex:pageBlockButtons location="bottom">
                <apex:commandButton value="Parse Xml" action="{!Parsexml}" />  
                <apex:commandButton value="ParseXML File" action="{!Parsexmlfile}"/>
            </apex:pageBlockButtons>
            <apex:inputTextArea value="{!xmlstring}" style="width:336px;height:260px;"/> &nbsp;&nbsp;
            <apex:inputTextArea value="{!outxmlstring}" style="width:336px;height:260px;" id="response"/><br/>

            <apex:outputLabel value="Select Xml File" for="file"/>
            <apex:inputFile fileName="{!fileName}" value="{!body}"/>
        </apex:pageBlock>
    </apex:form>
  
</apex:page>

Apex Class:

public  class Xmlparsar
{
    //xml string
    public String xmlstring{get;set;}
  
    //display xml string
    public String outxmlstring{get;set;}
  
    //rootelement
    public String rootElement{get;set;}
  
    //
    public String filename{get;set;}
  
    public blob body{get;set;}
     
    //constructor
    public Xmlparsar()
    {
   
    }
  
   
//Parsing xml what you entered in the left text area
    public pagereference Parsexml()
    {
       DOM.Document xmlDOC = new DOM.Document();
       xmlDOC.load(xmlstring);
       DOM.XMLNode rootElement = xmlDOC.getRootElement();
       outxmlstring=String.valueof(xmlDOC.getRootElement().getName());
       for(DOM.XMLNode xmlnodeobj:xmlDOC.getRootElement().getChildElements())
       //.getChildren())
       {       
        
        
          loadChilds(xmlnodeobj);        
       }     
       return null;
    }
  
    //loading the child elements
    private void loadChilds(DOM.XMLNode xmlnode)
    {
        for(Dom.XMLNode child : xmlnode.getChildElements())
        {
          if(child.getText()!= null)
          {
          outxmlstring+='\n'+child.getName()+': '+child.getText();
      
          }
          loadChilds(child);      
        }
    }
  
  
//This is for parsing xml file what you selected
  public pagereference Parsexmlfile()
  {
       DOM.Document xmlDOC = new DOM.Document();
       xmlstring=body.tostring();        
       xmlDOC.load(xmlstring);
       DOM.XMLNode rootElement = xmlDOC.getRootElement();
       outxmlstring=String.valueof(xmlDOC.getRootElement().getName());//gives you root element Name
       for(DOM.XMLNode xmlnodeobj:xmlDOC.getRootElement().getChildElements())
       {       
                 
          loadChilds(xmlnodeobj);
       }     
      return null;
    }
}
Ramakrishnan AyyanarRamakrishnan Ayyanar
I created one simple XML Parsing.I have Used one VF page and Apex Class.It's parsing correctly ...

VF Page:

<apex:page Controller="Xmlparsar">

<apex:form >
        <apex:pageBlock >
            <apex:pageBlockButtons location="bottom">
                <apex:commandButton value="Parse Xml" action="{!Parsexml}" />  
                <apex:commandButton value="ParseXML File" action="{!Parsexmlfile}"/>
            </apex:pageBlockButtons>
            <apex:inputTextArea value="{!xmlstring}" style="width:336px;height:260px;"/> &nbsp;&nbsp;
            <apex:inputTextArea value="{!outxmlstring}" style="width:336px;height:260px;" id="response"/><br/>

            <apex:outputLabel value="Select Xml File" for="file"/>
            <apex:inputFile fileName="{!fileName}" value="{!body}"/>
        </apex:pageBlock>
    </apex:form>
  
</apex:page>

Apex Class:

public  class Xmlparsar
{
    //xml string
    public String xmlstring{get;set;}
  
    //display xml string
    public String outxmlstring{get;set;}
  
    //rootelement
    public String rootElement{get;set;}
  
    //
    public String filename{get;set;}
  
    public blob body{get;set;}
     
    //constructor
    public Xmlparsar()
    {
   
    }
  
   
//Parsing xml what you entered in the left text area
    public pagereference Parsexml()
    {
       DOM.Document xmlDOC = new DOM.Document();
       xmlDOC.load(xmlstring);
       DOM.XMLNode rootElement = xmlDOC.getRootElement();
       outxmlstring=String.valueof(xmlDOC.getRootElement().getName());
       for(DOM.XMLNode xmlnodeobj:xmlDOC.getRootElement().getChildElements())
       //.getChildren())
       {       
        
        
          loadChilds(xmlnodeobj);        
       }     
       return null;
    }
  
    //loading the child elements
    private void loadChilds(DOM.XMLNode xmlnode)
    {
        for(Dom.XMLNode child : xmlnode.getChildElements())
        {
          if(child.getText()!= null)
          {
          outxmlstring+='\n'+child.getName()+': '+child.getText();
      
          }
          loadChilds(child);      
        }
    }
  
  
//This is for parsing xml file what you selected
  public pagereference Parsexmlfile()
  {
       DOM.Document xmlDOC = new DOM.Document();
       xmlstring=body.tostring();        
       xmlDOC.load(xmlstring);
       DOM.XMLNode rootElement = xmlDOC.getRootElement();
       outxmlstring=String.valueof(xmlDOC.getRootElement().getName());//gives you root element Name
       for(DOM.XMLNode xmlnodeobj:xmlDOC.getRootElement().getChildElements())
       {       
                 
          loadChilds(xmlnodeobj);
       }     
      return null;
    }
}