从字符串而不是文件解析 XML 时出现问题
我有一个 xml 解析器,当我从文件加载它时,它可以完美地工作:
private void getParsedXML(int id, Context context) throws Exception {
/* Get a SAXParser 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();
}
但现在我也从服务器获取一些内容。然后,XML 作为字符串发送,这就是它阻塞的地方:
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();
}
}
这里是 StringToString 函数
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;
}
,但这不起作用......
我确信字符串的内容。 我测试了 StringToStream 函数,它工作正常。
我还将日志放入 XMLHandlerLevel 中以检查发生了什么,并且函数 startDocument() 甚至没有被调用,但我没有收到任何错误消息,没有例外,所以我完全无法解释这一点。
恢复: 当流来自 context.getResources().openRawResource(id); 时,我的 XMLParser 工作正常; 但当它来自字符串时则不然 为什么?
谢谢你的任何想
法杰森
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 SAXParser 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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您似乎没有在采用字符串的代码路径中设置内容处理程序。
为什么不将 String 包装在 StringReader 中并解析它呢?
您的示例(已修改):
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?
You example (modified):
我不确定它会对你有帮助,但是有一个名为 Jsoup.jar 的外部 jar 文件将其添加到你的项目中,它具有解析 xml、html 的 API...请参阅互联网上的教程...
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....