Skip to content

Instantly share code, notes, and snippets.

Created December 3, 2012 00:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/4191809 to your computer and use it in GitHub Desktop.
Save anonymous/4191809 to your computer and use it in GitHub Desktop.
public with sharing class BPTCallout {
public BPTCallout() {}
public static Opportunity[] parseXML(XmlStreamReader reader) {
Opportunity[] oppList = new List<Opportunity>();
if (reader != null) {
while (reader.hasNext()) {
System.debug('$$$ reader.getEventType(): ' + reader.getEventType());
if (reader.getEventType() == XmlTag.END_DOCUMENT) {
break;
}
else if (reader.getEventType() == XmlTag.CHARACTERS) {
System.debug('$$$ reader.getLocalName(): ' + reader.getLocalName());
System.debug('$$$ reader.getText(): ' + reader.getText());
System.debug('$$$ reader.getNamespace(): ' + reader.getNamespace());
System.debug('$$$ reader.getlocation(): ' + reader.getlocation());
System.debug('$$$ reader.getprefix(): ' + reader.getprefix());
if (/*reader.getLocalName() == 'lname'*/ reader.getAttributeCount() == 4 && reader.getText() != null) {
oppList.add(new Opportunity(Name = reader.getText()));
}
}
reader.next();
}
}
System.debug('$$$ oppList: ' + oppList);
return oppList;
}
}
@isTest
private class BPTCalloutTest {
private static final String XML_STR = '<document>' +
'<result>success</result>' +
'<resultcode>000000</resultcode>' +
'<note></note>' +
'<item>' +
'<quantity>1</quantity>' +
'<fname>Bob</fname>' +
'<lname>Tungsten</lname>' +
'<address>23232 Fleet Street</address>' +
'<city>Santa Clara</city>' +
'<state>CA</state>' +
'<zip>94105</zip>' +
'<country>United States</country>' +
'<email>blahblahblah@blahblahblah.com</email>' +
'<phone>4155555555</phone>' +
'</item>' +
'</document>';
static testMethod void xmlStreamReaderTest() {
XmlStreamReader reader = new XmlStreamReader(XML_STR);
Opportunity[] opptyList = BPTCallout.parseXML(reader);
System.assert(opptyList != null);
System.assert(!opptyList.isEmpty());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment