如何从 android 中的 xml 标签获取值

how to get value from xml tag in android

我做了一个解析器,因为我正在获取那个 xml 标签的所有值,但是我想获取标签内的一些值作为参数,我不知道如何获取它来自标签,样本xml如下: sample.xml

  <ChargeableRateInfo averageBaseRate="226.7" averageRate="192.69" 
commissionableUsdTotal="192.69" currencyCode="USD" maxNightlyRate="192.69" 
nightlyRateTotal="192.69" surchargeTotal="46.8" total="239.49">
<NightlyRatesPerRoom size="1"><NightlyRate baseRate="226.7" 
rate="192.69" promo="true"/></NightlyRatesPerRoom>

,我想获取"averageRate","nightlyRateTotal","total"的值,但我不知道如何获取该数据?我的解析器如下:

while (eventType != XmlPullParser.END_DOCUMENT && !done) {
                tagName = parser.getName();

                switch (eventType) {
                case XmlPullParser.START_DOCUMENT:
                    break;
                case XmlPullParser.START_TAG:
                    if (tagName.equals(ITEM)) {
                        rssFeed = new RSSFeed_SelectedHotelResult();

                    }

                    if (tagName.equals(DESCRIPTION)) {
                        name = parser.nextText().toString();
                        hotel_name_list.add(name);
                        System.out
                                .println(":::::::::::::::::DescriptionL:lLLLLLLLLLL"
                                        + name);
                    }
                    if (tagName.equals(OFFER)) {
                        offer = parser.nextText().toString();
                        hotel_offer_list.add(offer);
                        System.out.println(":::::::::::::::::Offer" + offer);
                    }

                    if (tagName.equals(HOTEL_IMG)) {
                        thumbNailUrl = parser.nextText().toString();
                        hotel_image_list.add(thumbNailUrl);
                    }
                    if (tagName.equals(HOTEL_LOCATION)) {
                        locationDescription = parser.nextText().toString();
                        hotel_lOCATION_DESC.add(locationDescription);
                        System.out
                                .println("::::::::;Location deccription:::::::::::;;"
                                        + locationDescription);
                    }

                    if (tagName.equals(HOTEL_RATE_IMG)) {
                        tripAdvisorRatingUrl = parser.nextText().toString();
                    }

                    if (tagName.equals(HOTEL_PRICE)) {
                        lowRate = parser.nextText().toString();
                    }

                    if (tagName.equals(HOTEL_RATING)) {
                        hotelRating = parser.nextText().toString();
                        hotel_rate_list.add(hotelRating);
                    }

                    if (tagName.equals(HOTEL_ADDRESS)) {
                        address1 = parser.nextText().toString();
                        hotel_address_list.add(address1);
                        System.out
                                .println(":::::::::::::hotel address:::::::::::"
                                        + address1);
                    }
                    if (tagName.equals(DESTINATION_ID)) {
                        destinationId = parser.nextText().toString();
                        dest_id_list.add(destinationId);
                        System.out
                                .println("*********Here Is DestinationID:************"
                                        + destinationId);
                    }
                    if (tagName.equals(CITY)) {
                        city = parser.nextText().toString();
                        city_list.add(city);
                        System.out.println("*********Here Is City:************"
                                + city);
                    }
                    if (tagName.equals(HOTEL_ID)) {
                        hotelId = parser.nextText().toString();
                        hotel_id_list.add(hotelId);
                        System.out
                                .println("*********Here Is HotelID:************"
                                        + hotelId);
                    }
                    if (tagName.equals(LATITUDE)) {
                        latitude = parser.nextText().toString();
                        latitude_list.add(latitude);
                        System.out
                                .println("*********Here Is Latitude:************"
                                        + hotelId);
                    }
                    if (tagName.equals(LONGITUDE)) {
                        longitude = parser.nextText().toString();
                        longitude_list.add(longitude);
                        System.out
                                .println("*********Here Is Longitude:************"
                                        + hotelId);
                    }

                    break;
                case XmlPullParser.END_TAG:
                    if (tagName.equals(CHANNEL)) {
                        done = true;
                    } else if (tagName.equals(ITEM)) {

                        rssFeed = new RSSFeed_SelectedHotelResult(name,
                                thumbNailUrl, tripAdvisorRatingUrl,
                                hotelRating, locationDescription, lowRate,
                                destinationId, city, hotelId, latitude,
                                longitude, offer);
                        rssFeedList.add(rssFeed);

                    }

                    break;
                }
                eventType = parser.next();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return rssFeedList;

    }

尝试...parser.getAttributeValue(null, "averageRate");

您可以使用 DOM Parser 代替。

示例

Document doc = parser.getDomElement(YOUR_XML);
 String averageBaseRate= doc.getElementsByTagName("ChargeableRateInfo ").item(0).getAttributes().getNamedItem("averageBaseRate").getNodeValue();
 String averageRate= doc.getElementsByTagName("ChargeableRateInfo ").item(1).getAttributes().getNamedItem("averageRate").getNodeValue();
 ...

TAG_ITEM = "ChargeableRateInfo";

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

     xpp.setInput(new StringReader ("your_xml/"));
     int eventType = xpp.getEventType();
     while (eventType != XmlPullParser.END_DOCUMENT) {
      if(eventType == XmlPullParser.START_DOCUMENT) {
          System.out.println("Start document");
      } else if(eventType == XmlPullParser.END_DOCUMENT) {
          System.out.println("End document");
      } else if(eventType == XmlPullParser.START_TAG) {
          System.out.println("Start tag "+xpp.getName());

// For Attribute 
String tag = parser.getName();
    if (TAG_ITEM.equals(tag)) {

        Log.d(LOG_NAME, parser.getAttributeValue(null, "averageBaseRate"); );


        Log.d(LOG_NAME, parser.getAttributeValue(null, "averageRate"); );


        Log.d(LOG_NAME, parser.getAttributeValue(null, "commissionableUsdTotal"); );


        Log.d(LOG_NAME, parser.getAttributeValue(null, "currencyCode"); );
    }   


      } else if(eventType == XmlPullParser.END_TAG) {
          System.out.println("End tag "+xpp.getName());
      } else if(eventType == XmlPullParser.TEXT) {
          System.out.println("Text "+xpp.getText());
      }
      eventType = xpp.next();
     }