如何使用xml解析器在Android中阅读此amazonaws s3 XML。 我使用了解析器,但现在可以正常工作并提供空值。如何解决此问题,谢谢您
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代码:
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";
方法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() );
方法2:
ArrayList<MeanSetGet> list;
URL URL = new URL(wording[0]);
XMLReader mXMLReader = SAXParserFactory.newInstance().newSAXParser().getXMLReader();;
Log.e("OutPut", mXMLReader.toString());
XML解析器:
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));
}
}
我使用的依赖性:
implementation 'org.apache.httpcomponents:httpcore:4.4.1'