Android XMLPullParser вложенные атрибуты с тем же именем - PullRequest
0 голосов
/ 19 апреля 2019

Я анализирую файл XML в экземпляр класса. Один предмет выглядит так:

<item>
  <g:id>xx</g:id>
  <title>xxx</title>
  <description>xxx</description>
  <g:product_type>xxx</g:product_type>
  <link>xx</link>
  <g:image_link>xxx</g:image_link>
  <g:condition>xxx</g:condition>
  <g:availability>xxx</g:availability>
  <g:price>100</g:price>
  <g:brand>xxx</g:brand>
  <g:gtin/>
  <g:mpn>xx</g:mpn>
  <g:shipping>
    <g:country>xx</g:country>
    <g:service>xx</g:service>
    <g:price>10</g:price>
  </g:shipping>
  <pubDate>xxx</pubDate>
</item>

Метод разбора:

if (xpp.getName().equalsIgnoreCase("item")) {
                        insideItem = true;
                        i = new Item();

                    } else if (xpp.getName().equalsIgnoreCase("g:id")) {
                        if (insideItem)
                            i.setmID(xpp.nextText());
                    } else if (xpp.getName().equalsIgnoreCase("title")) {
                        if (insideItem)
                            i.setmTitle(xpp.nextText());
                    } else if (xpp.getName().equalsIgnoreCase("description")) {
                        if (insideItem)
                            i.setmDescription(xpp.nextText());
                    } else if (xpp.getName().equalsIgnoreCase("g:product_type")) {
                        if (insideItem)
                            i.setmProductType(xpp.nextText());
                    } else if (xpp.getName().equalsIgnoreCase("g:image_link")) {
                        if (insideItem)
                            i.setmPictureLink(xpp.nextText());
                    } else if (xpp.getName().equalsIgnoreCase("g:condition")) {
                        if (insideItem)
                            i.setmCondition(xpp.nextText());
                    } else if (xpp.getName().equalsIgnoreCase("g:availability")) {
                        if (insideItem)
                            i.setmAvailability(xpp.nextText());
                    } else if (xpp.getName().equalsIgnoreCase("g:price")) {
                        if (insideItem)
                            i.setmPrice(xpp.nextText());
                    } else if (xpp.getName().equalsIgnoreCase("g:brand")) {
                        if (insideItem)
                            i.setmBrand(xpp.nextText());
                    } else if (xpp.getName().equalsIgnoreCase("g:mpn")) {
                        if (insideItem)
                            i.setmMpn(xpp.nextText());
                    } else if (xpp.getName().equalsIgnoreCase("g:country")) {
                        if (insideItem)
                            i.setmShippingCountry(xpp.nextText());
                    } else if (xpp.getName().equalsIgnoreCase("g:service")) {
                        if (insideItem)
                            i.setmService(xpp.nextText());
                    } else if (xpp.getName().equalsIgnoreCase("g:price")) {
                        if (insideItem)
                            i.setmShippingCosts(xpp.nextText());
                    } else if (xpp.getName().equalsIgnoreCase("g:pubDate")) {
                        if (insideItem)
                            i.setMpubDate(xpp.nextText());
                    }

Я разбираю каждый атрибут XML-файла в поле класса. Но когда дело доходит до разбора атрибута "g: price", у меня возникают некоторые проблемы. Я знаю, это потому, что у меня есть два свойства с одинаковым именем.

Что является подходящим способом отличить цену товара от цены? Кто-нибудь может дать мне идею, как изменить метод?

Как замечание, я знаю, что было бы более эффективно использовать оператор switch, просто хотел получить некоторый опыт работы с xml.

...