使用Java中的SAX Parser同时读取父级和子级

时间:2013-12-28 12:04:24

标签: java xml sax

我有一个非常小的xml文件,我只想用Java阅读。在引用this post之后,我决定使用SAX解析器。我的xml文件如下所示 -

<?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>

在阅读此xml时,我无法识别已阅读和未阅读的书籍。 下面是解析器代码 -

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;
    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 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("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
            lib.addIntoReadBooks(book);
            // lib.addIntoUnreadBooks(book);
        }
    }

    /**
    * @return the library
    */
    public Library getLibrary() {
        return lib;
    }
    public static void main(String[] args) {
        try {
            ParseXML parseXML = new ParseXML("repository/books.xml");
            Library library = parseXML.getLibrary();

            System.out.println("Library=" + library);

            } catch (ParserConfigurationException e) {
            System.err.println("Error " + e.getMessage());
            } catch (SAXException e) {
            System.err.println("Error " + e.getMessage());
            } catch (IOException e) {
            System.err.println("Error " + e.getMessage());
        }
    }
}

图书馆和图书课程如下 -

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
}

Book.java

public class Book {
    private final String id;
    private final String lang;
    private final String title;
    private final String author;

    public Book(String id, String lang, String title, String author) {
        this.id = id;
        this.lang = lang;
        this.title = title;
        this.author = author;
    }
    //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;
    }
}

是否有更好的解决办法?

1 个答案:

答案 0 :(得分:1)

我不清楚这必然会更好,但您可以在currentList类中维护ParseXML字段,以便操作当前列表:

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;
    }

}

作为一种稍微不同的方法,您当然可以将列表和库名称保留为处理程序中的字段,并使用收集的所有数据在Library方法中创建endElement

注意:(已废弃)在此代码中,我使用字段extraBooks来允许在<book><read>标记之外发生<unread>个标记。它比在currentList方法中将null设置为endElement更安全一些。我讨厌NullPointerException

<强>更新

您不需要一次性字段extraBooks

您可以通过将currentList的初始化更改为

来实现相同的空安全性
private List<Book> currentList = new ArrayList<Book>();

endElement方法

@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
    if (qName.equals("read") || qName.equals("unread"))
        currentList = new ArrayList<Book>();
}