Input XML
<HistoryList Count="316" Time="2012-04-27T14:54Z 564ms" UserID="demo">
<History Description="CORN May 2012" Market="3" MarketName="CBOT" RequestedSymbol="@C`##" Status="0" TickerSymbol="@CK12">
<Day Close="623.25" Date="2012-04-16" High="629.0" Low="621.0" OI="277170" Open="626.75" Volume="125988"/>
</History>
</HistoryList>
Apex Parser
List<CustonObject> objCustom= new List<CustonObject>();
Dom.Document doc = res.getBodyDocument();
//Retrieve the root element for this document.
Dom.XMLNode InputNode= doc.getRootElement();
for(Dom.XMLNode child : InputNode.getChildElements())
{
for (dom.XmlNode awr : child.getChildElements() )
{
objCustom.add(loadDataToCustomObject(awr ));
}
}
}
}//InnerMost For Loop
} // Outermost for For Loop
//Inserting data to custom object
insert objCustom;
public static CustonObject loadDataToCustomObject(DOM.XMLNode xmlnode)
{
CustonObject dailyHistory = new CustonObject();
try
{
for(Dom.XMLNode child : xmlnode.getChildElements())
{
if(xmlnode.getAttributeCount() > 0)
{
if(String.isNotBlank(xmlnode.getAttributeValue(xmlnode.getAttributeKeyAt(0),xmlnode.getAttributeKeyNsAt(0))))
{
dailyHistory.Description__c=
String.valueOf(xmlnode.getAttributeValue(xmlnode.getAttributeKeyAt(0),
xmlnode.getAttributeKeyNsAt(0)).Trim());
}
if(String.isNotBlank(xmlnode.getAttributeValue(xmlnode.getAttributeKeyAt(2),xmlnode.getAttributeKeyNsAt(2))))
{
dailyHistory.MarketName__c=
String.valueOf(xmlnode.getAttributeValue(xmlnode.getAttributeKeyAt(2),
xmlnode.getAttributeKeyNsAt(2)).Trim());
}
if(String.isNotBlank(xmlnode.getAttributeValue(xmlnode.getAttributeKeyAt(3),xmlnode.getAttributeKeyNsAt(3))))
{
dailyHistory.RequestedSymbol__c=
String.valueOf(xmlnode.getAttributeValue(xmlnode.getAttributeKeyAt(3),
xmlnode.getAttributeKeyNsAt(3)).Trim());
}
if(String.isNotBlank(xmlnode.getAttributeValue(xmlnode.getAttributeKeyAt(5),xmlnode.getAttributeKeyNsAt(5))))
{
dailyHistory.TickerSymbol__c= String.valueOf(xmlnode.getAttributeValue(xmlnode.getAttributeKeyAt(5),
xmlnode.getAttributeKeyNsAt(5)).Trim());
}
}
if (child.getname() == 'Day')
{
if (child.getAttributeCount() > 0)
{
if(String.isNotBlank(child.getAttributeValue(child.getAttributeKeyAt(0),child.getAttributeKeyNsAt(0))))
{
dailyHistory.Close__c
=String.valueOf(child.getAttributeValue(child.getAttributeKeyAt(0),
child.getAttributeKeyNsAt(0)).Trim());
}
}
}
}
}
catch(Exception ex)
{
system.debug( ex.getMessage());
}
return dailyHistory;
}
<HistoryList Count="316" Time="2012-04-27T14:54Z 564ms" UserID="demo">
<History Description="CORN May 2012" Market="3" MarketName="CBOT" RequestedSymbol="@C`##" Status="0" TickerSymbol="@CK12">
<Day Close="623.25" Date="2012-04-16" High="629.0" Low="621.0" OI="277170" Open="626.75" Volume="125988"/>
</History>
</HistoryList>
Apex Parser
List<CustonObject> objCustom= new List<CustonObject>();
Dom.Document doc = res.getBodyDocument();
//Retrieve the root element for this document.
Dom.XMLNode InputNode= doc.getRootElement();
for(Dom.XMLNode child : InputNode.getChildElements())
{
for (dom.XmlNode awr : child.getChildElements() )
{
objCustom.add(loadDataToCustomObject(awr ));
}
}
}
}//InnerMost For Loop
} // Outermost for For Loop
//Inserting data to custom object
insert objCustom;
public static CustonObject loadDataToCustomObject(DOM.XMLNode xmlnode)
{
CustonObject dailyHistory = new CustonObject();
try
{
for(Dom.XMLNode child : xmlnode.getChildElements())
{
if(xmlnode.getAttributeCount() > 0)
{
if(String.isNotBlank(xmlnode.getAttributeValue(xmlnode.getAttributeKeyAt(0),xmlnode.getAttributeKeyNsAt(0))))
{
dailyHistory.Description__c=
String.valueOf(xmlnode.getAttributeValue(xmlnode.getAttributeKeyAt(0),
xmlnode.getAttributeKeyNsAt(0)).Trim());
}
if(String.isNotBlank(xmlnode.getAttributeValue(xmlnode.getAttributeKeyAt(2),xmlnode.getAttributeKeyNsAt(2))))
{
dailyHistory.MarketName__c=
String.valueOf(xmlnode.getAttributeValue(xmlnode.getAttributeKeyAt(2),
xmlnode.getAttributeKeyNsAt(2)).Trim());
}
if(String.isNotBlank(xmlnode.getAttributeValue(xmlnode.getAttributeKeyAt(3),xmlnode.getAttributeKeyNsAt(3))))
{
dailyHistory.RequestedSymbol__c=
String.valueOf(xmlnode.getAttributeValue(xmlnode.getAttributeKeyAt(3),
xmlnode.getAttributeKeyNsAt(3)).Trim());
}
if(String.isNotBlank(xmlnode.getAttributeValue(xmlnode.getAttributeKeyAt(5),xmlnode.getAttributeKeyNsAt(5))))
{
dailyHistory.TickerSymbol__c= String.valueOf(xmlnode.getAttributeValue(xmlnode.getAttributeKeyAt(5),
xmlnode.getAttributeKeyNsAt(5)).Trim());
}
}
if (child.getname() == 'Day')
{
if (child.getAttributeCount() > 0)
{
if(String.isNotBlank(child.getAttributeValue(child.getAttributeKeyAt(0),child.getAttributeKeyNsAt(0))))
{
dailyHistory.Close__c
=String.valueOf(child.getAttributeValue(child.getAttributeKeyAt(0),
child.getAttributeKeyNsAt(0)).Trim());
}
}
}
}
}
catch(Exception ex)
{
system.debug( ex.getMessage());
}
return dailyHistory;
}
No comments:
Post a Comment