Specific attribute with Xmlpullparser

Asked

Viewed 29 times

1

Good afternoon, everyone,

I’m trying to consume an XML file from a Url. So far so good.

Only I am consuming by the tag "img" and the attribute "src". Only the tag "img" appears twice and each one has its "src".

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

How do I set this specific attribute?

Follow example of XML and later 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();
    }
  • Guys got it done, it’s edited into the question!

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.