1

How to read this amazonaws s3 XML in android using xml parser. i user parser but now working and provide null value. how to resolve this issue thanks in advance

AWS S3 XML_FILE:

<ListBucketResult xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
   <Name>myMp3</Name>
   <Prefix />
   <Marker />
   <MaxKeys>1000</MaxKeys>
   <IsTruncated>false</IsTruncated>
   <Contents>
      <Key>Jumykhutby2019/</Key>
      <LastModified>2019-02-10T11:11:20.000Z</LastModified>
      <ETag>"d41d8cd98f00b204e9800998ecf8427e"</ETag>
      <Size>0</Size>
      <StorageClass>STANDARD</StorageClass>
   </Contents>
   <Contents>
      <Key>Jumykhutby2019/002  5 اکتوبر 2018 شیخ امین .MP3</Key>
      <LastModified>2019-02-13T19:44:25.000Z</LastModified>
      <ETag>"d0f7c653c582b89e14776d457ab4fcdb-6"</ETag>
      <Size>47186907</Size>
      <StorageClass>STANDARD</StorageClass>
   </Contents>
   <Contents>
      <Key>Jumykhutby2019/juma01.mp3</Key>
      <LastModified>2019-02-13T21:49:37.000Z</LastModified>
      <ETag>"a7bae0567c8e53f2074fffd95dce5404-3"</ETag>
      <Size>42532595</Size>
      <Owner>
         <ID>3858f4d0f71aa7b7411c83175a73742c9ebed9bda893722d34877751e84e5f02</ID>
      </Owner>
      <StorageClass>STANDARD</StorageClass>
   </Contents>

</ListBucketResult>

Java code:

static final String KEY_ITEM = "Contents";
static final String KEY_ID = "Key";
static final String KEY_NAME = "LastModified";
static final String KEY_COST = "Size";
static final String KEY_DESC = "StorageClass";

Method 1:

ArrayList<HashMap<String, String>> menuItems = new ArrayList<HashMap<String, String>>(); 
XMLParser parser = new XMLParser();
String xml = parser.getXmlFromUrl(XML_FILE); // getting XML

Document doc = parser.getDomElement(xml);
NodeList nl = doc.getElementsByTagName(KEY_ITEM);
for (int i = 0; i < nl.getLength(); i++) {
    HashMap<String, String> map = new HashMap<String, String>();
    Element e = (Element) nl.item(i);
    map.put(KEY_ID, parser.getValue(e, KEY_ID));
    map.put(KEY_NAME, parser.getValue(e, KEY_NAME));
    map.put(KEY_COST, "Rs." + parser.getValue(e, KEY_COST));
    map.put(KEY_DESC, parser.getValue(e, KEY_DESC));
    // adding HashList to ArrayList
    menuItems.add(map);
}
Log.i("OutPut",menuItems.toString() );

Method 2:

 ArrayList<MeanSetGet> list;
 URL URL = new URL(wording[0]);
 XMLReader mXMLReader = SAXParserFactory.newInstance().newSAXParser().getXMLReader();;
 Log.e("OutPut", mXMLReader.toString());

XML Parser:

class XMLParser {
    XMLParser() {
    }

    String getXmlFromUrl(String url) {
        String xml = null;
        try {
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(url);
            HttpResponse httpResponse = httpClient.execute(httpPost);
            HttpEntity httpEntity = httpResponse.getEntity();
            xml = EntityUtils.toString(httpEntity);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return xml;
    }

    Document getDomElement(String xml) {
        Document doc = null;
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        try {
            DocumentBuilder db = dbf.newDocumentBuilder();
            InputSource is = new InputSource();
            is.setEncoding("ISO-8859-1");
            is.setCharacterStream(new StringReader(xml));
            doc = db.parse(is);
        } catch (Exception e) {
            Log.e("Error: ", e.getMessage());
            return null;
        }
        return doc;
    }

    private String getElementValue(Node elem) {
        Node child;
        if (elem != null) {
            if (elem.hasChildNodes()) {
                for (child = elem.getFirstChild(); child != null; child = child.getNextSibling()) {
                    if (child.getNodeType() == Node.TEXT_NODE) {
                        return child.getNodeValue();
                    }
                }
            }
        }
        return "";
    }

    String getValue(Element item, String str) {
        NodeList n = item.getElementsByTagName(str);
        return this.getElementValue(n.item(0));
    }
}

Dependency i used:

   implementation 'org.apache.httpcomponents:httpcore:4.4.1'

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.