Specific attribute with XMLPullParser

1

Good afternoon guys,

I'm trying to consume an XML file from a url. So far so good.

I'm only consuming the "img" tag and the "src" attribute. Except that the "img" tag appears twice and each has its "src".

the "src" of the first tag that interests me.

How do I set this specific attribute?

Following is an example of the XML and later the code.

<book bgcolor="0xFFFFFF" pageheight="517" pagewidth="450" lang="pt" navigation="true" barpos="_705" zoomalign="top center" zoominit="1" bggradient="true">
  <chapter>
    <page anchor="cover">
      <img src="../../../../../lib/upload/preview.php?codsisupload=7186" aa="true" width="450" height="517"/>
      <img src="src/blank.gif" left="0" top="0" hires="../../../../../lib/upload/preview.php?codsisupload=7185" iconpos="left" gallery="confianca" overlay="none" width="830" height="955"/>
    </page>
    <page>
      <img src="../../../../../lib/upload/preview.php?codsisupload=7188" aa="true" width="450" height="517"/>
      <img src="src/blank.gif" left="0" top="0" hires="../../../../../lib/upload/preview.php?codsisupload=7187" iconpos="left" gallery="confianca" overlay="none" width="830" height="955"/>
    </page>
    <page>
      <img src="../../../../../lib/upload/preview.php?codsisupload=7190" aa="true" width="450" height="517"/>
      <img src="src/blank.gif" left="0" top="0" hires="../../../../../lib/upload/preview.php?codsisupload=7189" iconpos="left" gallery="confianca" overlay="none" width="830" height="955"/>
    </page>
    <page>
      <img src="../../../../../lib/upload/preview.php?codsisupload=7192" aa="true" width="450" height="517"/>
      <img src="src/blank.gif" left="0" top="0" hires="../../../../../lib/upload/preview.php?codsisupload=7191" iconpos="left" gallery="confianca" overlay="none" width="830" height="955"/>
    </page>
  </chapter>
</book>
try {
        factory = XmlPullParserFactory.newInstance();
        factory.setNamespaceAware(true);
        XmlPullParser xpp = factory.newPullParser();

        URL url = new URL(XMLFile);
        URLConnection conn = url.openConnection();
        InputStream input = conn.getInputStream();

        xpp.setInput(input, null);

        int eventType = xpp.getEventType();

        while (eventType != XmlPullParser.END_DOCUMENT) {

            if (eventType == XmlPullParser.START_TAG) {
                if (xpp.getName().equals("img")) {
                    image = new ImageModel();
                    image.setUrl(xpp.getAttributeValue(null, "src"));
                }
            } else if (eventType == XmlPullParser.END_TAG) {
                if (xpp.getName().equals("img")) {
                    data.add(image);
                }
            }
            eventType = xpp.next();
        }
    } catch (XmlPullParserException | IOException e) {
        e.printStackTrace();
    }

Solution

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

        URL url = new URL(XMLFile);
        URLConnection conn = url.openConnection();
        InputStream input = conn.getInputStream();

        xpp.setInput(input, null);

        int eventType = xpp.getEventType();

        while (eventType != XmlPullParser.END_DOCUMENT) {
            if (eventType == XmlPullParser.START_TAG && xpp.getName().equalsIgnoreCase("img")
                    && Integer.parseInt(xpp.getAttributeValue(null, "width")) == 450
                    && Integer.parseInt(xpp.getAttributeValue(null, "height")) == 517){

                    image = new ImageModel();
                    image.setUrl(xpp.getAttributeValue(null, "src"));
                    data.add(image);
            }
            eventType = xpp.next();
        }
    } catch (XmlPullParserException | IOException e) {
        e.printStackTrace();
    }
    
asked by anonymous 19.01.2016 / 18:54

0 answers