如何解析XML(Fogbugz XML API)?

发布于 2024-09-12 04:13:47 字数 1119 浏览 9 评论 0原文

我正在创建一个连接到 Fogbugz XML API 的 Android 应用程序(向 API 发送请求,接收回 XML 文件)。现在,我正在创建一个服务来处理这些请求,并解析每个请求以访问可用信息。最好的方法是什么?如果我得到一个 inputStream,我应该使用 SAX 解析器吗?

        DefaultHttpClient httpclient = new DefaultHttpClient(); 
    HttpGet httpget = new HttpGet("My URL THAT RETURNS AN XML FILE"); 
    HttpResponse response;
    try {
            response = httpclient.execute(httpget);
            HttpEntity entity = response.getEntity(); 
            InputStream stream =  entity.getContent();
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(stream)); 
            String responseString = "";
            String temp;

            while ((temp=bufferedReader.readLine()) != null)
            {
                responseString += temp;
            }

            if (entity != null) { 
                entity.consumeContent(); 
            }

    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } 

I am creating an Android application that connects to the Fogbugz XML API (sends a request to the API, receives back an XML file). Right now, I am creating a service that will handle these requests, and parse each in order to access usable information. What would be the best way to do this? If I am getting an inputStream, should I use the SAX parser?

        DefaultHttpClient httpclient = new DefaultHttpClient(); 
    HttpGet httpget = new HttpGet("My URL THAT RETURNS AN XML FILE"); 
    HttpResponse response;
    try {
            response = httpclient.execute(httpget);
            HttpEntity entity = response.getEntity(); 
            InputStream stream =  entity.getContent();
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(stream)); 
            String responseString = "";
            String temp;

            while ((temp=bufferedReader.readLine()) != null)
            {
                responseString += temp;
            }

            if (entity != null) { 
                entity.consumeContent(); 
            }

    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } 

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

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

发布评论

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

评论(1

分开我的手 2024-09-19 04:13:47

我建议您使用一些 XML DOM 库,例如 XOM 或 Dom4j。使用树结构比使用 SAX 事件要容易得多。就我个人而言,我在 Foglyn 中使用 XOM —— FogBugz for Eclipse 插件。您可以将 InputStream 直接传递到 SAX/XOM/Dom4j 解析器,无需先构建字符串。此外,请确保您使用正确的编码...您的代码在这方面已损坏。 (当您将 InputStream 传递给解析器时,这是由解析器处理的。在 XOM 中,您可以使用 new Builder().build(InputStream) 方法)。

FogBugz API 的一个提示...当获取有关案例的详细信息时,并且您不需要事件(注释),则根本不要获取它们。即不要将事件放入列列表中。

I suggest that you use some XML DOM library like XOM or Dom4j. It will be much easier for you to work with tree structure than with SAX events. Personally, I use XOM in Foglyn -- FogBugz for Eclipse plugin. You can pass InputStream directly to your SAX/XOM/Dom4j parser, there is no need to build string first. Furthermore, make sure you use correct encoding ... your code is broken in this regard. (When you pass InputStream to your parser, this is handled by parser. In XOM you can use new Builder().build(InputStream) method).

One FogBugz API hint ... when getting details about case, and you don't need events (comments), don't fetch them at all. I.e. don't put events into list of columns.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文