簡體   English   中英

使用Android的Sax解析器解析XML

[英]parse XML using Sax parser for android

如何使用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.
}

`

您的代碼在解析作者時將其添加到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>();
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM