XMLBuilder.java |
002 | |
003 | 用于創(chuàng)建DOM,Root結(jié)點(diǎn) |
004 | |
005 | /******************************************************************** |
006 | * 項(xiàng)目名稱 :rochoc <p> |
007 | * 包名稱 :rochoc.xml.oper <p> |
008 | * 文件名稱 :XmlBuilder <p> |
009 | * 編寫者 :luoc <p> |
010 | * 編寫日期 :2005-6-22 <p> |
011 | * 程序功能(類)描述 : 根據(jù)傳入的XML文件生成Document和root結(jié)點(diǎn)<p> |
012 | * |
013 | * 程序變更日期 : |
014 | * 變更作者 : |
015 | * 變更說明 : |
016 | ********************************************************************/ |
017 | package rochoc.xml.oper; |
018 | |
019 | import java.io.File; |
020 | import java.io.IOException; |
021 | |
022 | import javax.xml.parsers.DocumentBuilder; |
023 | import javax.xml.parsers.DocumentBuilderFactory; |
024 | import javax.xml.parsers.ParserConfigurationException; |
025 | |
026 | import org.apache.log4j.Logger; |
027 | import org.w3c.dom.Document; |
028 | import org.w3c.dom.Element; |
029 | import org.xml.sax.SAXException; |
030 | |
031 | /** |
032 | * 類名:XmlBuilder <p> |
033 | * 類描述:根據(jù)傳入的XML文件生成Document和root結(jié)點(diǎn) <p> |
034 | * 編寫者 :luoc<p> |
035 | * 編寫日期 :2005-6-22<p> |
036 | * 主要public成員變量:<p> |
037 | * 主要public方法: <p> |
038 | **/ |
039 | |
040 | public class XmlBuilder |
041 | { |
042 | /** |
043 | *構(gòu)造函數(shù)說明: <p> |
044 | *參數(shù)說明:@param path <p> |
045 | **/ |
046 | public XmlBuilder(String path) |
047 | { |
048 | this .path=path; |
049 | init(); |
050 | } |
051 | |
052 | /** |
053 | * 方法名稱:init<p> |
054 | * 方法功能:初始化函數(shù)<p> |
055 | * 參數(shù)說明: <p> |
056 | * 返回:void <p> |
057 | * 作者:luoc |
058 | * 日期:2005-6-22 |
059 | **/ |
060 | public void init() |
061 | { |
062 | buildDocument(); |
063 | buildRoot(); |
064 | } |
065 | |
066 | /** |
067 | * 方法名稱:buildDocument<p> |
068 | * 方法功能:將XML文件生成Document <p> |
069 | * 參數(shù)說明: <p> |
070 | * 返回:void <p> |
071 | * 作者:luoc |
072 | * 日期:2005-6-22 |
073 | **/ |
074 | private void buildDocument() |
075 | { |
076 | DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance(); |
077 | try |
078 | { |
079 | DocumentBuilder builder=factory.newDocumentBuilder(); |
080 | logger.debug( "Construct document builder success." ); |
081 | doc=builder.parse( new File(path)); |
082 | logger.debug( "Build xml document success." ); |
083 | } catch (ParserConfigurationException e) |
084 | { |
085 | logger.error( "Construct document builder error:" +e); |
086 | } catch (SAXException e) |
087 | { |
088 | logger.error( "Parse xml file error:" +e); |
089 | } catch (IOException e) |
090 | { |
091 | logger.error( "Read xml file error:" +e); |
092 | } |
093 | } |
094 | |
095 | /** |
096 | * 方法名稱:buildRoot<p> |
097 | * 方法功能:生成XML的根結(jié)點(diǎn)<p> |
098 | * 參數(shù)說明: <p> |
099 | * 返回:void <p> |
100 | * 作者:luoc |
101 | * 日期:2005-6-22 |
102 | **/ |
103 | private void buildRoot() |
104 | { |
105 | root=doc.getDocumentElement(); |
106 | } |
107 | |
108 | /** |
109 | * @return 返回 doc。 |
110 | */ |
111 | public Document getDoc() |
112 | { |
113 | return doc; |
114 | } |
115 | /** |
116 | * @param doc 要設(shè)置的 doc。 |
117 | */ |
118 | public void setDoc(Document doc) |
119 | { |
120 | this .doc = doc; |
121 | } |
122 | /** |
123 | * @return 返回 path。 |
124 | */ |
125 | public String getPath() |
126 | { |
127 | return path; |
128 | } |
129 | /** |
130 | * @param path 要設(shè)置的 path。 |
131 | */ |
132 | public void setPath(String path) |
133 | { |
134 | this .path = path; |
135 | } |
136 | /** |
137 | * @return 返回 root。 |
138 | */ |
139 | public Element getRoot() |
140 | { |
141 | return root; |
142 | } |
143 | /** |
144 | * @param root 要設(shè)置的 root。 |
145 | */ |
146 | public void setRoot(Element root) |
147 | { |
148 | this .root = root; |
149 | } |
150 | /*全局變量*/ |
151 | private String path= null ; //xml文件路徑 |
152 | private Document doc= null ; //xml文件對(duì)應(yīng)的document |
153 | private Element root= null ; //xml文件的根結(jié)點(diǎn) |
154 | private Logger logger=Logger.getLogger(getClass().getName()); |
155 | } |
001 | XmlOper.java |
002 | |
003 | 用于操作XML文件,包括查找、新增、刪除、修改結(jié)點(diǎn) |
004 | |
005 | /******************************************************************** |
006 | * 項(xiàng)目名稱 :rochoc <p> |
007 | * 包名稱 :rochoc.xml.oper <p> |
008 | * 文件名稱 :XmlOper <p> |
009 | * 編寫者 :luoc <p> |
010 | * 編寫日期 :2005-6-22 <p> |
011 | * 程序功能(類)描述 : 對(duì)XML進(jìn)行讀寫操作 <p> |
012 | * |
013 | * 程序變更日期 : |
014 | * 變更作者 : |
015 | * 變更說明 : |
016 | ********************************************************************/ |
017 | package rochoc.xml.oper; |
018 | |
019 | import java.util.ArrayList; |
020 | |
021 | import org.apache.log4j.Logger; |
022 | import org.w3c.dom.Document; |
023 | import org.w3c.dom.Element; |
024 | import org.w3c.dom.Node; |
025 | import org.w3c.dom.NodeList; |
026 | |
027 | /** |
028 | * 類名:XmlOper <p> |
029 | * 類描述:對(duì)XML文件進(jìn)行讀寫操作,均為靜態(tài)函數(shù) <p> |
030 | * 編寫者 :luoc<p> |
031 | * 編寫日期 :2005-6-22<p> |
032 | * 主要public成員變量:<p> |
033 | * 主要public方法: <p> |
034 | **/ |
035 | |
036 | public class XmlOper |
037 | { |
038 | /** |
039 | *構(gòu)造函數(shù)說明: <p> |
040 | *參數(shù)說明: <p> |
041 | **/ |
042 | private XmlOper() |
043 | { |
044 | } |
045 | |
046 | /** |
047 | * 方法名稱:getNodeList<p> |
048 | * 方法功能:獲取父結(jié)點(diǎn)parent的所有子結(jié)點(diǎn)<p> |
049 | * 參數(shù)說明:@param parent |
050 | * 參數(shù)說明:@return <p> |
051 | * 返回:NodeList <p> |
052 | * 作者:luoc |
053 | * 日期:2005-6-22 |
054 | **/ |
055 | public static NodeList getNodeList(Element parent) |
056 | { |
057 | return parent.getChildNodes(); |
058 | } |
059 | |
060 | /** |
061 | * 方法名稱:getElementsByName<p> |
062 | * 方法功能:在父結(jié)點(diǎn)中查詢指定名稱的結(jié)點(diǎn)集 <p> |
063 | * 參數(shù)說明:@param parent |
064 | * 參數(shù)說明:@param name |
065 | * 參數(shù)說明:@return <p> |
066 | * 返回:Element[] <p> |
067 | * 作者:luoc |
068 | * 日期:2005-6-22 |
069 | **/ |
070 | public static Element [] getElementsByName(Element parent,String name) |
071 | { |
072 | ArrayList resList= new ArrayList(); |
073 | NodeList nl=getNodeList(parent); |
074 | for ( int i= 0 ;i<nl.getLength();i++) |
075 | { |
076 | Node nd=nl.item(i); |
077 | if (nd.getNodeName().equals(name)) |
078 | { |
079 | resList.add(nd); |
080 | } |
081 | } |
082 | Element [] res= new Element [resList.size()]; |
083 | for ( int i= 0 ;i<resList.size();i++) |
084 | { |
085 | res[ 0 ]=(Element)resList.get(i); |
086 | } |
087 | logger.debug(parent.getNodeName()+ "'s children of " +name+ |
088 | "'s num:" +res.length); |
089 | return res; |
090 | } |
091 | |
092 | /** |
093 | * 方法名稱:getElementName<p> |
094 | * 方法功能:獲取指定Element的名稱 <p> |
095 | * 參數(shù)說明:@param element |
096 | * 參數(shù)說明:@return <p> |
097 | * 返回:String <p> |
098 | * 作者:luoc |
099 | * 日期:2005-6-22 |
100 | **/ |
101 | public static String getElementName(Element element) |
102 | { |
103 | return element.getNodeName(); |
104 | } |
105 | |
106 | /** |
107 | * 方法名稱:getElementValue<p> |
108 | * 方法功能:獲取指定Element的值<p> |
109 | * 參數(shù)說明:@param element |
110 | * 參數(shù)說明:@return <p> |
111 | * 返回:String <p> |
112 | * 作者:luoc |
113 | * 日期:2005-6-22 |
114 | **/ |
115 | public static String getElementValue(Element element) |
116 | { |
117 | NodeList nl=element.getChildNodes(); |
118 | for ( int i= 0 ;i<nl.getLength();i++) |
119 | { |
120 | if (nl.item(i).getNodeType()==Node.TEXT_NODE) //是一個(gè)Text Node |
121 | { |
122 | logger.debug(element.getNodeName()+ " has a Text Node." ); |
123 | return element.getFirstChild().getNodeValue(); |
124 | } |
125 | } |
126 | logger.error(element.getNodeName()+ " hasn't a Text Node." ); |
127 | return null ; |
128 | } |
129 | |
130 | /** |
131 | * 方法名稱:getElementAttr<p> |
132 | * 方法功能:獲取指定Element的屬性attr的值 <p> |
133 | * 參數(shù)說明:@param element |
134 | * 參數(shù)說明:@param attr |
135 | * 參數(shù)說明:@return <p> |
136 | * 返回:String <p> |
137 | * 作者:luoc |
138 | * 日期:2005-6-22 |
139 | **/ |
140 | public static String getElementAttr(Element element,String attr) |
141 | { |
142 | return element.getAttribute(attr); |
143 | } |
144 | |
145 | /** |
146 | * 方法名稱:setElementValue<p> |
147 | * 方法功能:設(shè)置指定Element的值 <p> |
148 | * 參數(shù)說明:@param element |
149 | * 參數(shù)說明:@param val <p> |
150 | * 返回:void <p> |
151 | * 作者:luoc |
152 | * 日期:2005-6-22 |
153 | **/ |
154 | public static void setElementValue(Element element,String val) |
155 | { |
156 | Node node=element.getOwnerDocument().createTextNode(val); |
157 | NodeList nl=element.getChildNodes(); |
158 | for ( int i= 0 ;i<nl.getLength();i++) |
159 | { |
160 | Node nd=nl.item(i); |
161 | if (nd.getNodeType()==Node.TEXT_NODE) //是一個(gè)Text Node |
162 | { |
163 | nd.setNodeValue(val); |
164 | logger.debug( "modify " +element.getNodeName()+ "'s node value succe." ); |
165 | return ; |
166 | } |
167 | } |
168 | logger.debug( "new " +element.getNodeName()+ "'s node value succe." ); |
169 | element.appendChild(node); |
170 | } |
171 | |
172 | /** |
173 | * 方法名稱:setElementAttr<p> |
174 | * 方法功能:設(shè)置結(jié)點(diǎn)Element的屬性<p> |
175 | * 參數(shù)說明:@param element |
176 | * 參數(shù)說明:@param attr |
177 | * 參數(shù)說明:@param attrVal <p> |
178 | * 返回:void <p> |
179 | * 作者:luoc |
180 | * 日期:2005-6-22 |
181 | **/ |
182 | public static void setElementAttr(Element element, |
183 | String attr,String attrVal) |
184 | { |
185 | element.setAttribute(attr,attrVal); |
186 | } |
187 | |
188 | |
189 | /** |
190 | * 方法名稱:addElement<p> |
191 | * 方法功能:在parent下增加結(jié)點(diǎn)child<p> |
192 | * 參數(shù)說明:@param parent |
193 | * 參數(shù)說明:@param child <p> |
194 | * 返回:void <p> |
195 | * 作者:luoc |
196 | * 日期:2005-6-22 |
197 | **/ |
198 | public static void addElement(Element parent,Element child) |
199 | { |
200 | parent.appendChild(child); |
201 | } |
202 | |
203 | /** |
204 | * 方法名稱:addElement<p> |
205 | * 方法功能:在parent下增加字符串tagName生成的結(jié)點(diǎn)<p> |
206 | * 參數(shù)說明:@param parent |
207 | * 參數(shù)說明:@param tagName <p> |
208 | * 返回:void <p> |
209 | * 作者:luoc |
210 | * 日期:2005-6-22 |
211 | **/ |
212 | public static void addElement(Element parent,String tagName) |
213 | { |
214 | Document doc=parent.getOwnerDocument(); |
215 | Element child=doc.createElement(tagName); |
216 | parent.appendChild(child); |
217 | } |
218 | |
219 | /** |
220 | * 方法名稱:addElement<p> |
221 | * 方法功能:在parent下增加tagName的Text結(jié)點(diǎn),且值為text<p> |
222 | * 參數(shù)說明:@param parent |
223 | * 參數(shù)說明:@param tagName |
224 | * 參數(shù)說明:@param text <p> |
225 | * 返回:void <p> |
226 | * 作者:luoc |
227 | * 日期:2005-6-22 |
228 | **/ |
229 | public static void addElement(Element parent,String tagName,String text) |
230 | { |
231 | Document doc=parent.getOwnerDocument(); |
232 | Element child=doc.createElement(tagName); |
233 | setElementValue(child,text); |
234 | parent.appendChild(child); |
235 | } |
236 | |
237 | /** |
238 | * 方法名稱:removeElement<p> |
239 | * 方法功能:將父結(jié)點(diǎn)parent下的名稱為tagName的結(jié)點(diǎn)移除<p> |
240 | * 參數(shù)說明:@param parent |
241 | * 參數(shù)說明:@param tagName <p> |
242 | * 返回:void <p> |
243 | * 作者:luoc |
244 | * 日期:2005-6-22 |
245 | **/ |
246 | public static void removeElement(Element parent,String tagName) |
247 | { |
248 | logger.debug( "remove " +parent.getNodeName()+ "'s children by tagName " +tagName+ " begin..." ); |
249 | NodeList nl=parent.getChildNodes(); |
250 | for ( int i= 0 ;i<nl.getLength();i++) |
251 | { |
252 | Node nd=nl.item(i); |
253 | if (nd.getNodeName().equals(tagName)) |
254 | { |
255 | parent.removeChild(nd); |
256 | logger.debug( "remove child '" +nd+ "' success." ); |
257 | } |
258 | } |
259 | logger.debug( "remove " +parent.getNodeName()+ "'s children by tagName " +tagName+ " end." ); |
260 | } |
261 | |
262 | |
263 | /*全局變量*/ |
264 | static Logger logger=Logger.getLogger( "XmlOper" ); |
265 | } |
001 | XmlCreater.java |
002 | |
003 | 用于創(chuàng)建XML文件 |
004 | |
005 | /******************************************************************** |
006 | * 項(xiàng)目名稱 :rochoc <p> |
007 | * 包名稱 :rochoc.xml.oper <p> |
008 | * 文件名稱 :XmlCreater <p> |
009 | * 編寫者 :luoc <p> |
010 | * 編寫日期 :2005-6-22 <p> |
011 | * 程序功能(類)描述 : 創(chuàng)建DOM并生成XML文件 <p> |
012 | * |
013 | * 程序變更日期 : |
014 | * 變更作者 : |
015 | * 變更說明 : |
016 | ********************************************************************/ |
017 | package rochoc.xml.oper; |
018 | |
019 | import java.io.File; |
020 | |
021 | import javax.xml.parsers.DocumentBuilder; |
022 | import javax.xml.parsers.DocumentBuilderFactory; |
023 | import javax.xml.parsers.ParserConfigurationException; |
024 | import javax.xml.transform.Transformer; |
025 | import javax.xml.transform.TransformerConfigurationException; |
026 | import javax.xml.transform.TransformerException; |
027 | import javax.xml.transform.TransformerFactory; |
028 | import javax.xml.transform.dom.DOMSource; |
029 | import javax.xml.transform.stream.StreamResult; |
030 | |
031 | import org.apache.log4j.Logger; |
032 | import org.w3c.dom.Document; |
033 | import org.w3c.dom.Element; |
034 | |
035 | /** |
036 | * 類名:XmlCreater <p> |
037 | * 類描述: 創(chuàng)建DOM并生成XML文件<p> |
038 | * 編寫者 :luoc<p> |
039 | * 編寫日期 :2005-6-22<p> |
040 | * 主要public成員變量:<p> |
041 | * 主要public方法: <p> |
042 | **/ |
043 | |
044 | public class XmlCreater |
045 | { |
046 | /** |
047 | *構(gòu)造函數(shù)說明: <p> |
048 | *參數(shù)說明:@param path xml文件路徑 <p> |
049 | **/ |
050 | public XmlCreater(String path) |
051 | { |
052 | this .path=path; |
053 | init(); |
054 | } |
055 | |
056 | /** |
057 | * 方法名稱:init<p> |
058 | * 方法功能: 初始化函數(shù) <p> |
059 | * 參數(shù)說明: <p> |
060 | * 返回:void <p> |
061 | * 作者:luoc |
062 | * 日期:2005-6-22 |
063 | **/ |
064 | private void init() |
065 | { |
066 | DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance(); |
067 | try |
068 | { |
069 | DocumentBuilder builder=factory.newDocumentBuilder(); |
070 | doc=builder.newDocument(); //新建DOM |
071 | } catch (ParserConfigurationException e) |
072 | { |
073 | logger.error( "Parse DOM builder error:" +e); |
074 | } |
075 | } |
076 | |
077 | /** |
078 | * 方法名稱:createRootElement<p> |
079 | * 方法功能:創(chuàng)建根結(jié)點(diǎn),并返回 <p> |
080 | * 參數(shù)說明:@param rootTagName <p> |
081 | * 返回:Element <p> |
082 | * 作者:luoc |
083 | * 日期:2005-6-22 |
084 | **/ |
085 | public Element createRootElement(String rootTagName) |
086 | { |
087 | if (doc.getDocumentElement()== null ) |
088 | { |
089 | logger.debug( "create root element '" +rootTagName+ "' success." ); |
090 | Element root=doc.createElement(rootTagName); |
091 | doc.appendChild(root); |
092 | return root; |
093 | } |
094 | logger.warn( "this dom's root element is exist,create fail." ); |
095 | return doc.getDocumentElement(); |
096 | } |
097 | |
098 | /** |
099 | * 方法名稱:createElement<p> |
100 | * 方法功能:在parent結(jié)點(diǎn)下增加子結(jié)點(diǎn)tagName<p> |
101 | * 參數(shù)說明:@param parent |
102 | * 參數(shù)說明:@param tagName <p> |
103 | * 返回:Element <p> |
104 | * 作者:luoc |
105 | * 日期:2005-6-22 |
106 | **/ |
107 | public Element createElement(Element parent,String tagName) |
108 | { |
109 | Document doc=parent.getOwnerDocument(); |
110 | Element child=doc.createElement(tagName); |
111 | parent.appendChild(child); |
112 | return child; |
113 | } |
114 | |
115 | /** |
116 | * 方法名稱:createElement<p> |
117 | * 方法功能:在parent結(jié)點(diǎn)下增加值為value的子結(jié)點(diǎn)tabName<p> |
118 | * 參數(shù)說明:@param parent |
119 | * 參數(shù)說明:@param tagName |
120 | * 參數(shù)說明:@param value <p> |
121 | * 返回:Element <p> |
122 | * 作者:luoc |
123 | * 日期:2005-6-22 |
124 | **/ |
125 | public Element createElement(Element parent,String tagName,String value) |
126 | { |
127 | Document doc=parent.getOwnerDocument(); |
128 | Element child=doc.createElement(tagName); |
129 | XmlOper.setElementValue(child,value); |
130 | parent.appendChild(child); |
131 | return child; |
132 | } |
133 | |
134 | /** |
135 | * 方法名稱:createAttribute<p> |
136 | * 方法功能:在parent結(jié)點(diǎn)下增加屬性 <p> |
137 | * 參數(shù)說明:@param parent |
138 | * 參數(shù)說明:@param attrName 屬性名 |
139 | * 參數(shù)說明:@param attrValue 屬性值<p> |
140 | * 返回:void <p> |
141 | * 作者:luoc |
142 | * 日期:2005-6-22 |
143 | **/ |
144 | public void createAttribute(Element parent,String attrName,String attrValue) |
145 | { |
146 | XmlOper.setElementAttr(parent,attrName,attrValue); |
147 | } |
148 | |
149 | /** |
150 | * 方法名稱:buildXmlFile<p> |
151 | * 方法功能:根據(jù)DOM生成XML文件<p> |
152 | * 參數(shù)說明: <p> |
153 | * 返回:void <p> |
154 | * 作者:luoc |
155 | * 日期:2005-6-22 |
156 | **/ |
157 | public void buildXmlFile() |
158 | { |
159 | TransformerFactory tfactory=TransformerFactory.newInstance(); |
160 | try |
161 | { |
162 | Transformer transformer=tfactory.newTransformer(); |
163 | DOMSource source= new DOMSource(doc); |
164 | logger.debug( "New DOMSource success." ); |
165 | StreamResult result= new StreamResult( new File(path)); |
166 | logger.debug( "New StreamResult success." ); |
167 | transformer.setOutputProperty( "encoding" , "GBK" ); |
168 | transformer.transform(source,result); |
169 | logger.debug( "Build XML File '" +path+ "' success." ); |
170 | } catch (TransformerConfigurationException e) |
171 | { |
172 | logger.error( "Create Transformer error:" +e); |
173 | } catch (TransformerException e) |
174 | { |
175 | logger.error( "Transformer XML file error:" +e); |
176 | } |
177 | } |
178 | |
179 | /** |
180 | * @return 返回 doc。 |
181 | */ |
182 | public Document getDoc() |
183 | { |
184 | return doc; |
185 | } |
186 | /** |
187 | * @param doc 要設(shè)置的 doc。 |
188 | */ |
189 | public void setDoc(Document doc) |
190 | { |
191 | this .doc = doc; |
192 | } |
193 | /** |
194 | * @return 返回 path。 |
195 | */ |
196 | public String getPath() |
197 | { |
198 | return path; |
199 | } |
200 | /** |
201 | * @param path 要設(shè)置的 path。 |
202 | */ |
203 | public void setPath(String path) |
204 | { |
205 | this .path = path; |
206 | } |
207 | /*全局變量*/ |
208 | private Logger logger = Logger.getLogger(getClass().getName()); |
209 | private Document doc= null ; //新創(chuàng)建的DOM |
210 | private String path= null ; //生成的XML文件絕對(duì)路徑 |
211 | } |
聯(lián)系客服