如何解析SD应用程序的SD卡?

时间:2022-09-19 09:00:32

I have xml file this format from sd card.

我有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>

how to read this xml file from sd card and show result all items into textview?

如何从SD卡读取此xml文件并将所有项目显示到textview中?

2 个解决方案

#1


like this you can parse.

像这样你可以解析。

 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);
    }

#2


To pass your file in to be read you will need to do first get your file:

要传递您的文件以进行阅读,您需要先获取文件:

String sdcardDir = Environment.getExternalStorageDirectory().getAbsolutePath;
File fileToRead = new File(dir, "path/to/file");
InputStream stream = new FileInputStream(fileToRead);

See the following site on how to parse XML: http://developer.android.com/training/basics/network-ops/xml.html

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

With the inputstream you can now pass it to your parser from the link.

使用输入流,您现在可以从链接将其传递给解析器。

EDIT: You can add it to your textview by adding it to your layout then in your activity/fragment find the view and call .setText(String).

编辑:您可以将它添加到您的textview,然后在您的activity / fragment中找到视图并调用.setText(String)。

#1


like this you can parse.

像这样你可以解析。

 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);
    }

#2


To pass your file in to be read you will need to do first get your file:

要传递您的文件以进行阅读,您需要先获取文件:

String sdcardDir = Environment.getExternalStorageDirectory().getAbsolutePath;
File fileToRead = new File(dir, "path/to/file");
InputStream stream = new FileInputStream(fileToRead);

See the following site on how to parse XML: http://developer.android.com/training/basics/network-ops/xml.html

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

With the inputstream you can now pass it to your parser from the link.

使用输入流,您现在可以从链接将其传递给解析器。

EDIT: You can add it to your textview by adding it to your layout then in your activity/fragment find the view and call .setText(String).

编辑:您可以将它添加到您的textview,然后在您的activity / fragment中找到视图并调用.setText(String)。