在Android中解析嵌套的XML节点

时间:2014-08-27 09:29:44

标签: java android xml parsing

我有这样的XML文件:

<catalog>
   <category>
      <books>
         <book>
            <id>book1</id>
            <description>short description</description>
            <pages>number of pages</pages>
            <author>Anonymous</author>
            <name>Book title</name>
         </book>
         <book>
            <id>book2</id>
            <description>short description</description>
            <pages>number of pages</pages>
            <author>Anonymous</author>
            <name>Book title</name>
         </book>
      </books>
   </category>
   <category>
      <books>
         <book>
            <id>book3</id>
            <description>short description</description>
            <pages>number of pages</pages>
            <author>Anonymous</author>
            <name>Book title</name>
         </book>
      </books>
   </category>
</catalog>

我需要一个带有Categories的ArrayList和带有Books的ArrayList。这是我的书和类别课程:

public class Book 
{
    private String id;
    private String description;
    private String pages;
    private String author;
    private String name;
}
public class Category {
    private ArrayList<Book> books;
    private String name;
}

最简单的方法是什么?仅使用android库和东西比使用一些不用于我的任务的商业用途的库更好。我尝试了一些DOM解析器,但它并没有很好用。

2 个答案:

答案 0 :(得分:2)

实际上可以使用 dom 解析器轻松完成。这是一个独立的java程序来解析你的XML文件。我在标准输出中打印值,您可以修改它以在Android应用程序中使用。

    import org.w3c.dom.Document;
    import org.w3c.dom.Node;
    import org.w3c.dom.NodeList;
    import org.xml.sax.SAXException;
    import org.w3c.dom.Element;

    import javax.xml.parsers.DocumentBuilder;
    import javax.xml.parsers.DocumentBuilderFactory;
    import javax.xml.parsers.ParserConfigurationException;
    import java.io.File;
    import java.io.IOException;
    import java.util.ArrayList;

    public class Main {

        public static void main(String[] args) throws IOException, SAXException, ParserConfigurationException {
            File fXmlFile = new File("books");
            DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
            Document doc = dBuilder.parse(fXmlFile);

            // iterate through <category> tags
            NodeList categoryList = doc.getElementsByTagName("category");
            for (int categoryNo=0;categoryNo<categoryList.getLength();categoryNo++) {

                Element categoryNode = (Element)categoryList.item(categoryNo);

                System.out.println("Category No  " +categoryNo);

                //iterate through  <books> tags
                NodeList booksList = categoryNode.getElementsByTagName("books");
                for (int booksNo=0;booksNo<booksList.getLength();booksNo++) {

                    Element books = (Element)booksList.item(0);

                    // iterate through <book> tags
                    NodeList bookList = books.getElementsByTagName("book");
                    for(int bookNo=0;bookNo<bookList.getLength();bookNo++) {
                        Element bookElement = (Element)bookList.item(bookNo);

                        System.out.println(bookElement.getElementsByTagName("id").item(0).getTextContent());
                        System.out.println(bookElement.getElementsByTagName("description").item(0).getTextContent());
                        System.out.println(bookElement.getElementsByTagName("pages").item(0).getTextContent());
                        System.out.println(bookElement.getElementsByTagName("author").item(0).getTextContent());
                        System.out.println(bookElement.getElementsByTagName("name").item(0).getTextContent());
                    }

                }
            }
        }
    }

答案 1 :(得分:0)

查看android xml pull parser,此页面也有示例 (它是一个SAX解析器)