Wrong XML structure XML. Parsing – android



I'm trying to parse this link.


This is my DocumentBuilder:



public final static Document XMLfromString(String xml) {
Document doc = null;

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
try {

DocumentBuilder db = dbf.newDocumentBuilder();

InputSource is = new InputSource();
is.setCharacterStream(new StringReader(xml));
doc = db.parse(is);

} catch (ParserConfigurationException e) {
System.out.println("XML parse error: " + e.getMessage());
return null;
} catch (SAXException e) {
System.out.println("Wrong XML file structure: " + e.getMessage());
return null;
} catch (IOException e) {
System.out.println("I/O exeption: " + e.getMessage());
return null;
}

return doc;
}


Every time I get "NullPointerException: Attempt to invoke interface method 'org.w3c.dom.Element org.w3c.dom.Document.getDocumentElement()' on a null object reference" when I try to get NodeList nl = doc.getElementsByTagName("item"); because of Wrong XML file structure.


No comments:

Post a Comment