读取XML文件
问题描述:
我使用下面的代码读取名为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)
答
读我只是改变了我的文件的路径为“Environment.getExternalStorageDirectory(),ContactFile.xml”。所以它现在是:File XmlFile = new File(Environment.getExternalStorageDirectory(),“newfile/contactFile/ContactFile.xml”);
现在,它的工作。
答
我不知道你在干什么,编号
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
}
您无法从Android上以这种方式访问PC上的文件 – nikis
您需要使用DDMS推送xml文件,并更好地使用android提供的[XmlPullParser](http://developer.android.com/reference /org/xmlpull/v1/XmlPullParser.html) – ritesht93
您是否知道json – 2014-03-24 11:58:05