在Java中使用SAX解析器同时读取父级和子级
我有一个非常小的xml文件,我只想用Java阅读。参考之后,我决定使用SAX解析器。我的xml文件如下所示-在Java中使用SAX解析器同时读取父级和子级,java,xml,sax,Java,Xml,Sax,我有一个非常小的xml文件,我只想用Java阅读。参考之后,我决定使用SAX解析器。我的xml文件如下所示- <?xml version="1.0" encoding="UTF-8"?> <catalog> <library name="Central Library"> <read> <book id="001" lang="ENG" title="Operating System Conce
<?xml version="1.0" encoding="UTF-8"?>
<catalog>
<library name="Central Library">
<read>
<book id="001" lang="ENG" title="Operating System Concepts" author="Silberschatz" />
<book id="002" lang="ENG" title="Design Patterns: Elements of Reusable Object-Oriented Software" author="Gangs of Four" />
</read>
<unread>
<book id="003" lang="ENG" title="Introduction to Algorithms" author="Cormen" />
<book id="004" lang="ENG" title="Computer networks" author="Tanenbaum" />
</unread>
</library>
</catalog>
图书馆和图书课如下-
Library.java
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public final class Library {
private final String name;
private final List<Book> readBooks;
private final List<Book> unreadBooks;
public Library(String name) {
this.name = name;
readBooks = new ArrayList<Book>();
unreadBooks = new ArrayList<Book>();
}
public void addIntoReadBooks(Book book) {
getReadBooks().add(book);
}
public void addIntoUnreadBooks(Book book) {
getUnreadBooks().add(book);
}
//Getters
}
如何决定该书应添加到哪个列表
我的回答
不知怎的,我设法接受了下面的代码,它使用两个标志来跟踪访问的节点-
import java.io.IOException;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
public class ParseXML extends DefaultHandler {
private Library lib;
private boolean read;
private boolean unread;
public ParseXML(String file) throws ParserConfigurationException, SAXException, IOException {
parse(file);
}
private void parse(String file) throws ParserConfigurationException, SAXException, IOException {
final SAXParserFactory factory = SAXParserFactory.newInstance();
final SAXParser parser = factory.newSAXParser();
parser.parse(file, this);
}
@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
if (qName.equals("read")) {
read = false;
}
if (qName.equals("unread")) {
unread = false;
}
}
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
if (qName.equals("library")) {
String name = attributes.getValue("name");
lib = new Library(name);
}
if (qName.equals("read")) {
read = true;
}
if (qName.equals("unread")) {
unread = true;
}
if (qName.equals("book")) {
String id = attributes.getValue("id");
String lang = attributes.getValue("lang");
String title = attributes.getValue("title");
String author = attributes.getValue("author");
Book book = new Book(id, lang, title, author);
// How to decide here, to which list this book should be added
if (read && !unread) {
lib.addIntoReadBooks(book);
} else if (!read && unread) {
lib.addIntoUnreadBooks(book);
}
}
}
/**
* @return the lib
*/
public Library getLibrary() {
return lib;
}
}
是否有更好的解决方案?我不清楚这是否一定更好,但您可以在
ParseXML
类中维护一个currentList
字段,以便当前列表进行操作:
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
public class ParseXML extends DefaultHandler {
private Library lib;
private List<Book> extraBooks = new ArrayList<Book>();
private List<Book> currentList = extraBooks;
public ParseXML(String file) throws ParserConfigurationException, SAXException, IOException {
parse(file);
}
private void parse(String file) throws ParserConfigurationException, SAXException, IOException {
final SAXParserFactory factory = SAXParserFactory.newInstance();
final SAXParser parser = factory.newSAXParser();
parser.parse(file, this);
}
@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
if (qName.equals("read") || qName.equals("unread"))
currentList = extraBooks;
}
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
if (qName.equals("library")) {
String name = attributes.getValue("name");
lib = new Library(name);
}
else if (qName.equals("read")) {
currentList = lib.getReadBooks();
}
else if (qName.equals("unread")) {
currentList = lib.getUnreadBooks();
}
else if (qName.equals("book")) {
String id = attributes.getValue("id");
String lang = attributes.getValue("lang");
String title = attributes.getValue("title");
String author = attributes.getValue("author");
currentList.add(new Book(id, lang, title, author));
}
}
/**
* @return the lib
*/
public Library getLibrary() {
return lib;
}
}
以及endElement
方法
@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
if (qName.equals("read") || qName.equals("unread"))
currentList = new ArrayList<Book>();
}
@覆盖
public void endElement(字符串uri、字符串localName、字符串qName)引发SAXException{
if(qName.equals(“读取”)| | qName.equals(“未读”))
currentList=新的ArrayList();
}
为什么投票关闭?我已经在很多地方提到过,但没有发现这种情况。为什么你没有正确地缩进代码?读起来很痛苦:)@Pavelholl:我道歉。我只是从Eclipse复制并粘贴。这个问题似乎离题了,因为它不是关于解决问题的。非常感谢。我在SAX中使用XMLReader
又找到了一种方法。它具有设置ContentHandler
的功能。非常感谢你。
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
public class ParseXML extends DefaultHandler {
private Library lib;
private List<Book> extraBooks = new ArrayList<Book>();
private List<Book> currentList = extraBooks;
public ParseXML(String file) throws ParserConfigurationException, SAXException, IOException {
parse(file);
}
private void parse(String file) throws ParserConfigurationException, SAXException, IOException {
final SAXParserFactory factory = SAXParserFactory.newInstance();
final SAXParser parser = factory.newSAXParser();
parser.parse(file, this);
}
@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
if (qName.equals("read") || qName.equals("unread"))
currentList = extraBooks;
}
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
if (qName.equals("library")) {
String name = attributes.getValue("name");
lib = new Library(name);
}
else if (qName.equals("read")) {
currentList = lib.getReadBooks();
}
else if (qName.equals("unread")) {
currentList = lib.getUnreadBooks();
}
else if (qName.equals("book")) {
String id = attributes.getValue("id");
String lang = attributes.getValue("lang");
String title = attributes.getValue("title");
String author = attributes.getValue("author");
currentList.add(new Book(id, lang, title, author));
}
}
/**
* @return the lib
*/
public Library getLibrary() {
return lib;
}
}
private List<Book> currentList = new ArrayList<Book>();
@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
if (qName.equals("read") || qName.equals("unread"))
currentList = new ArrayList<Book>();
}