用JAVA创建,读取XML文件

首先导入dom4j.jar包,这个包里面有我们创建XML文件需要用到的类,比如常用的有:

Document:
import org.dom4j.Document;
Import org.dom4j.DocumentHelper;
DocumentHelper.createDocument(); 获得文档对象
addElement方法增加根节点
getRootElement方法获得根节点
asXML方法将文档对象包含的内容转换为字符
addComment方法在增加注释
getPath方法获得路径,根节点为/

Element:
import org.dom4j.Elemet;
addElement方法增加子节点
addAttribute方法增加属性
getName方法获得节点名称
asXML方法将文档对象包含的内容转换为字符
addComment方法在增加注释
getPath方法获得路径
addText方法增加节点值
setText方法设置节点值
addNamespace方法增加命名空间
attributeCount方法获得属性个数
attributeValue方法获得属性值
elementText方法指定元素的值

Attribute:
Import org.dom4j.Attribute;
getName方法获得属性名称
getValue方法获得属性值
setValue方法设置属性值

Node:
import org.dom4j.Node;
Document和Element中selectSingleNode方法获得对象
setName方法设置节点名称
getText方法获得节点值
setText方法设置节点值

List,Iterator:
import java.util.List;
Document和Element中selectNodes方法获得对象
import java.util.Iterator;
List中iterator方法,Iterator中hasNext方法和next方法来获得节点或属性

XMLWriter:
import org.dom4j.io.XMLWriter;
Writer方法与OutputStream(FileOutputStream),Writer(FileWriter)
write方法写对象
close方法关闭写对象

OutputFormat:
import org.dom4j.io.OutputFormat;
setEncoding方法设置字符集
createCompactFormat方法为紧凑格式,默认
createPrettyFormat方法为缩进格式

SAXReader:
import org.dom4j.io.SAXReader ;
read方法与File

下面,我们用这些类来实现一下创建XML文件的简单过程:

 1 None.gif package  Test;
 2 None.gif
 3 None.gif import  java.io. * ;
 4 None.gif import  org.dom4j. * ;
 5 None.gif import  org.dom4j.io. * ;
 6 None.gif
 7 ExpandedBlockStart.gifContractedBlock.gif public   class  xmlTest  dot.gif {
 8InBlock.gif    
 9ExpandedSubBlockStart.gifContractedSubBlock.gif    public void writeXML()dot.gif{
10InBlock.gif        Document doc = org.dom4j.DocumentHelper.createDocument();
11InBlock.gif        //创建节点操作对象
12InBlock.gif        Element rootElement = doc.addElement("student");
13InBlock.gif        //创建根节点对象rootElement,标签名为student
14InBlock.gif        
15InBlock.gif        rootElement.setText("hello");
16InBlock.gif        //对节点设置内容,后面读取XML文件的时候可以把他取出来
17InBlock.gif        rootElement.addAttribute("id","030511111");
18InBlock.gif        //设置节点属性
19InBlock.gif        Element name = rootElement.addElement("name");
20InBlock.gif        //添加子节点
21InBlock.gif        name.setText("xiaozhang");
22InBlock.gif        //设置子节点内容
23InBlock.gif        Element age = rootElement.addElement("age");
24InBlock.gif        age.setText("23");
25InBlock.gif        
26ExpandedSubBlockStart.gifContractedSubBlock.gif        try dot.gif{
27InBlock.gif            OutputFormat fmt = new OutputFormat();
28InBlock.gif            //创建输出格式对象
29InBlock.gif            fmt.setEncoding("gb2312");
30InBlock.gif            XMLWriter writer = new XMLWriter(fmt);
31InBlock.gif            //以输出格式为参数,创建XML文件输出对象
32InBlock.gif            OutputStream out = new FileOutputStream("d:\\test.xml");
33InBlock.gif            //创建输出流..
34InBlock.gif            writer.setOutputStream(out);
35InBlock.gif            //设置输出流
36InBlock.gif            writer.write(doc);
37InBlock.gif            //输出doc对象,即形成XML文件
38ExpandedSubBlockStart.gifContractedSubBlock.gif        }
 catch (Exception e) dot.gif{
39InBlock.gif
40InBlock.gif            e.printStackTrace();
41ExpandedSubBlockEnd.gif        }

42InBlock.gif        
43ExpandedSubBlockEnd.gif    }

44InBlock.gif    
45ExpandedSubBlockStart.gifContractedSubBlock.gif    public void readXML()dot.gif{
46InBlock.gif        SAXReader reader = new SAXReader();
47InBlock.gif        //创建读取对象
48InBlock.gif        Document doc;
49ExpandedSubBlockStart.gifContractedSubBlock.gif        try dot.gif{
50InBlock.gif            doc = reader.read(new File("d:\\test.xml"));
51InBlock.gif            //从test.xml文件中取得节点操作对象
52InBlock.gif            Element name = doc.getRootElement();
53InBlock.gif            //取得节点对象
54InBlock.gif            System.out.println(name.getText());
55InBlock.gif            //输出节点内容
56ExpandedSubBlockStart.gifContractedSubBlock.gif        }
 catch (Exception e) dot.gif{
57InBlock.gif            e.printStackTrace();
58ExpandedSubBlockEnd.gif        }

59ExpandedSubBlockEnd.gif    }

60InBlock.gif    
61ExpandedSubBlockStart.gifContractedSubBlock.gif    public static void main(String[] args)dot.gif{
62InBlock.gif        xmlTest s = new xmlTest();
63InBlock.gif        s.writeXML();
64InBlock.gif        s.readXML();    
65ExpandedSubBlockEnd.gif    }

66ExpandedBlockEnd.gif}

67 None.gif
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值