0

我的安卓手机中的解析器有问题!这是 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 回答
0

这是一个使用 SAX 解析器的示例,它可以帮助您

解析器类:

    public class XMLParser extends DefaultHandler 
{
    // xml Tags name
    private final String TENDER_TYPE = "TenderType";
    private final String TRX_TYPE    = "TransactionType";
    private final String DATA_ELEM   = "DataElement";   
    //xml Values
    private final String NAME        = "Name";
    private final String VALUE       = "Value";
    private final String AMOUNT      = "Amount";
    private final String T_TYPE      = "TenderType";
    private final String CLRK_ID     = "ClerkId";
    private final String INV_NUM     = "InvoiceNum";
    private final String AUTH        = "Authorization";
    private final String ORIG_SEQ    = "OriginalSequence";
    private final String ORIG_REF    = "OriginalReference";
    private final String TAG         = "Tag";
    private final String DESCRIPTION = "Description";


    //list for imported Config data
    private ArrayList<TenderType> theTenderTypeList   = null;
    private TenderType currentTenderType              = null;

    private ArrayList<TransactionType> theTrxTypeList = null;
    private TransactionType currentTrxType            = null;

    private ArrayList<DataElement> theDataElementList = null;
    private DataElement currentDataElement            = null;


    @Override
    public void startDocument() throws SAXException 
    {
        super.startDocument();
        if(theTenderTypeList == null)
        {
            theTenderTypeList  = new ArrayList<TenderType>();
        }
        if(theTrxTypeList == null)
        {
            theTrxTypeList = new ArrayList<TransactionType>();
        }
        if(theDataElementList == null)
        {
            theDataElementList = new ArrayList<DataElement>();
        }
    }

    @Override
    public void startElement(String uri, String localName, String name, Attributes attributes) throws SAXException 
    {   
        if (localName.equalsIgnoreCase(TRX_TYPE))
        {           
            this.currentTrxType = new TransactionType(attributes.getValue(NAME),
                    attributes.getValue(VALUE),attributes.getValue(AMOUNT), 
                    attributes.getValue(T_TYPE),attributes.getValue(CLRK_ID),
                    attributes.getValue(INV_NUM), attributes.getValue(AUTH),
                    attributes.getValue(ORIG_SEQ), attributes.getValue(ORIG_REF));

            this.theTrxTypeList.add(currentTrxType);
        }
        else if (localName.equalsIgnoreCase(TENDER_TYPE))
        {           
            this.currentTenderType = new TenderType(attributes.getValue(NAME),
                                                    attributes.getValue(VALUE));
            theTenderTypeList.add(currentTenderType);
        }
        else if (localName.equalsIgnoreCase(DATA_ELEM))
        {           
            this.currentDataElement = new DataElement(attributes.getValue(TAG),
                                                    attributes.getValue(DESCRIPTION));
            theDataElementList.add(currentDataElement);
        }

    }

    public ArrayList<TenderType> getTenderTypeList()
    {
        return theTenderTypeList;
    }

    public ArrayList<TransactionType> getTrxTypeList()
    {
        return theTrxTypeList;
    }

    public ArrayList<DataElement> getDataElementList()
    {
        return theDataElementList;
    }

}

然后你可以称之为:

SAXParserFactory fabrique = SAXParserFactory.newInstance();
        SAXParser parseur = null;

        try
        {
            parseur = fabrique.newSAXParser();
        } 
        catch (ParserConfigurationException e) 
        {
            System.out.println("Parse Config Exception");
        } 
        catch (SAXException e) 
        {
            System.out.println("SAX Parse Exception");
        }

        handler = new XMLParser();

        try 
        {
            /* Parse Config File */
            myFileCfg = new File(FolderPath + CFG_FILE_NAME);
            parseur.parse(myFileCfg, handler);

        } 
        catch (SAXException e) 
于 2013-04-19T13:47:52.183 回答