0

如何使用 sax 解析器解析 xml?我很难将作者部分解析为数组。我从这里跟随示例http://ganeshtiwaridotcomdotnp.blogspot.com/2011/08/xml-parsing-using-saxparser-with.html

XML

<catalog>
    <book id="001" lang="ENG">
        <isbn>23-34-42-3</isbn>
        <regDate>1990-05-24</regDate>
        <title>Operating Systems</title>
        <publisher country="USA">Pearson</publisher>
        <price>400</price>
        <authors>
            <author>
                <name>Ganesh Tiwari</name>
                <age>1</age>
            </author>
        </authors>
    </book>
    <book id="002">
        <isbn>24-300-042-3</isbn>
        <regDate>1995-05-12</regDate>
        <title>Distributed Systems</title>
        <publisher country="Nepal">Ekata</publisher>
        <price>500</price>
        <authors>
            <author>
                <name>Mahesh Poudel</name>
                <age>2</age>
            </author>
                <author>
                <name>Bikram Adhikari</name>
                <age>3</age>
            </author>
            <author>
                <name>Ramesh Poudel</name>
                <age>4</age>
            </author>
        </authors>
        </book>
</catalog>

开始

public BookSaxParser() {
    bookL = new ArrayList<Book>();
    authorL = new ArrayList<Author>();
}


public List<Book> printDatas() {

    return bookL;
}

@Override 
public void startElement(String s, String s1, String elementName, Attributes attributes) throws SAXException {
    // if current element is book , create new book
    // clear tmpValue on start of element

    if (elementName.equalsIgnoreCase("book")) {
        bookTmp = new Book();
        bookTmp.setId(attributes.getValue("id"));
        bookTmp.setLang(attributes.getValue("lang"));

    }

    if (elementName.equalsIgnoreCase("author")) {
        authorTmp = new Author();

    }
    // if current element is publisher
    if (elementName.equalsIgnoreCase("publisher")) {
        bookTmp.setPublisher(attributes.getValue("country"));
    }

}
@Override
public void endElement(String s, String s1, String element) throws SAXException {
    // if end of book element add to list
    if (element.equals("book")) {
        bookL.add(bookTmp);
    }


    if (element.equals("authors")) {
        bookTmp.setAuthors(authorL);

    }

    if (element.equals("author")) {
        authorL.add(authorTmp);

    }

    if(element.equalsIgnoreCase("name")){
        authorTmp.setName(tmpValue);

    }

    if(element.equalsIgnoreCase("age")){
        authorTmp.setAge(Integer.parseInt(tmpValue));
    }




    if (element.equalsIgnoreCase("isbn")) {
        bookTmp.setIsbn(tmpValue);
    }
    if (element.equalsIgnoreCase("title")) {
        bookTmp.setTitle(tmpValue);
    }

    if(element.equalsIgnoreCase("price")){
        bookTmp.setPrice(Integer.parseInt(tmpValue));
    }

    if(element.equalsIgnoreCase("regDate")){
        try {
            bookTmp.setRegDate(sdf.parse(tmpValue));
        } catch (ParseException e) {
            //System.out.println("date parsing error");
        }
    }
}
@Override
public void characters(char[] ac, int i, int j) throws SAXException {
    tmpValue = new String(ac, i, j);
}

public void endDocument() throws SAXException {
    // you can do something here for example send
    // the Channel object somewhere or whatever.
}

`

4

1 回答 1

0

您的代码在解析作者时将作者添加到 authorL 列表中,然后将相同的列表分配给每本书。

要解决此问题,您需要在每次遇到 Authors 元素时创建一个新的作者列表

就像是:

public void startElement(String s, String s1, String elementName, Attributes attributes) throws SAXException {
    // Your existing code here

    if (elementName.equalsIgnoreCase("authors")) {
        authorL = new ArrayList<Author>();
    }
}
于 2012-07-20T04:08:38.647 回答