Problems parsing XML from a String but not a file
I have an xml parser that works perfectly when I load it from a file:
private void getParsedXML(int id, Context context) throws Exception {
/* Get a SA开发者_Python百科XParser from the SAXPArserFactory. */
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
/* Get the XMLReader of the SAXParser we created. */
XMLReader xr = sp.getXMLReader();
/* Create a new ContentHandler and apply it to the XML-Reader */
XMLHandlerLevel myExampleHandler = new XMLHandlerLevel();
xr.setContentHandler(myExampleHandler);
/* Load xml file from raw folder*/
InputStream in = context.getResources().openRawResource(id);
/* Begin parsing */
xr.parse(new InputSource(in));
this.levelData = myExampleHandler.getLevelData();
in.close();
}
But now I also get somme content form a server. The XML is then send as a String and this is where it blocks:
private void getParsedXML(String s) throws Exception{
if(this.levelData == null){
InputStream in = StringToStream(s);
/* Get a SAXParser from the SAXPArserFactory. */
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
XMLHandlerLevel myExampleHandler = new XMLHandlerLevel();
XMLReader xr = sp.getXMLReader();
/* Begin parsing */
xr.parse(new InputSource(in));
this.levelData = myExampleHandler.getLevelData();
Log.e("jason",(levelData!=null)?levelData.toString():null);
in.close();
}
}
and here is the StringToString Function
public static InputStream StringToStream(String text) {
InputStream is = null;
/*
* Convert String to InputStream using ByteArrayInputStream
* class. This class constructor takes the string byte array
* which can be done by calling the getBytes() method.
*/
try {
is = new ByteArrayInputStream(text.getBytes("UTF-8"));
is.reset();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (Exception e){
Log.e("jason","failed again");
}
return is;
}
But this doesn't work...
I am sure of the content of the String. and I tested the StringToStream function it works fine.
I also put logs in XMLHandlerLevel to check what was happening and NOTHING the function startDocument() isn't even called but I get no error message no exceptions so I'm completely lost to explain this.
to resume: My XMLParser works fine when the Stream comes from context.getResources().openRawResource(id); but not when it comes form a String Why?
Thanks for any ideas
Jason
You don't appear to be setting a content handler in the code path that takes a string.
Why not wrap the String in a StringReader and parse that instead?
InputSource is = new InputSource(new StringReader(string));
xr.parse(is);
You example (modified):
private void getParsedXML(String s) throws Exception{
if(this.levelData == null){
/* Get a SAXParser from the SAXPArserFactory. */
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
XMLHandlerLevel myExampleHandler = new XMLHandlerLevel();
XMLReader xr = sp.getXMLReader();
/* Begin parsing */
InputSource is = new InputSource(new StringReader(string));
xr.parse(is);
this.levelData = myExampleHandler.getLevelData();
Log.e("jason",(levelData!=null)?levelData.toString():null);
in.close();
}
}
i am not sure that it will help u but there is a external jar file called Jsoup.jar add it to your project, it have the APIs of parsing xml,html...see tutorial on internet....
精彩评论