- 论坛徽章:
- 0
|
package test;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLDecoder;
import java.util.Iterator;
import java.util.List;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.VisitorSupport;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
public class MyTest {
/**
* 自动遍历节点,可以重载很多方法。
*/
public class MyVisitor extends VisitorSupport {
public void visit(Attribute attr) {
System.out.println(attr.getName()); //打印属性名
}
public void visit(Element element) {
System.out.println(element.getName()); //打印元素名
}
public void visit(Element element) {
System.out.println(element.getName()+":"+element.getData());
//打印元素名:元素值
System.out.println(element.hasMixedContent());
//打印该元素是否是复合类型(是否包含子结点)
}
}
private static String xmlPath = null;
public static void main(String[] args) throws Exception {
MyTest myTest = new MyTest();
myTest.createDocument();
myTest.getIterator(myTest.getRootElement(myTest.read(xmlPath)));
myTest.treeWalk(xmlPath);
myTest.getRootElement(myTest.read(xmlPath)).accept(myTest.new MyVisitor());
myTest.getString(myTest.read(xmlPath));
}
/**
###################################################################################
# 构造函数,定义文件的路径(和class放在一起)
###################################################################################
*/
public MyTest() throws Exception {
xmlPath = getClassFilePath().substring(1) + this.getClass().getName().replace('.', '/') + ".xml";
}
/**
###################################################################################
# 生成XML文档
###################################################################################
*/
public void createDocument() {
Document document = DocumentHelper.createDocument();
Element catalogElement = document.addElement("catalog");
catalogElement.addComment("An XML Catalog");
catalogElement.addProcessingInstruction("target", "text");
Element journalElement = catalogElement.addElement("journal");
journalElement.addAttribute("title", "XML Zone");
journalElement.addAttribute("publisher", "IBM developerWorks");
Element articleElement = journalElement.addElement("article");
articleElement.addAttribute("level", "Intermediate");
articleElement.addAttribute("date", "December-2001");
Element titleElement = articleElement.addElement("title");
titleElement.setText("Java configuration with XML Schema");
Element authorElement = articleElement.addElement("author");
Element firstNameElement = authorElement.addElement("firstname");
firstNameElement.setText("Marcello");
Element lastNameElement = authorElement.addElement("lastname");
lastNameElement.setText("Vitaletti");
document.addDocType("struts-config", "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN", "
http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd
");
//设置了之后在读取的时候总是去查找验证,暂时没有找到原因,以后找到了在贴上。
//找到了解决方法,加上.
try {
this.saveXml(document, this.xmlPath, "GBK");
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
/**
###################################################################################
# 遍历属性节点(查找XHTML文档中所有的超链接)
#
# @param document
# @throws DocumentException
#
###################################################################################
*/
public void findLinks(Document document) throws DocumentException {
List list = document.selectNodes("//a/@href");
for (Iterator iter = list.iterator(); iter.hasNext();) {
Attribute attribute = (Attribute) iter.next();
String url = attribute.getValue();
System.out.println(url);
}
}
/**
###################################################################################
# 遍历元素的节点
#
# @param root
# @throws Exception
#
###################################################################################
*/
public void getIterator(Element root) throws Exception {
for (Iterator i = root.elementIterator(); i.hasNext();) {
Element element = (Element) i.next();
System.out.println(element);
}
}
/**
###################################################################################
# 返回该文档的根元素
#
# @param doc
# @return
#
###################################################################################
*/
public Element getRootElement(Document doc) {
return doc.getRootElement();
}
/**
###################################################################################
# Xml 转 String
#
# @param document
# @return
#
###################################################################################
*/
public void getString(Document document) {
System.out.println(document.asXML());
/*
如果是String转换成XML:
String text = James ;
Document document = DocumentHelper.parseText(text);
*/
}
/**
###################################################################################
# 用SAX读入一个文档,返回这个Document
#
# @param fileName
# @return
# @throws MalformedURLException
# @throws DocumentException
#
###################################################################################
*/
public Document read(String fileName) throws MalformedURLException, DocumentException {
SAXReader reader = new SAXReader();
reader.setEntityResolver(new EntityResolver() {
public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException {
InputStream dtdin=null;
dtdin = this.getClass().getResourceAsStream("struts-config_1_1.dtd");
//dtdin=new FileInputStream(new File("c:\\struts-config_1_1.dtd"));
return new InputSource(dtdin);
}
});
Document document = reader.read(new File(fileName));
return document;
}
/**
###################################################################################
# 格式化XML文档,并按指定字符集输出
#
# @param document
# @param fileName
# @param encoding
# @return
# @throws UnsupportedEncodingException
# @throws FileNotFoundException
# @throws IOException
#
###################################################################################
*/
public int saveXml(Document document, String fileName, String encoding) throws UnsupportedEncodingException, FileNotFoundException, IOException {
int returnValue = 0;
XMLWriter output = null;
/** 格式化输出 , 类型 IE 浏览一样 */
OutputFormat format = OutputFormat.createPrettyPrint();
/** 命令行格式化输出*/
//OutputFormat format = OutputFormat.createCompactFormat();
format.setIndent("\t"); // 使用TAB缩进
/** 指定 XML 字符集编码 */
format.setEncoding(encoding);
output = new XMLWriter(new FileOutputStream(new File(fileName)), format);
output.write(document);
output.close();
/** 执行成功 , 需返回 1 */
returnValue = 1;
return returnValue;
}
/**
###################################################################################
# 遍历传入元素下面所有节点
#
# @param element
#
###################################################################################
*/
public void treeWalk(Element element) {
for (int i = 0, size = element.nodeCount(); i
//"test" 是当前包的路径,如果是“”系统返回的是你classes环境的根目录路径
return URLDecoder.decode(sourceUrl.getFile(), "GBK");
}
}
需要说明的是,在用dom4j生成美观模式的文档在读取的时候会将回车换行当作文本来处理。关于修改的方法我没有加入进去,主要是比较简单,看看就会了。
本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u1/57965/showart_1165772.html |
|