要开始学习使用Saxparser,我想要解析一个非常小的XML文件。 我的XML文件:
编辑:(注意:每个条目都有一个用于识别的ID。我最终的目标是能够解析每个具有id的更复杂的xml文件)
<entry id="2000" >
<word>"hello"</word>
</entry>
现在我创建一个入门类:
public class Entry {
private int id;
private String word;
public int getID() {
return id;
}
public void setID(int id) {
this.id = id;
}
public String getWord() {
return word;
}
public void setWord(String word) {
this.word = word;
}
@Override
public String toString() {
return "Entry:: ID="+this.id+" word=" + this.word;
}
}
我的扩展默认处理程序:
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
import java.util.ArrayList;
import java.util.List;
public class MyHandler extends DefaultHandler {
private List<Entry> wordList = null;
private Entry word = null;
public List<Entry> getwordList() {
return wordList;
}
boolean bword = false;
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
if (qName.equalsIgnoreCase("entry")) {
String id = attributes.getValue("id");
word = new Entry();
word.setID(Integer.parseInt(id));
if (wordList == null) {
wordList = new ArrayList<>();
} else if (qName.equalsIgnoreCase("word")) {
bword = true;
}
}
}
@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
if (qName.equalsIgnoreCase("entry")) {
wordList.add(word);
}
}
@Override
public void characters(char ch[], int start, int length) throws SAXException {
if (bword) {
word.setWord(new String(ch, start, length));
bword = false;
}
}
}
最后我在这里运行:
import org.xml.sax.SAXException;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import java.io.File;
import java.io.IOException;
import java.util.List;
public class Main {
public static void main(String[] args) {
SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
try {
SAXParser saxParser = saxParserFactory.newSAXParser();
MyHandler handler = new MyHandler();
saxParser.parse(new File("/home/user/Desktop/test.xml"), handler);
//get verb liste
List<Entry> wordList = handler.getwordList();
//print verb
for (Entry word : wordList)
System.out.print(word);
} catch (ParserConfigurationException | SAXException | IOException e) {
e.printStackTrace();
}
}
}
结果如下:
Entry:: ID=2000 word=null
我知道问题出现在startElement中,因为它将我的bword设置为false或者它不识别标记。因为如果我将字符方法设置为true,那么if(bword){等它给了我
Entry:: ID=2000 word=
而不是
Entry:: ID=2000 word=null
答案 0 :(得分:0)
if (qName.equalsIgnoreCase("eintrag")) {
它必须是输入我猜
,例外是因为
您没有检查并非所有子元素都没有属性ID ...就像Word元素一样......
所以这里:
String id = attributes.getValue("id");
word = new Entry();
word.setID(Integer.parseInt(id));
id为null,然后你将尝试解析为一个爆炸代码的null ...
答案 1 :(得分:0)
感谢@fukanchic解释并指出错误:花括号设置错误
这里是更正后的MyHandler部分。
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
import java.util.ArrayList;
import java.util.List;
public class MyHandler extends DefaultHandler {
private List<Entry> wordList = null;
private Entry word = null;
public List<Entry> getwordList() {
return wordList;
}
boolean bword = false;
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
if (qName.equalsIgnoreCase("entry")) {
String id = attributes.getValue("id");
word = new Entry();
word.setID(Integer.parseInt(id));
if (wordList == null)
wordList = new ArrayList<>();
} else if (qName.equalsIgnoreCase("word")) {
bword = true;
}
}
@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
if (qName.equalsIgnoreCase("entry")) {
wordList.add(word);
}
}
@Override
public void characters(char ch[], int start, int length) throws SAXException {
if (bword) {
word.setWord(new String(ch, start, length));
bword = false;
}
}
}