parse() is not returning a document
In my app, I am retrieving an xml file from the web, I want to parse it, display the title and the date and then later the body.
Variable ‘target’ is the web address of a specific xml file – at this point the variable ‘target’ has held the correct value
Variable ‘line’ is a string contain the body of the xml file
Code:
public static <Uri> String getXML(String target){
String line = null;
try {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(target);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
line = EntityUtils.toString(httpEntity);
} catch (UnsupportedEncodingException e) {
line = "<results status=\"error\"><msg>Can't connect to server</msg></results>";
} catch (MalformedURLException e) {
line = "<results status=\"error\"><msg>Can't connect to server</msg></results>";
} catch (IOException e) {
line="<results status=\"error\"><msg>Can't connect to server</msg></results>";
}
return line;
}
It then goes to the following call Document doc = XMLfunctions.XMMLfromString(xml)
Upon access Variable ‘target’ still contains the characters of the xml file in a string format
The program then goes to XMLfromString(String xml) where xml is the returned value by getXML – this value is the actual xml file as a string – variable watching shows the correct characters in the string
Code:
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));
/* this fails, the string ‘is’ contains the correct characters from the xml file
Code:
doc = db.parse(is);
after parsing the variable watch shows a value of "doc = DocumentImpl(ID=83##########)+
Code:
} 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 exception: " + e.getMessage());
return null;
}
return doc;
}
Any ideas or help would be greatly appreciated.