2012-07-20 96 views
0

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

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

`

+2

显示你已经尝试的东西,我们可以帮你看到你在哪里出错。 – 2012-07-20 02:17:19

+0

我创建了2本课本和作者来获取和设置价值。但结果不是我想要的,所有的书最终都由4位作者组成。 – user1462722 2012-07-20 02:37:37

回答

0

您的代码加入作者的authorL列表为你解析它们,然后分配该同样的列表到每本书。

要解决这个问题,你需要每一个你遇到的作者元素

喜欢的东西的时间来创建作家的新列表:

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>(); 
    } 
} 
+0

你救了我一天的老板,非常感谢你。让我开始爱萨克斯解析器,我感觉很好。 – user1462722 2012-07-20 05:47:37