0

Android フォンのパーサーに問題があります。ここにXMLのコードがあります

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<file>
    <Staff>
        <connection>wifi</connection>
        <timestamp>20</timestamp>
        <sport>0</sport>
    </Staff>
    <Staff>
        <connection>3g</connection>
        <timestamp>40</timestamp>
        <sport>0</sport>
    </Staff>
    <Staff>
        <connection>wifi</connection>
        <timestamp>60</timestamp>
        <sport>0</sport>        
    </Staff>
    <Staff>
        <connection>3g</connection>
        <timestamp>80</timestamp>
        <sport>0</sport>        
    </Staff>
</file>

ここに私が持っているパーサーコードがあります

 try {
                    InputStream filename = null;
                Document obj_doc = null;
                DocumentBuilderFactory doc_build_fact = null;
                DocumentBuilder doc_builder = null;
                filename = new FileInputStream("/sdcard/data.xml");
                doc_build_fact = DocumentBuilderFactory.newInstance();
                doc_builder = doc_build_fact.newDocumentBuilder();
                System.out.println("readed data.xml");
                obj_doc = doc_builder.parse(filename);

                NodeList obj_nod_list = null;
                if (null != obj_doc) {
                    org.w3c.dom.Element feed = obj_doc.getDocumentElement();
                    obj_nod_list = feed.getElementsByTagName("file"); 
                }

                Element root = obj_doc.getDocumentElement();
                NodeList items = root.getElementsByTagName("Staff");
                System.out.println("items "+items.getLength());



                for (int i = 0; i < items.getLength(); i++) {
                    Node item = items.item(i);
                    NodeList properties = item.getChildNodes();
                    System.out.println("properties length "+item.getChildNodes().getLength()); 
//                  System.out.println("properties "+properties.getLength()); 
                    for (int j = 0; j < items.getLength(); j++) {
                        Node property = properties.item(j);
                       // System.out.println("properties "+property.getNodeName());   
                        String name = property.getNodeName();

                        if (name.equalsIgnoreCase("connection")) {
                             // Store it where you want
                                connection.add(property.getFirstChild().getNodeValue()); 
                             System.out.println("connection "+connection);
//                           System.out.println("connection "+connection.get(i));
                        }
                            if (name.equalsIgnoreCase("timestamp")) {
                                int inttimestamp = Integer.parseInt(property.getFirstChild().getNodeValue());
                                timestamp.add(inttimestamp); 

                             System.out.println("timestamp "+timestamp);
                        }
                            if (name.equalsIgnoreCase("sport")) {
                                int inttimestamp = Integer.parseInt(property.getFirstChild().getNodeValue());
                                capacity.add(inttimestamp); 

                             System.out.println("capacity "+capacity);
                        }


                    }
                }

                } catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (ParserConfigurationException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (SAXException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

解析が完了したら!最後の子を読み取ることができません! それはスポーツです!解決策を教えてください!また、xml を変更して「スポーツ」を最初に配置すると、最後のノードを読み取れなくなります。ありがとう

4

3 に答える 3

0

解決しました!XMLファイルの構造が悪かった!変えてから!出来た。あなたのオーサーに感謝します

于 2013-04-19T14:25:10.027 に答える
0

jsoup html パーサーを使用します。公式サイトはこちら。html、xml などの解析に使用できます。タグ ベースのパーサーです。

于 2013-04-19T13:44:18.537 に答える