程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> JSP編程 >> 關於JSP >> jsp 增加,刪除,讀取xml文件中指定標記的內容代碼

jsp 增加,刪除,讀取xml文件中指定標記的內容代碼

編輯:關於JSP

jsp 增加,刪除,讀取xml文件中指定標記的內容代碼

網頁特效p/jsp.html target=_blank >jsp教程 增加,刪除,讀取xml文件中指定標記的內容代碼

package xml;

import java.io.file;
import java.util.list;

import org.dom4j.document;
import org.dom4j.documentexception;
import org.dom4j.element;
import org.dom4j.qname;
import org.dom4j.io.saxreader;

public class dom4jtest {

    private saxreader reader;
    private document document;
    private static string path = "c:/dom4jsample1.xml";

    public dom4jtest(string str) throws documentexception {
        reader = new saxreader();
        document = reader.read(new file(str));
    }

    public static void main(string[] args) throws documentexception {
        // todo auto-generated method stub
        dom4jtest test = new dom4jtest(path);   
        test.getinfo();

    }

    public void getinfo() {
        element root = document.getrootelement();

        qname qname = new qname("ip");
        list<element> list = root.elements(qname);

        for (int i = 0; i < list.size(); i++) {
            system.out.println(list.get(i).gettext());
        }

    }

}

public void add(string add) throws ioexception {
 element root = document.getrootelement();
 element e = root.addelement("ip");
 e.settext(add);
 writefile(document);
}

    // 寫入文件
private void writefile(document doc) throws ioexception {
 filewriter out = new filewriter(path);
 doc.write(out);
 out.flush();
 out.close();
}

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved