You need to sign in to do that
Don't have an account?
kdavis
XML Parsing Documentation
Hi,
Can anyone tell me where to find some quality documention on parsing XML with apex. I have no experience in parsing XML, so some detailed walkthrough's would be very helpful.
Thanks!
They include an entire code block on how to read XML files. If you get stuck, feel free to ask and we'll see what we can do to help you out.
All Answers
They include an entire code block on how to read XML files. If you get stuck, feel free to ask and we'll see what we can do to help you out.
Appreciate it!
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;
}
}
can u once tell me?
thanks in advance
@Shekar
You will need to upload the xml file .
like as test.xml file.
I used your code to parse the XML file. It is working fine.
Now, i would like to throw error message if the xml file is not chosen and the command button "Parse XML File" is clicked.
Could you please share the code for the same.
Thanks !!