1. 程式人生 > >linux下用dom4j寫入xml時 報錯java.io.FileNotFoundException..(沒有那個檔案或目錄

linux下用dom4j寫入xml時 報錯java.io.FileNotFoundException..(沒有那個檔案或目錄

今天在使用dom4j寫入xml檔案是報錯:java.io.FileNotFoundException..(沒有那個檔案或目錄)

但是我去讀取檔案時卻是正常的,這時我想到了linux下的許可權問題,所一我改了許可權,再試,還是這個錯誤,百度一下,發現這種錯誤大多是路徑不對或者檔名錯誤還有可能是檔案不存在,在讀取的時候卻是正常的,這就可以排除這些問題了啊。但是我還是檢查了一遍路徑,發現,我的workspace路徑下有空格我想肯定是這個問題了,於是將空格改為下劃線,呵呵,檔案載入不進來了,這下我想到的是改workspace,這個在file-->Switch WorkSpace-->Other,然後選擇修改後的路徑就ok了。

下面是我修改後的寫入xml的java程式碼:

package hnist.com.xml01;

import java.io.FileWriter;
import java.io.IOException;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;


public class xmlWrite {
	public static void main(String args[]){
		XMLWriter out = null;
		try {
			//先建立一個的document物件
			Document d = DocumentHelper.createDocument();
			//為d新增節點,並返回該節點
			Element root = d.addElement("users");
			//為根節點新增節點,並且返回新增的節點
			Element eu = root.addElement("user");
			//新增屬性
			eu.addAttribute("id", "1");
			//新增其它節點
			eu.addElement("name").addText("張三");
			eu.addElement("sex").addText("男");
			eu.addElement("age").addText("20");
			eu = root.addElement("user");
			eu.addAttribute("id", "2");
			eu.addElement("name").addText("李四");
			eu.addElement("sex").addText("女");
			eu.addElement("age").addText("19");
			eu = root.addElement("user");
			eu.addAttribute("id", "3");
			eu.addElement("name").addText("王五");
			eu.addElement("sex").addText("男");
			eu.addElement("age").addText("22");
			//獲取xml檔案所在的路徑
			String path = xmlWrite.class.getClassLoader().getResource("xml/user.xml").getPath();
			//專案中不用替換
			path = path.replace("bin", "src");
			System.out.println(path);
			//建立xmlWriter來寫資料
			out = new XMLWriter(new FileWriter(path), OutputFormat.createPrettyPrint());
			//將節點寫入到xml檔案中
			out.write(d);
			System.out.println("寫入成功!");
		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			try {
				if(out != null) out.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}


執行結果為:

/home/long/Workspaces/MyEclipse_Professional_2014/xml01/src/xml/user.xml
寫入成功!

user.xml中為:

<?xml version="1.0" encoding="UTF-8"?>

<users>
  <user id="1">
    <name>張三</name>
    <sex>男</sex>
    <age>20</age>
  </user>
  <user id="2">
    <name>李四</name>
    <sex>女</sex>
    <age>19</age>
  </user>
  <user id="3">
    <name>王五</name>
    <sex>男</sex>
    <age>22</age>
  </user>
</users>

讀取user.xml的java程式碼:
package hnist.com.xml01;

import java.util.List;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

public class xmlXpath {
	public static void main(String args[]){
		SAXReader reader = new SAXReader();
		try {
			String path = xml01.class.getClassLoader().getResource("xml/user.xml").getPath();
			System.out.println("path = "+path);
			Document d = reader.read(path);
			Element root = d.getRootElement();
			//相對路徑查詢
			List<Element> eles = root.selectNodes("user");
			System.out.println(eles);
			//絕對路徑查詢,從根節點users查詢user,而不會查詢user下的節點
			eles = root.selectNodes("/users/user");
			System.out.println("包含節點個數:"+eles.size()+" : "+eles);
			//查詢所有性別為男的姓名
			System.out.println("----------------------------------");
			eles = root.selectNodes("/users/user[sex='男']");
			for(Element e:eles){
				System.out.println("性別為男:"+e.elementText("name"));
			}
			System.out.println("----------------------------------");
			//查詢age<=20的姓名
			eles = root.selectNodes("/users/user[@id<=2]");
			for(Element e:eles){
				System.out.println("id<=2 :"+e.elementText("name"));
			}
			System.out.println("----------------------------------");
			//查詢名稱中包含有'張'的age節點
			eles = root.selectNodes("/users/user[contains(name,'張')]/age");
			for(Element e:eles){
				System.out.println("名稱中包含有'張'的age節點:"+e.getTextTrim());
			}
			System.out.println("----------------------------------");
			//查詢age<=20並且sex='女'的name
			eles = root.selectNodes("/users/user[age<=20 and sex='女']/name");
			for(Element e:eles){
				System.out.println("age<=20並且sex='女'的name :"+e.getTextTrim());
			}
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}
}

執行結果為:

path = /home/long/Workspaces/MyEclipse_Professional_2014/xml01/bin/xml/user.xml
[[email protected] [Element: <user attributes: [[email protected] [Attribute: name id value "1"]]/>], [email protected] [Element: <user attributes: [[email protected] [Attribute: name id value "2"]]/>], [email protected] [Element: <user attributes: [[email protected] [Attribute: name id value "3"]]/>]]
包含節點個數:3 : [[email protected] [Element: <user attributes: [[email protected] [Attribute: name id value "1"]]/>], [email protected] [Element: <user attributes: [[email protected] [Attribute: name id value "2"]]/>], [email protected] [Element: <user attributes: [[email protected] [Attribute: name id value "3"]]/>]]
----------------------------------
性別為男:張三
性別為男:王五
----------------------------------
id<=2 :張三
id<=2 :李四
----------------------------------
名稱中包含有'張'的age節點:20
----------------------------------
age<=20並且sex='女'的name :李四