Java操作XML(使用org.w3c.dom)1/3

来源:互联网 发布:linux vi覆盖文件内容 编辑:程序博客网 时间:2024/05/16 11:08

一、创建DOM

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
XMLBuilder.java
 
 用于创建DOM,Root结点
 
/********************************************************************
 * 项目名称    :rochoc   <p>
 * 包名称      :rochoc.xml.oper <p>
 * 文件名称    :XmlBuilder   <p>
 * 编写者     :luoc    <p>
 * 编写日期    :2005-6-22    <p>
 * 程序功能(类)描述 : 根据传入的XML文件生成Document和root结点<p>
 *
 * 程序变更日期   :
 * 变更作者    :
 * 变更说明    :
********************************************************************/
package rochoc.xml.oper;
 
import java.io.File;
import java.io.IOException;
 
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
 
import org.apache.log4j.Logger;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.xml.sax.SAXException;
 
/**
 * 类名:XmlBuilder  <p>
 * 类描述:根据传入的XML文件生成Document和root结点 <p>
 * 编写者 :luoc<p>
 * 编写日期 :2005-6-22<p>
 * 主要public成员变量:<p>
 * 主要public方法:   <p>
 **/
 
public class XmlBuilder
{
    /**
     *构造函数说明:       <p>
     *参数说明:@param path   <p>
    **/
    public XmlBuilder(String path)
    {
        this.path=path;
        init();
    }
     
    /**
    * 方法名称:init<p>
    * 方法功能:初始化函数<p>
    * 参数说明: <p>
    * 返回:void <p>
    * 作者:luoc
    * 日期:2005-6-22
    **/
    public void init()
    {
        buildDocument();
        buildRoot();
    }
     
    /**
    * 方法名称:buildDocument<p>
    * 方法功能:将XML文件生成Document <p>
    * 参数说明: <p>
    * 返回:void <p>
    * 作者:luoc
    * 日期:2005-6-22
    **/
    private void buildDocument()
    {
        DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
        try
        {
            DocumentBuilder builder=factory.newDocumentBuilder();
            logger.debug("Construct document builder success.");
            doc=builder.parse(new File(path));           
            logger.debug("Build xml document success.");
        }catch(ParserConfigurationException e)
        {
            logger.error("Construct document builder error:"+e);
        }catch(SAXException e)
        {
            logger.error("Parse xml file error:"+e);
        }catch(IOException e)
        {
            logger.error("Read xml file error:"+e);
        }
    }
     
    /**
    * 方法名称:buildRoot<p>
    * 方法功能:生成XML的根结点<p>
    * 参数说明: <p>
    * 返回:void <p>
    * 作者:luoc
    * 日期:2005-6-22
    **/
    private void buildRoot()
    {
        root=doc.getDocumentElement();
    }
     
    /**
     * @return 返回 doc。
     */
    public Document getDoc()
    {
        return doc;
    }
    /**
     * @param doc 要设置的 doc。
     */
    public void setDoc(Document doc)
    {
        this.doc = doc;
    }
    /**
     * @return 返回 path。
     */
    public String getPath()
    {
        return path;
    }
    /**
     * @param path 要设置的 path。
     */
    public void setPath(String path)
    {
        this.path = path;
    }
    /**
     * @return 返回 root。
     */
    public Element getRoot()
    {
        return root;
    }
    /**
     * @param root 要设置的 root。
     */
    public void setRoot(Element root)
    {
        this.root = root;
    }
    /*全局变量*/
    private String path=null;//xml文件路径
    private Document doc=null;//xml文件对应的document
    private Element root=null;//xml文件的根结点
    private Logger logger=Logger.getLogger(getClass().getName());
}
原创粉丝点击