Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save manisnesan/7753b4cc702f8a40d167 to your computer and use it in GitHub Desktop.
Save manisnesan/7753b4cc702f8a40d167 to your computer and use it in GitHub Desktop.
/**
* Sample Method to convert the inputstream feed from GSA to JaxB class Gsafeed
*/
public Gsafeed parseRecords(InputStream feedInputStream) {
SAXParserFactory spf = SAXParserFactory.newInstance();
spf.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
spf.setFeature("http://xml.org/sax/features/validation", false);
spf.setNamespaceAware(true); // Binding attributes
EntityResolver entityResolver = new EntityResolver() {
@Override
public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException {
//To ignore DocType
if (publicId.equals("-//Google//DTD GSA Feeds//EN")) {
return new InputSource(new StringReader(""));
}
return null;
}
};
XMLReader xmlReader = spf.newSAXParser().getXMLReader();
xmlReader.setEntityResolver(entityResolver);
SAXSource source = new SAXSource(xmlReader, new InputSource(feedInputStream));
JAXBContext jaxbContext = JAXBContext.newInstance(JAXB-GENERATATED-CLASSES-DIRECTORY);
Unmarshaller um = jaxbContext.createUnmarshaller();
gsa = (Gsafeed) um.unmarshal(source);
return gsa;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment