SAX解析和父/子关系

时间:2015-02-28 19:37:08

标签: java xml parsing dom sax

所以我面临这个问题,我需要解析XML文件以弹出域对象。听起来很简单,但是,XML文件中的元素可以具有未知的子容量,因此域对象可以具有到该类的另一个对象的实例变量,该对象可以具有到同一类的实例变量的变量等等。举个例子,这是XML文件提供的示例:

<categories>
    <category id="1">
        <name>XML</name>
        <category id="2">
            <name>XPath</name>
        </category>
        <category id="3">
            <name>XML Schema</name>
        </category>
        <category id="4">
            <name>XSLT</name>
        </category>
        <category id="5">
            <name>XSL-FO</name>
        </category>
        <category id="6">
            <name>XQuery</name>
        </category>
    </category>
    <category id="7">
        <name>Java</name>
        <category id="100">
            <name>SDK</name>
            <category id="8">
                <name>Collections</name>
            </category>
            <category id="9">
                <name>NIO</name>
            </category>
            <category id="10">
                <name>Concurrency</name>
            </category>
        </category>
        <category id="1000">
            <name>EE</name>
            <category id="11">
                <name>EJB</name>
            </category>
            <category id="12">
                <name>Web</name>
            </category>
            <category id="13">
                <name>Webservices</name>
            </category>
        </category>
        <category id="0">
            <name>Examen boeken</name>
        </category>
    </category>
</categories>

我已经使用DOM解析器完成了这项工作但是对于我的研究,我需要使用SAX解析器来完成此操作。我陷入困境,我需要告诉哪个元素哪个元素作为子元素,哪个元素哪个元素作为父元素。

截至目前,我设法在包含其ID和名称的地图中获取所有类别条目。

代码如下所示:

public static void main(String[] args) throws SAXException, IOException,
        ParserConfigurationException {
    Bookshelf mijnBookshelf = new Bookshelf("boekenfestijn");
    Map<Integer, Category> categories = new HashMap<Integer, Category>();
    // TODO inlezen
    try {

        SAXParserFactory factory = SAXParserFactory.newInstance();
        SAXParser saxParser = factory.newSAXParser();

        DefaultHandler handler = new DefaultHandler() {

            String reading = null;

            boolean inCategory = false;
            boolean inName = false;

            int categoryId;

            Category currentCategory;


            public void startElement(String uri, String localName,
                    String qName, Attributes attributes)
                    throws SAXException {

                if(qName.equalsIgnoreCase("CATEGORY") && attributes.getValue("id") != null){
                    inCategory = true;
                    categoryId = Integer.parseInt(attributes.getValue("id"));
                    System.out.println("START HANDLING ID -> " + attributes.getValue("id"));

                }
                if(qName.equalsIgnoreCase("NAME")){
                    inName = true;
                }


            }

            public void endElement(String uri, String localName,
                    String qName) throws SAXException {


                if(inCategory){
                    inCategory = false;
                    System.out.println("CATEGORY ID : " + categoryId + " NAME : " + reading);
                    currentCategory = new Category(categoryId, reading);
                    currentCategory.setBookshelf(mijnBookshelf);
                    categories.put(categoryId, currentCategory);
                    System.out.println("END HANDLING");
                }

                if(inName){
                    inName = false;
                }



            }













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


            }

        };

        saxParser.parse("bookshelf.xml", handler);

    } catch (Exception e) {
        e.printStackTrace();
    }

    for (Integer i : categories.keySet()) {
        System.out.println("ID: " + categories.get(i).getId() + "->"
                + categories.get(i).getName());
    }

对于“类别”类

public class Category {

private Integer id;
private String name;
private Category parent;
private List<Category> children = new ArrayList<Category>();
private Bookshelf bookshelf;

public Category(){

}

public Category(Integer id, String name) {
    super();
    this.id = id;
    this.name = name;
}

public Integer getId() {
    return id;
}

public void setId(Integer id) {
    this.id = id;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public Category getParent() {
    return parent;
}

public void setParent(Category parent){
    this.parent = parent;
}

public List<Category> getChildren() {
    return children;
}

public String toString() {
    String s = bookshelf.getName() + "/";
    if (parent != null) {
        s = parent.toString();
    }
    s += name + "/";
    return s;
}

public Bookshelf getBookshelf() {
    return bookshelf;
}

public void setBookshelf(Bookshelf bookshelf) {
    this.bookshelf = bookshelf;
}

这就是我陷入困境的地步?我怎么能继续定义父子关系?我怎么知道我的处理程序中的哪个元素具有哪个元素作为children / parent?

非常感谢任何帮助!

TLDR:在使用sax解析器填充域对象时,如何定义父/子关系?

1 个答案:

答案 0 :(得分:2)

在SAX中,您无法直接知道哪个元素是另一个元素的父元素。处理此信息的常用方法是管理LIFO堆栈(例如java.util.Stack)。您按startElement()方法上的元素并将其弹出endElement()

不幸的是,由于startElement(),您无法预测子元素,直到您“遇到”它们。