首先导包:
dom4j-1.6.1.jar
jaxen-1.1-beta-6.jar
代码
/**
* Document doc = reader.read(new File("./src/bean-action.xml"));
* Element ele = doc.getRootElement();得到根标签
* ele.element("bean");得到指定标签
* e1e.attributeValue("id");得到指定属性
* e1e.getText();得到文本
*
*
*/
public class Dom4jTest {
static SAXReader reader = null;
static Document doc = null;
static Iterator<Node> it = null;
static{
//创建xml解析器对象
reader = new SAXReader();
//读取xml文档,返回Document
try {
doc = reader.read(new File("./src/bean-action.xml"));
//nodeIterator 得到当前节点下的所有子节点对象(不包括孙以下的节点)
it = doc.nodeIterator();
} catch (DocumentException e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws Exception {
// nodeTest( it );
// Element el = doc.getRootElement();
// getChildNodes(el);
// Element ele = doc.getRootElement();
// StringBuffer sb = new StringBuffer();
// printXML(ele,sb);
// System.out.println(sb.toString());
test5();
}
/**
* *
* description:循环得到所有子节点
* @param it
* @throws Exception
*/
public static void nodeTest( Iterator<Node> it ) throws Exception{
while(it.hasNext()){//判断是否有下一个元素
Node node = it.next();
// String name = node.getName();
// System.out.println(name);
//继续去除其下面的子节点,只有标签节点才有子节点,判断当前节点是否是标签节点
if(node instanceof Element){
Element elemnt = (Element)node;
System.out.println(elemnt.getName());//得到节点名称
Iterator<Node> it1 = elemnt.nodeIterator();
nodeTest(it1);
}
}
}
/**
* *
* description:得到所有子节点
* @param el
*/
public static void getChildNodes( Element el ){
System.out.println(el.getName());
Iterator<Node> it = el.nodeIterator();
while(it.hasNext()){
Node node = it.next();
if(node instanceof Element){
Element e = (Element)node;
getChildNodes(e);
}
}
}
public static void Test(){
//读取文档
//得到跟标签
Element ele = doc.getRootElement();
//得到当前标签下指定名称的第一个子标签
Element e1 = ele.element("bean");
System.out.println(e1.getName());
//得到当前标签下指定名称的所有标签
Iterator<Element> it = ele.elementIterator("bean");
//得到当前标签下的所有子标签
List<Element> list = ele.elements();
//获取更深层次的标签(方法只能一层层的获取)
Element e2 = doc.getRootElement().element("bean").element("property");
System.out.println(e2.getName());
}
/**
* 获取属性
*/
public static void test1(){
//获取属性:(先获取属性所在的标签对象,然后才能获取属性)
//1.得到标签对象
Element ele = doc.getRootElement();
Element e1 = ele.element("bean");
//2.得到属性
//2.1得到指定名称的属性值
String id = e1.attributeValue("id");
System.out.println(id);
//2.2得到指定属性名称的属性对象
Attribute attribute = e1.attribute("id");
System.out.println(attribute.getName()+":::"+attribute.getValue());
//2.3得到所有属性对象 返回list集合
List<Attribute> list = e1.attributes();
//2.4得到所有属性对象 返回迭代器
Iterator<Attribute> it = e1.attributeIterator();
}
/**
* *
* description:获取文本
*/
public static void test2(){
Element ele = doc.getRootElement();
//空格和换行也是xml的内容
String content = ele.getText();
System.out.println("========="+content);
//获取文本 先获取表签在获取标签上的文本
Element e1 = ele.element("bean");
//1.得到文本
String text = e1.getText();
//2.得到指定子标签名的文本内容
Element e2 = e1.element("property");
String textValue = e2.getText(); //得到标签的文本内容
}
/**
* *
* description:输出xml
* @param e
* @param sb
*/
public static void printXML(Element e,StringBuffer sb){
sb.append("<"+e.getName() );
List<Attribute> list = e.attributes();
if(list!=null&&list.size()>0){
for(Attribute a : list){
sb.append(" "+a.getName()+"="+"\""+a.getValue()+"\"");
}
sb.append(">");
}
Iterator<Node> it = e.nodeIterator();
while(it.hasNext()){
Node node = it.next();
if(node instanceof Element){
printXML((Element)node,sb);
}
if(node instanceof Text){
Text text = (Text)node;
sb.append(text.getText());
}
}
System.out.println("</"+e.getName()+">");
sb.append("</"+e.getName()+">" );
}
/**
*
* description:把xml文档信息封装到对象中
*/
public static void test3(){
Element ele = doc.getRootElement();
Iterator<Element> it = ele.elementIterator("bean");
while(it.hasNext()){
Element e = it.next();
String str = e.elementText("property");//读取当前表签下的propert标签的文本
System.out.println(str);
break;
}
}
/**
* *
* description:拷贝xml
* @throws Exception
*/
public static void test4() throws Exception{
//创建xml解析器
//SAXReader reader = new SAXReader();
//读取xml文件 生成Document对象
Document document = reader.read(new File("./src/struts.xml"));
FileOutputStream out = new FileOutputStream(new File("D:/struts.xml"));
XMLWriter write = new XMLWriter(out);
write.write(document);//写
write.close();
}
/**
* *
* description:拷贝xml
* @throws Exception
*/
public static void test5() throws Exception{
//创建xml解析器
//SAXReader reader = new SAXReader();
//读取xml文件 生成Document对象
Document document = reader.read(new File("./src/struts.xml"));
FileOutputStream out = new FileOutputStream(new File("D:/struts2.xml"));
OutputFormat of = OutputFormat.createCompactFormat();//设置紧凑的格式
OutputFormat of1 = OutputFormat.createPrettyPrint();//设置漂亮的格式
XMLWriter write = new XMLWriter(out,of1);
write.write(document);
write.close();
}
}