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

Parse XML in an Attachment File
I have an xml file which is being loaded into Salesforce as a document using the input file tag (VF Page), I am able to access the document and get the contents. I am assigning the contents to a DOM.Document object, but when I am trying to traverse down to child objects it is not giving the values and its always null.
This is the body of text file
<?xml version="1.0" encoding="UTF-8"?>
<Process>
<SalesProcess Name="Test Process">
<Phase Name="Test" Order="1" Duration="0">
<Goal Name="Test" Order="1" Confidence="0">
<Description>Test Goal</Description>
</Goal>
</Phase>
</SalesProcess>
</Process>
Method Parsing the document
public void loadAttachment(){
String paramvalue = EncodingUtil.base64Encode(attach.body);
Blob afterblob = EncodingUtil.base64Decode(paramvalue);
attachmentBody = afterblob.tostring();
Dom.Document document = new Dom.Document();
document.load(attachmentBody);
Dom.Xmlnode rootnode = doc.getrootElement();
for(Dom.XMLNode child : rootnode.getChildElements()) {
System.debug(child.getName()); // This value is blank.
}
}
When I debug I am getting the following string as root node
XMLNode[ELEMENT,Process,null,null,null,[XMLNode[ELEMENT,SalesProcess,null,[common.apex.api.dom.XmlNode$Attribute@6393be55, common.apex.api.dom.XmlNode$Attribute@78b4f185, common.apex.api.dom.XmlNode$Attribute@2f5c90ac],null,[XMLNode[ELEMENT,Phase,null,[common.apex.api.dom.XmlNode$Attribute@661e247e, common.apex.api.dom.XmlNode$Attribute@44ee0092, common.apex.api.dom.XmlNode$Attribute@65679498],null,[XMLNode[ELEMENT,Goal,null,[common.apex.api.dom.XmlNode$Attribute@734cbb2a, common.apex.api.dom.XmlNode$Attribute@3293fd89, common.apex.api.dom.XmlNode$Attribute@11ea4750],null,[XMLNode[ELEMENT,Description,null,null,null,[XMLNode[TEXT,null,null,null,null,null,Test Goal,]],null,]],null,]],null,]],null,]],null,]
I printed the document.tostring() and the string is matching the xml, but i am not able to access the document values and it gives me a weird string when try to access the root node, if i try to access root node child i get a blank value.
This is the body of text file
<?xml version="1.0" encoding="UTF-8"?>
<Process>
<SalesProcess Name="Test Process">
<Phase Name="Test" Order="1" Duration="0">
<Goal Name="Test" Order="1" Confidence="0">
<Description>Test Goal</Description>
</Goal>
</Phase>
</SalesProcess>
</Process>
Method Parsing the document
public void loadAttachment(){
String paramvalue = EncodingUtil.base64Encode(attach.body);
Blob afterblob = EncodingUtil.base64Decode(paramvalue);
attachmentBody = afterblob.tostring();
Dom.Document document = new Dom.Document();
document.load(attachmentBody);
Dom.Xmlnode rootnode = doc.getrootElement();
for(Dom.XMLNode child : rootnode.getChildElements()) {
System.debug(child.getName()); // This value is blank.
}
}
When I debug I am getting the following string as root node
XMLNode[ELEMENT,Process,null,null,null,[XMLNode[ELEMENT,SalesProcess,null,[common.apex.api.dom.XmlNode$Attribute@6393be55, common.apex.api.dom.XmlNode$Attribute@78b4f185, common.apex.api.dom.XmlNode$Attribute@2f5c90ac],null,[XMLNode[ELEMENT,Phase,null,[common.apex.api.dom.XmlNode$Attribute@661e247e, common.apex.api.dom.XmlNode$Attribute@44ee0092, common.apex.api.dom.XmlNode$Attribute@65679498],null,[XMLNode[ELEMENT,Goal,null,[common.apex.api.dom.XmlNode$Attribute@734cbb2a, common.apex.api.dom.XmlNode$Attribute@3293fd89, common.apex.api.dom.XmlNode$Attribute@11ea4750],null,[XMLNode[ELEMENT,Description,null,null,null,[XMLNode[TEXT,null,null,null,null,null,Test Goal,]],null,]],null,]],null,]],null,]],null,]
I printed the document.tostring() and the string is matching the xml, but i am not able to access the document values and it gives me a weird string when try to access the root node, if i try to access root node child i get a blank value.
Code sample in following links will hepl you:
http://developer.force.com/cookbook/recipe/parsing-xml-using-the-apex-dom-parser
http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_xml_dom.htm