You need to sign in to do that
Don't have an account?

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>
http://developer.force.com/cookbook/recipe/parsing-xml-using-the-apex-dom-parser
All Answers
Hi Ramesh,
Use Dom.Document and Dom.XmlNode - there is an example in the docs.
Cheers,
Pat
Follow the link I just posted to the example in the docs.
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!!
http://developer.force.com/cookbook/recipe/parsing-xml-using-the-apex-dom-parser
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');
}
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;"/>
<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;
}
}
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;"/>
<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;
}
}
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;"/>
<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;
}
}