Java 中解析 XML 文件的混乱

发布于 2024-11-18 21:16:10 字数 1779 浏览 4 评论 0原文

给定这个 XML 文件:

<?xml version="1.0" encoding="UTF-8"?>
<root>
   <data>
      <track clipid="1">
         <url>http://www.emp3world.com/to_download.php?id=33254</url>
         <http_method>GET or POST</http_method>
         <post_body>a=1&b=2&c=3</post_body>
      </track>
   </data>
</root>

我所追求的是从此 XML 文件打印类似的内容:

ID: 1
URL: http://www.emp3world.com/to_download.php?id=33254
Http method: GET or POST

目前这是我的原始处理程序代码:

class MyHandler extends DefaultHandler
{
    String str = "";
    StringBuilder s = new StringBuilder();
    public void startElement(String namespaceURI, String sName, String qName, Attributes atts)
    {
        if(qName.equals("track"))
        {
            s.append("ID: ").append(atts.getValue("clipid")).append("\n");
        }
        if(qName.equals("url"))
        {
            s.append("URL: ");
        }
        if(qName.equals("http_method"))
        {
            s.append("Http method: ");
        }
    }

    public void endElement(String uri, String localName, String qName)
    {
        if(qName.equals("url"))
        {
            s.append(str).append("\n");
            str = "";
        }
        if(qName.equals("http_method"))
        {
            s.append(str).append("\n");
            str = "";
        }
        System.out.println(s);
    }

    public void characters(char[] ch, int start, int length) throws SAXException {
        str = new String(ch, start, length);
    }
}

我的问题是它总是打印结果 4 次(第一次没有 Http Method 字段)。我想这对所有 Sax 解析器初学者来说都是一个问题。
我知道 startElement、endElement、characters 函数的作用,但如您所见,我不知道如何正确使用它们。我应该在代码中更改哪些内容才能获得正确的输出?

Given this XML file:

<?xml version="1.0" encoding="UTF-8"?>
<root>
   <data>
      <track clipid="1">
         <url>http://www.emp3world.com/to_download.php?id=33254</url>
         <http_method>GET or POST</http_method>
         <post_body>a=1&b=2&c=3</post_body>
      </track>
   </data>
</root>

What I am after is to print something like this from this XML file:

ID: 1
URL: http://www.emp3world.com/to_download.php?id=33254
Http method: GET or POST

At the moment this is my primitive handler code:

class MyHandler extends DefaultHandler
{
    String str = "";
    StringBuilder s = new StringBuilder();
    public void startElement(String namespaceURI, String sName, String qName, Attributes atts)
    {
        if(qName.equals("track"))
        {
            s.append("ID: ").append(atts.getValue("clipid")).append("\n");
        }
        if(qName.equals("url"))
        {
            s.append("URL: ");
        }
        if(qName.equals("http_method"))
        {
            s.append("Http method: ");
        }
    }

    public void endElement(String uri, String localName, String qName)
    {
        if(qName.equals("url"))
        {
            s.append(str).append("\n");
            str = "";
        }
        if(qName.equals("http_method"))
        {
            s.append(str).append("\n");
            str = "";
        }
        System.out.println(s);
    }

    public void characters(char[] ch, int start, int length) throws SAXException {
        str = new String(ch, start, length);
    }
}

My problem is that it always prints the results 4 times(first time without the Http Method field. I guess this is a problem for all Sax Parsers beginners.
I know what startElement, endElement, characters functions do, but as you can see, I don't know how to use them correctly. What should I change in my code so I can have the correct output?

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

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

发布评论

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

评论(1

哆啦不做梦 2024-11-25 21:16:10

问题是你的人物方法。更改其主体,

s.append(new String(ch, start, length));

然后将此行添加到 startElement 的开头

s.setLength(0);

,您应该会看到一些输出。

以下是关于 SAX 的 Java 教程有关字符的内容方法:

解析器不需要一次返回任何特定数量的字符。解析器可以一次返回从单个字符到数千个字符的任何内容,并且仍然是符合标准的实现。因此,如果您的应用程序需要处理它看到的字符,那么明智的做法是让 strings() 方法将字符累积在 java.lang.StringBuffer 中,并仅在您确定已找到所有字符时才对它们进行操作。< /p>

The problem is your characters method. Change its body to

s.append(new String(ch, start, length));

then add this line to the start of startElement

s.setLength(0);

and you should see some output.

Here's what the Java tutorial on SAX has to say about the characters method:

Parsers are not required to return any particular number of characters at one time. A parser can return anything from a single character at a time up to several thousand and still be a standard-conforming implementation. So if your application needs to process the characters it sees, it is wise to have the characters() method accumulate the characters in a java.lang.StringBuffer and operate on them only when you are sure that all of them have been found.

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