-1

我有 XML:

    <?xml version="1.0" encoding="UTF-8"?>
          <categories type="array">
               <category>
                    <category_id>8</category_id>
                    <category_name>Công Nghệ  & Xe</category_name>
                    <category_code>cong-nghe-xe</category_code>
                    <subcategory>
                         <category_id>59</category_id>
                         <category_name>Kiến trúc - thủ thuật</category_name>
                         <category_code>cong-nghe-xe_kien-thuc-thu-thuat</category_code>
                    </subcategory>
                    .................................................
               </category>
               ................................................. 
          </categories>

我如何阅读它并在 Android 中填充 ListView,我应该怎么做:DOM、XML Parser 或 SAX?

4

2 回答 2

0

对于像这样的小片段,我更喜欢 DOM。

String xml;
Document doc;
Element root = null;
try {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    doc = builder.parse( new InputSource( new StringReader( xml ) ) );
    root = doc.getDocumentElement();
} catch ( Exception e ) {
    // do something
}

if( root != null ) {
    NodeList cats = root.getElementsByTagName( "category" );
    for( int i=0; i < cats.getLength(); i++ ) {
    Element category = (Element)cats.item( i );
        ....
    }
}
于 2012-09-20T03:34:39.357 回答
0

你试过Android的例子吗?

http://developer.android.com/training/basics/network-ops/xml.html

于 2012-09-20T03:37:45.917 回答