package org.lxh;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.UnsupportedEncodingException;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;
public class CreateXml {
public static void main(String[] args) {
File f=new File("d:"+File.separator+"my.xml");
Document docu=DocumentHelper.createDocument(); //创建xml文档
Element linkman=docu.addElement("linkman"); //创建根节点
Element name=linkman.addElement("name"); //创建子元素
Element age=linkman.addElement("age");
name.setText("陈瑞银"); //设置name节点的内容
age.setText("22"); //设置age节点的内容
OutputFormat format=OutputFormat.createPrettyPrint(); //指定输出格式
format.setEncoding("UTF-8"); //指定输出编码
try {
XMLWriter w=new XMLWriter(new FileOutputStream(f),format); //输出文件
w.write(docu); //输出内容
w.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
package org.lxh;
import java.io.File;
import java.util.Iterator;
import org.dom4j.*;
import org.dom4j.io.SAXReader;
public class ReadXml {
public static void main(String[] args) {
File f=new File("d:"+File.separator+"my.xml");
SAXReader read=new SAXReader(); //建立SAX解析读取
Document document=null;
try {
document=read.read(f); //读取文档
Element root=document.getRootElement(); //取得根元素
//下面给注释的部分用于解析复杂的xml(3层或以上)
/*Iterator it=root.elementIterator(); //取得全部子节点
while(it.hasNext())
{
/*Element e=(Element)it.next();
System.out.println(e.elementText("name")); //取得文本元素
System.out.println(e.elementText("age"));
}*/
System.out.println(root.elementText("age"));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有