熟悉用Java解析XML文件 [英] Confussion with parsing XML file in Java
问题描述
鉴于此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文件打印这样的东西:
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);
}
}
我的问题是它总是打印结果4次(第一次没有Http Method字段。我想这对所有Sax Parsers初学者来说都是一个问题。
我知道startElement,endElement,characters函数是做什么的,但正如你所看到的,我不知道如何使用它们corectly。我应该在代码中更改什么,以便我能得到正确的输出?
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 corectly. What should I change in my code so i can have the correct output ?
谢谢。
推荐答案
问题是你的角色方法。将它的身体改为
The problem is your characters method. Change its body to
s.append(new String(ch, start, length));
然后将此行添加到开头startElement
then add this line to the start of startElement
s.setLength(0);
你应该看到一些输出。
这是什么 SAX上的Java教程必须说明字符方法:
Here's what the Java tutorial on SAX has to say about the characters method:
解析器不需要一次返回任何特定数量的字符。解析器可以一次从单个字符返回任何数字,但仍然是符合标准的实现。因此,如果您的应用程序需要处理它看到的字符,那么使用characters()方法在java.lang.StringBuffer中累积字符并且只有在您确定已找到所有字符时才对它们进行操作是明智的。 / p>
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.
这篇关于熟悉用Java解析XML文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!