2

我想为我的 android 应用程序解析从数据库软件导出的 XML 文件。然而,一些标签有这样的参数:

<row>
<value column="Index" null="false">1</value>
<value column="Front" null="false">INFO</value>
<value column="Back" null="false">INFO</value>
<value column="Check" null="false">0</value>
</row>

在尝试查找开始标记来解析它时,我指定了什么字符串值?(例如:要查找行,我将开始抽头与“行”进行比较,如果它返回 true,则计算数据。我对每个值(即索引、前、后和分别检查)做什么?)

我的java代码如下

   try{ 
    XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
    factory.setNamespaceAware(true);
    XmlPullParser xpp = factory.newPullParser();

    InputStream stream = context.getResources().openRawResource(com.Whydea.chemistryhelper.R.raw.appxml);
    xpp.setInput(stream, null);

    int eventType = xpp.getEventType();
    while (eventType != XmlPullParser.END_DOCUMENT){
        if(eventType==XmlPullParser.START_TAG){
            handleStartTag(xpp.getName());  //handels Start Tag
        } else if (eventType==XmlPullParser.END_TAG){
            handleEndTag(xpp.getName());
            Ctag=null;
        } else if (eventType==XmlPullParser.TEXT){
            handleText(xpp.getText());
        }
        eventType=xpp.next();
    }

    }catch (NotFoundException e){
        Log.d("XMLpp",e.getMessage());          
    }catch (XmlPullParserException e){
        Log.d("XMLpp",e.getMessage());
    }catch (IOException e){
        Log.d("XMLpp",e.getMessage());
    }    

`

编辑:

每个“值”开始标签都有自己的属性(column=...),我如何访问这些?

例如:要访问一行,我有一个值为“row”的字符串常量,并检查开始标签是否对应于它,它是否有效。但是当我声明一个值为“value column=\"Check\" null=\"false\""的字符串常量(我必须使用\ other wise“给出错误)时,它没有找到那个开始标签。那又怎样我的常数应该是?

4

3 回答 3

1

如果我正确理解了您的问题,那么要获取您需要执行的每个值,基本上您想要获取 xml 标记内每个属性的值

          int attributeCount = xpp.getAttributeCount();
        for(int i = 0; i<attributeCount; i++){
            String name = xpp.getAttributeName(i);
            //Log.d(TAG, "Name: "+name);
            if(name != null && name.equalsIgnoreCase("column")){

                return Integer.parseInt(xpp.getAttributeValue(i));                
            }
        }

因此,一旦您遇到该行,然后在找到它后查找开始标记“值”,然后使用上面的代码获取属性的各个值。

根据您的评论,如果您想获取 XML 标记的文本值,则必须使用 getText() 方法。找到 START_TAG 值后,执行以下代码:

                    eventType = xpp.next();
                    if(eventType == XmlPullParser.TEXT){
                        String text = xpp.getText();                              
                    }

对于 xml 标签 'INFO' 值,它将返回 'INFO'

于 2013-03-26T10:43:27.153 回答
1
    try {
        final Service S = new Service();
        String xmlString = S.ImportAllPollBoothStatus(IMEI,asscd, boothno);
        if(xmlString.toLowerCase().trim().equals("false")){
            return false;
        }
        DocumentBuilderFactory docFactory = DocumentBuilderFactory
                .newInstance();
        DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
        InputSource is = new InputSource();
        is.setCharacterStream(new StringReader(xmlString));
        Document doc = docBuilder.parse(is);

        NodeList nodes = doc.getElementsByTagName("HT");

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

            Element element = (Element) nodes.item(i);
            NodeList blockidnodes = doc.getElementsByTagName("Table");

            for (int blockidcount = 0; blockidcount < blockidnodes
                    .getLength(); blockidcount++) {

                NodeList PollpercentId = element
                        .getElementsByTagName("PollpercentId");
                Element line1 = (Element) PollpercentId.item(blockidcount);

                NodeList asscd1 = element
                        .getElementsByTagName("asscd");
                Element line2 = (Element) asscd1.item(blockidcount);

                NodeList pollgcd = element
                        .getElementsByTagName("pollgcd");
                Element line3 = (Element) pollgcd.item(blockidcount);


                NodeList SessionYearIdref = element
                        .getElementsByTagName("SessionYearIdref");
                Element line4 = (Element) SessionYearIdref.item(blockidcount);

                NodeList MaleVoters = element
                        .getElementsByTagName("MaleVoters");
                Element line5 = (Element) MaleVoters.item(blockidcount);

                NodeList FemaleVoters = element
                        .getElementsByTagName("FemaleVoters");
                Element line6 = (Element) FemaleVoters.item(blockidcount);

                NodeList UpdatedDate = element
                        .getElementsByTagName("UpdatedDate");
                Element line7 = (Element) UpdatedDate.item(blockidcount);

                NodeList timeslot = element
                        .getElementsByTagName("timeslot");
                Element line8 = (Element) timeslot.item(blockidcount);


            this.db.insertVotingStatus(
                        getCharacterDataFromElement(line1),
                        getCharacterDataFromElement(line2),
                        getCharacterDataFromElement(line3),
                        getCharacterDataFromElement(line4),
                        getCharacterDataFromElement(line5),
                        getCharacterDataFromElement(line6),
                        getCharacterDataFromElement(line7),
                        getCharacterDataFromElement(line8));

            }
        }


    }
     catch (Exception e) {
        Log.e("EXCEPTION DURING VIDHANSABHA INSERION",
                "======Insert-VIDHANSABHA-DETAILS=====================" + e);
        return false;
    }
    return true;
于 2013-12-05T10:24:25.543 回答
0

您是否开发了生成 XML 文件的应用程序?如果是这样,你为什么不改变它?如果 XML 具有以下格式,则解析 XML 会容易得多:

<item Index="1" Front="INFO" Back="INFO" Check="0"/>
<item Index="2" Front="INFO" Back="INFO" Check="1"/>
于 2013-03-26T10:47:32.677 回答