Hi, i have created a saxparser object and xml read in java inside a POJO object that handles different types of xml files depending on what the caller specify's
this POJO gets an xml value that represents a list of contacts names and ID's while the other xml file/value represents a single contacts actual details i.e. phone number, address etc etc.
My question more about how i can remove duplicated code from the below code:
public static List<ContactName> extractContactList(String xml, int type) {
mXMLdata = new StringReader(xml);
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser sp = factory.newSAXParser();
XMLReader xr = sp.getXMLReader();
if(type == XML_MODE_PARSE_CONTACT_DESC){
mContactDescHandler = new ContactDescXmlHandler();
xr.setContentHandler(mContactDescHandler);
xr.parse(new InputSource(mXMLdata));
return mContactDescHandler.getContactDesc();
return null;
} else if(type == XML_MODE_PARSE_CONTACT_LIST){
mContactListHandler = new ContactListXmlHandler();
xr.setContentHandler(mContactListHandler);
xr.parse(new InputSource(mXMLdata));
return mContactListHandler.getContactNameList();
}
As you can see i am using two different POJO'S that extend "DefaultHandler
and they both use my XMLReader
to do setContentHandler
and parse.
is their a way in java to return a generic List<>()
object as the two handlers return me different list pojo's or am i better of leaving it has it is OR seperate the two completely in different methods?
The xr.setContentHandler(mContactDescHandler);
and
xr.parse(new InputSource(mXMLdata));
i could definitely write it once but i think the main issue is the return type.
cheers in advance