2014-03-24 206 views
0

我使用下面的代碼讀取名爲ContactFile一個XML文件,我在C:目錄讀取XML文件

public class MainActivity extends Activity 
{ 
    @Override 
    protected void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     extract(null); 
    } 

    public static void extract(String argv[]) 
    {  
     try 
     { 
      File XmlFile = new File("C:\\ResourceFile\\ContactFile.xml"); 
      DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); 
      DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); 
      Document doc = dBuilder.parse(XmlFile); 
      doc.getDocumentElement().normalize();  
      System.out.println("Root element :" + doc.getDocumentElement().getNodeName()); 
      NodeList nList = doc.getElementsByTagName("ExtractContact"); 
      System.out.println("----------------------------"); 

      for (int temp = 0; temp < nList.getLength(); temp++) { 
       Node nNode = nList.item(temp); 
       System.out.println("\nCurrent Element :" + nNode.getNodeName());  
       if (nNode.getNodeType() == Node.ELEMENT_NODE) 
       { 
        Element eElement = (Element) nNode; 
        System.out.println("id : " + eElement.getAttribute("id")); 
        System.out.println("Name : " + eElement.getElementsByTagName("name").item(0).getTextContent()); 
        System.out.println("Phone Number : " + eElement.getElementsByTagName("phoneNumber").item(0).getTextContent()); 
       } 
      } 
     } 
     catch (Exception e) 
     { 
      e.printStackTrace(); 
     } 
    } 
} 

的XML文件保存的resourcefile有形式:

<Document> 
    <ExtractContact> 
     <id></id> 
     <name></name> 
     <phoneNumber /> 
    </ExtractContact> 
</Document> 

我我得到這個錯誤:

E/Trace(1596): error opening trace file: No such file or directory (2)

+0

您無法從Android上以這種方式訪問​​PC上的文件 – nikis

+0

您需要使用DDMS推送xml文件,並更好地使用android提供的[XmlPullParser](http://developer.android.com/reference /org/xmlpull/v1/XmlPullParser.html) – ritesht93

+0

您是否知道json – 2014-03-24 11:58:05

回答

0

讀我只是改變了我的文件的路徑爲「Environment.getExternalStorageDirectory(),ContactFile.xml」。所以它現在是:File XmlFile = new File(Environment.getExternalStorageDirectory(),「newfile/contactFile/ContactFile.xml」);

現在,它的工作。

0

我不知道你在幹什麼,編號

File XmlFile = new File("C:\\ResourceFile\\ContactFile.xml"); 

請在您的資產文件夾中有您的文件,並從那裏取回!

做如下

InputStream is = getResources().getAssets().open("ContactFile.xml"); 
String result= convertStreamToString(is); 

獲取的字符串,我們可以寫convertStreamToString方法如下

public static String convertStreamToString(InputStream is) 
      throws IOException { 
      Writer writer = new StringWriter(); 
     char[] buffer = new char[2048]; 
     try { 
      Reader reader = new BufferedReader(new InputStreamReader(is, 
        "UTF-8")); 
      int n; 
      while ((n = reader.read(buffer)) != -1) { 
       writer.write(buffer, 0, n); 
      } 
     } finally { 
      is.close(); 
     } 
     String text = writer.toString(); 
     return text; 
} 

的情況下,如果該文件是在SD卡如下

比閱讀文件
File sdcard = Environment.getExternalStorageDirectory(); 

//Get the text file 
File file = new File(sdcard,"file.txt"); 

//Read text from file 
StringBuilder text = new StringBuilder(); 

try { 
    BufferedReader br = new BufferedReader(new FileReader(file)); 
    String line; 

    while ((line = br.readLine()) != null) { 
     text.append(line); 
     text.append('\n'); 
    } 
} 
catch (IOException e) { 
    //You'll need to add proper error handling here 
} 
+0

如果xml文件保存在手機的SD卡或根目錄中,我必須放置哪條路徑? – Marya

+0

只需將ContactFile.xml放入您的android項目的資產文件夾中,並使用我給予您的代碼! –

+0

實際上ContactFile.xml是在手機的SD卡中創建的,所以我不能把它放在你說的其他地方(資產文件夾爲例)。我需要從SD卡讀取它 – Marya

0

Here你可以找到如何從資產中讀取文件。

here如何從原始