首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何从android应用程序的SD卡中解析XML?

如何从android应用程序的SD卡中解析XML?
EN

Stack Overflow用户
提问于 2015-05-13 12:06:35
回答 2查看 404关注 0票数 2

我有来自SD卡的这种格式的xml文件。

代码语言:javascript
复制
<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中?

EN

回答 2

Stack Overflow用户

发布于 2015-05-13 13:04:40

就像这样,你可以解析。

代码语言:javascript
复制
 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);
    }
票数 1
EN

Stack Overflow用户

发布于 2015-05-13 12:45:16

要传递您的文件以便读取,您需要首先获取您的文件:

代码语言:javascript
复制
String sdcardDir = Environment.getExternalStorageDirectory().getAbsolutePath;
File fileToRead = new File(dir, "path/to/file");
InputStream stream = new FileInputStream(fileToRead);

有关如何解析XML的信息,请参阅以下站点:http://developer.android.com/training/basics/network-ops/xml.html

有了inputstream,您现在可以将它从链接传递给解析器。

编辑:您可以将其添加到您的文本视图中,方法是将其添加到您的布局中,然后在您的活动/片段中找到该视图并调用.setText(String)。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30205501

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档