简体   繁体   中英

Android :Parse XML Data: Tags with arguments

I want to parse an XML file i exported from a database software, for my android app. However some of the tags have arguments like so:

<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>

what string value do i specify while trying to find the start tag to parse it? ( for example: to find the row i compare the start tap to "row" and if it returns true i compute the data. What do i do for each value ie Index,Front,Back and Check separately?)

My java code is as follows

   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());
    }    

`

EDIT:

Each "value" start tags has its own attribute(column=...), how do i access those?

For example: to access a row, i have a String constant with the value "row", and check if the start tag corresponds to that, and it works. But when i declare a string constant with value "value column=\\"Check\\" null=\\"false\\""( i have to use \\ other wise " give errors), it does not find find that start tag. So what should my constant be?

if i understand your question correctly then to get each of the values you need to do following, basically you want to get the value of each attribute inside the xml tag

          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));                
            }
        }

So once you have encountered the row then you look for the start Tag "value" once you have found it, then use the above code to get the individual value of attributes.

As per your comment if you want to get the text value of an XML tag then you will have to use the getText() method. Once you have found the START_TAG value then execute below code:

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

For the xml tag 'INFO' value it will return 'INFO'

Did you develop the app which generates the XML file? If so why don't you change it? It would be much easier to parse the XML if it has this format:

<item Index="1" Front="INFO" Back="INFO" Check="0"/>
<item Index="2" Front="INFO" Back="INFO" Check="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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM