0

我有一个这样的xml:

<dict>
    <key>id</key>
    <string><![CDATA[ 1 ]]></string>
    <key>posizione</key>
    <string><![CDATA[ 1 ]]></string>
    <key>id_parent</key>
    <string><![CDATA[ 0 ]]></string>
    <key>prodotto</key>
    <string><![CDATA[ Caduta temporanea ]]></string>
</dict>
<dict>
    <key>id</key>
    <string><![CDATA[ 1 ]]></string>
    <key>posizione</key>
    <string><![CDATA[ 1 ]]></string>
    <key>id_parent</key>
    <string><![CDATA[ 0 ]]></string>
    <key>prodotto</key>
    <string><![CDATA[ Caduta temporanea ]]></string>
</dict>

我如何解析这个以获得每个的简单字符串?通常,我使用这种方法:带有 cdata 的 Android xml 只返回 这个,我只获得前几个

有人能帮我吗?

4

1 回答 1

0

使用下面的代码使用 Dom Parser 解析相同的标签。

public class DomParserSampleActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        ScrollView mScrView1 = new ScrollView(this);

        /** Create a new layout to display the view */
        LinearLayout layout = new LinearLayout(this);
        layout.setOrientation(1);

        /** Create a new textview array to display the results */
        TextView id[];
        TextView published[];
        TextView content[];
        TextView title[];

        TextView mediacontent[];
        TextView mediathumbnail[];

        try {
            URL url = new URL("URL_OF_XML_FILE");
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
            Document doc = db.parse(new InputSource(url.openStream()));
            doc.getDocumentElement().normalize();

            NodeList nodeList = doc.getElementsByTagName("dict");

            /** Assign textview array length by arraylist size */
            firstkey = new TextView[nodeList.getLength()];
            secondkey = new TextView[nodeList.getLength()];
            thirdkey = new TextView[nodeList.getLength()];
            fourthkey = new TextView[nodeList.getLength()];

            for (int i = 0; i < nodeList.getLength(); i++) {

                Node node = nodeList.item(i);

                firstkey[i] = new TextView(this);
                secondkey[i] = new TextView(this);
                thirdkey[i] = new TextView(this);
                fourthkey[i] = new TextView(this);

                Element fstElmnt = (Element) node;

                NodeList keyList = fstElmnt.getElementsByTagName("key");
                Element keyElement = (Element) keyList.item(0);
                keyList = keyElement.getChildNodes();
                firstkey[i].setText("key is = "
                        + ((Node) keyList.item(0)).getNodeValue());
                secondkey[i].setText("key is = "
                        + ((Node) keyList.item(1)).getNodeValue());
                thirdkey[i].setText("key is = "
                        + ((Node) keyList.item(2)).getNodeValue());
                fourthkey[i].setText("key is = "
                        + ((Node) keyList.item(3)).getNodeValue());

                layout.addView(firstkey[i]); 
                layout.addView(secondkey[i]); 
                layout.addView(thirdkey[i]); 
                layout.addView(fourthkey[i]);    
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        /** Set the layout view to display */

        mScrView1.addView(layout);
        setContentView(mScrView1);
    }
}
于 2012-09-21T12:37:55.400 回答