我从 sd 卡中有这种格式的 xml 文件。
<consignments creationDate="2015-05-11 08:04:38">
<consignment iid="142435846">
<consignmentId>194556772</consignmentId>
<orderCode>MUC-EC-4556772</orderCode>
<pickupDate>2015-04-01</pickupDate>
<referenceConsignor>236847.1</referenceConsignor>
<consignorCountry>DE</consignorCountry>
<consignorZip>83125</consignorZip>
<consignorCity>EGGSTAETT</consignorCity>
</consignment>
如何从 sd 卡中读取这个 xml 文件并将所有项目的结果显示到 textview 中?
最佳答案
这样就可以解析了。
try {
File file = new File("mnt/sdcard/xxx.xml");
InputStream is = new FileInputStream(file.getPath());
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(new InputSource(is));
doc.getDocumentElement().normalize();
NodeList nodelist = doc.getElementsByTagName("consignment");
Node node = nodeList.item(0);
Element fstElmnt = (Element) node;
String id=fstElmnt.getAttribute("iid");
String consignmentId=(String)fstElmnt.Element("consignmentId");
String orderCode=(String)fstElmnt.Element("orderCode");
}
catch (Exception e)
{
System.out.println("XML Pasing Excpetion = " + e);
}
关于java - 如何从 Android 应用程序的 SD 卡解析 XML?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30205501/