📄 xmlreadwrite.java
字号:
package com.ntsky.xml;
import java.io.*;
import java.util.*;
import org.dom4j.*;
import org.dom4j.io.*;
import org.dom4j.dom.*;
import org.apache.log4j.Logger ;
import com.ntsky.xml.XMLPath;
import javax.servlet.http.*;
import java.net.URL;
/**
* <p>Title: Ntsky OpenSource BBS</p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2004</p>
* <p>Company: 天网文化发展有限公司</p>
* @author 姚君林
* @version 1.0
*/
public class XMLReadWrite extends HttpServlet{
private final static Logger logger = Logger.getLogger(XMLReadWrite.class);
private static XMLReadWrite xmlReadWrite = null;
private static Object obj = new Object();
private XMLReadWrite(){
}
/**
* 获取读取XML的实例
* @return XMLReader
*/
public static XMLReadWrite getInstance() {
if (xmlReadWrite == null) {
synchronized(obj){
if (xmlReadWrite == null) {
xmlReadWrite = new XMLReadWrite();
//logger.info("初始创建xmlReadWrite对象 :" + xmlReadWrite);
}
}
}
return xmlReadWrite;
}
/**
* 读取XML文件
* @param xmlPath String
* @throws DocumentException
* @throws Exception
* @return Document
*/
public Document read(String xmlPath) throws Exception {
SAXReader reader = new SAXReader();
Document doc = reader.read(new File(getRealPath(xmlPath)));
//logger.info("成功载入xml文件");
return doc;
}
/**
* 写xml
* @param args String[]
*/
public void write(Document document,String xmlPath) throws Exception{
//logger.info("xml的路径 : " +getRealPath(xmlPath));
XMLWriter writer = new XMLWriter(new FileWriter(getRealPath(xmlPath)),OutputFormat.createPrettyPrint());
writer.write(document);
writer.close();
}
/**
* 文件的相对路径
* @param path String
* @return String
*/
public String getRealPath(String path){
//logger.info("path = " + path);
URL url = null;
try{
url = this.getClass().getClassLoader().getResource(path); //com.ntsky.properties.
logger.info("成功获得XML的物理路径 : " + url.getFile());
}
catch(Exception e){
//logger.info("url error :" + e.getMessage());
}
return url.getFile();
}
public static void main(String[] args) {
/*System.out.println(XMLReadWrite.getInstance().getRealPath(XMLPath.NTSKY_COPYLEFT));
//File.getAbsolutePath();*/
try {
Document document = XMLReadWrite.getInstance().read(XMLPath.
NTSKY_LINK);
System.out.println("ok");
Element root = document.getRootElement();
System.out.println("root name = " + root.getName());
List list_linkId = document.selectNodes("//link/@linkId");
Iterator rs_linkId = list_linkId.iterator();
System.out.println("rs_linkId = "+ rs_linkId.hasNext());
while (rs_linkId.hasNext()) {
//root.remove(e);
//Node node = document.selectSingleNode("//ntsky/link");
//System.out.println("node text : " + node.getName());
//System.out.println("删除" + (document.remove(root)));
//System.out.println("删除" + (root.remove(node)));
//document.remove(node);
Iterator rs_root = root.elementIterator();
Node node = null;
String linkId = null;
for (; rs_root.hasNext(); ) {
node = (Node) rs_root.next();
//List list_root = document.selectNodes("//link");
//Element e = (Element)rs_linkId.next();
Attribute attribute = (Attribute) rs_linkId.next();
linkId = attribute.getText();
System.out.println("linkId = " + linkId);
//获取节点
//element = (Element) rs_root.next();
//System.out.println("root remove : " +root.remove(element));
//System.out.println("element : " + element.getName());
//Iterator rs = element.elementIterator("url");
System.out.println("linkId isTrue : " + linkId.equals("14"));
if (linkId.equals("14")) {
System.out.println("该element为 :" + node.getName());
System.out.println("成功 isTrue : " + root.remove(node));
System.out.println(document.remove(node)); ;
System.out.println("删除节点");
XMLReadWrite.getInstance().write(document,XMLPath.NTSKY_LINK);
}
}
}
System.out.println("执行");
/*
System.out.println("root : " + root.getText());
//枚举所有的节点
//List list = document.selectNodes("//link");
//Iterator rs_root = list.iterator();
//while (rs_root.hasNext()) {
List list_linkId = document.selectNodes("//link/@linkId");
Iterator rs_linkId = list_linkId.iterator();
while (rs_linkId.hasNext()) {
Iterator rs_root = root.elementIterator();
for (;rs_root.hasNext();){
//获取节点
Element element = (Element) rs_root.next();
Iterator rs = element.elementIterator("url");
Attribute attribute = (Attribute) rs_linkId.next();
String linkId = attribute.getText();
System.out.println("linkId = " + linkId);
while (rs.hasNext()) {
Element n = (Element) rs.next();
System.out.println(n.getName());
System.out.println(n.getText());
}
}
}
*/
}
catch (DocumentException ex) {
}
catch (Exception ex) {
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -