我真的很讨厌问题。我一直在寻找谷歌和stackoverflow的决心,试图解决它,但我尝试了很多解决方案,它仍然无法正常工作。
我想使用DocumentBuilder从XML文件中读取XML数据。
这是我的代码:
public void LoadFromFile(){
try{
//File fXmlFile = new File("fishlist.xml");
DocumentBuilderFactory dbFactory =
DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse("file:///C:/fishlist.xml"); // <--- there is exception run
doc.getDocumentElement().normalize();
}catch(Exception ex){
ex.printStackTrace();
}
}
当我尝试运行此方法时,它会显示异常:
W/System.err: java.io.FileNotFoundException: /C:/fishlist.xml: open failed:
ENOENT (No such file or directory)
W/System.err: at libcore.io.IoBridge.open(IoBridge.java:456)
W/System.err: at java.io.FileInputStream.<init>(FileInputStream.java:76)
W/System.err: at
libcore.net.url.FileURLConnection.connect(FileURLConnection.java:123)
W/System.err: at org.apache.harmony.xml.parsers.DocumentBuilderImpl.parse(DocumentBuilderImpl.jav
a:117)
W/System.err: at
javax.xml.parsers.DocumentBuilder.parse(DocumentBuilder.java:155)
W/System.err: Caused by: android.system.ErrnoException: open failed: ENOENT (No such file or directory)
W/System.err: at libcore.io.Posix.open(Native Method)
W/System.err: at libcore.io.BlockGuardOs.open(BlockGuardOs.java:186)
W/System.err: at libcore.io.IoBridge.open(IoBridge.java:442)
是的。我有这个方向的文件。请帮忙。
答案 0 :(得分:0)
要阅读的数据
<?xml version="1.0"?>
<company>
<staff id="1001">
<firstname>yong</firstname>
<lastname>mook kim</lastname>
<nickname>mkyong</nickname>
<salary>100000</salary>
</staff>
<staff id="2001">
<firstname>low</firstname>
<lastname>yin fong</lastname>
<nickname>fong fong</nickname>
<salary>200000</salary>
</staff>
</company>
尝试使用此代码的java代码
try {
File fXmlFile = new File("/Users/mkyong/staff.xml");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(fXmlFile);
//optional, but recommended
//read this - http://stackoverflow.com/questions/13786607/normalization-in-dom-parsing-with-java-how-does-it-work
doc.getDocumentElement().normalize();
System.out.println("Root element :" + doc.getDocumentElement().getNodeName());
NodeList nList = doc.getElementsByTagName("staff");
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("Staff id : " + eElement.getAttribute("id"));
System.out.println("First Name : " + eElement.getElementsByTagName("firstname").item(0).getTextContent());
System.out.println("Last Name : " + eElement.getElementsByTagName("lastname").item(0).getTextContent());
System.out.println("Nick Name : " + eElement.getElementsByTagName("nickname").item(0).getTextContent());
System.out.println("Salary : " + eElement.getElementsByTagName("salary").item(0).getTextContent());
}
}
} catch (Exception e) {
e.printStackTrace();
}
Current Element :staff
Staff id : 1001
First Name : yong
Last Name : mook kim
Nick Name : mkyong
Salary : 100000
Current Element :staff
Staff id : 2001
First Name : low
Last Name : yin fong
Nick Name : fong fong
Salary : 200000
答案 1 :(得分:0)
过去我的一个潜在解决方法是为文件创建一个File对象。
File xmlFile = new File("C:/fishlist.xml");
然后,tou可以做以下两件事之一:
继续像往常一样发送uri:
Document doc = dBuilder.parse(xmlFile.toURI().toString);