java中解析xml字符串时出现问题

发布于 2024-11-18 03:22:47 字数 650 浏览 1 评论 0原文

我正在编写一个 Android 应用程序,我想从 Web 获取一个 xml 字符串并获取它包含的所有信息。 首先,我得到字符串(此代码有效):


URL url = new URL("here my adrress");
BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream()));
String myData = reader.readLine();
reader.close();

然后,我使用 DOM:


DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
InputSource is = new InputSource();
is.setCharacterStream(new StringReader(myData));

仍然没有问题。当我编写


Document doc = db.parse(is);

应用程序时,不再执行任何操作。它停止,没有错误。 有人可以告诉我发生了什么事吗?

I'm writing an android application, and I would like to get an xml string from web and get all info it contains.
First of all, i get the string (this code works):


URL url = new URL("here my adrress");
BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream()));
String myData = reader.readLine();
reader.close();

Then, I use DOM:


DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
InputSource is = new InputSource();
is.setCharacterStream(new StringReader(myData));

Still no problem. When I write


Document doc = db.parse(is);

the application doesn't do anything more. It stops, without errors.
Can someone please tell me what's going on?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

满身野味 2024-11-25 03:22:47

我不知道为什么你的代码不起作用,因为没有错误,但我可以提供替代方案。

首先,我很确定你的新InputStream“是”是不必要的。 “parse()”可以直接将“url.openStream()”或“myData”作为参数。

错误的另一个原因可能是您的 xml 数据有多于一行(我知道您说过代码的第一部分有效,但我宁愿提及它,只是为了确定)。如果是这样,“reader.readLine()”只会获取 xml 数据的一部分。

我希望这会有所帮助。

I wouldn't know why your code doesn't work since there is no error but I can offer alternatives.

First, I am pretty sure your new InputStream "is" is unnecessary. "parse()" can take "url.openStream()" or "myData" directly as an argument.

Another cause of error could be that your xml data has more than one line(I know you said that the first part of your code worked but I'd rather mention it, just to be sure). If so, "reader.readLine()" will only get you a part of your xml data.

I hope this will help.

没有你我更好 2024-11-25 03:22:47

使用 SAXParser 而不是 DOM 解析器。 SAXParser 比 DOM 解析器更高效。这是关于 SAXParser 的两个很好的教程
1. http://www.androidpeople.com/android-xml-parsing -教程使用-saxparser
2. http://www.anddev.org/parsing_xml_from_the_net_-_using_the_saxparser-t353.html

Use SAXParser instead of DOM parser. SAXParser is more efficient than DOM parser. Here is two good tutorials on SAXParser
1. http://www.androidpeople.com/android-xml-parsing-tutorial-using-saxparser
2. http://www.anddev.org/parsing_xml_from_the_net_-_using_the_saxparser-t353.html

烙印 2024-11-25 03:22:47

使用XmlPullParser,速度非常快。从网络传入字符串并获取包含所有值的哈希表。

public Hashtable<String, String> parse(String myData) { 
    XmlPullParser parser = Xml.newPullParser();
    Hashtable<String, String> responseFromServer = new Hashtable<String, String>();

    try {
         parser.setInput(new StringReader (responseString));
         int eventType = parser.getEventType();
         while (eventType != XmlPullParser.END_DOCUMENT) {
             if(eventType == XmlPullParser.START_TAG) {
                 String currentName = parser.getName();
                 String currentText = parser.nextText();

                 if (currentText.trim().length() > 0) {
                     responseFromServer.put(currentName, currentText);
                 } 
             }               
             eventType = parser.next();
         }         
    } catch (Exception e) {
        e.printStackTrace();
    }
    return responseFromServer;
}

Use XmlPullParser, it's very fast. Pass in the string from the web and get a hashtable with all the values.

public Hashtable<String, String> parse(String myData) { 
    XmlPullParser parser = Xml.newPullParser();
    Hashtable<String, String> responseFromServer = new Hashtable<String, String>();

    try {
         parser.setInput(new StringReader (responseString));
         int eventType = parser.getEventType();
         while (eventType != XmlPullParser.END_DOCUMENT) {
             if(eventType == XmlPullParser.START_TAG) {
                 String currentName = parser.getName();
                 String currentText = parser.nextText();

                 if (currentText.trim().length() > 0) {
                     responseFromServer.put(currentName, currentText);
                 } 
             }               
             eventType = parser.next();
         }         
    } catch (Exception e) {
        e.printStackTrace();
    }
    return responseFromServer;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文