- 在线时间
- 1957 小时
- 最后登录
- 2024-6-29
- 注册时间
- 2004-4-26
- 听众数
- 49
- 收听数
- 0
- 能力
- 60 分
- 体力
- 40956 点
- 威望
- 6 点
- 阅读权限
- 255
- 积分
- 23861
- 相册
- 0
- 日志
- 0
- 记录
- 0
- 帖子
- 20501
- 主题
- 18182
- 精华
- 5
- 分享
- 0
- 好友
- 140
TA的每日心情 | 奋斗 2024-6-23 05:14 |
|---|
签到天数: 1043 天 [LV.10]以坛为家III
群组: 万里江山 群组: sas讨论小组 群组: 长盛证券理财有限公司 群组: C 语言讨论组 群组: Matlab讨论组 |
网上看到的几个代码,不知道有没有用,没有测试过!
< >1.使用parseconfig方法</P>
g; f1 }( w, s! Y. g- w" b< >import javax.xml.parsers.*;<BR>import org.w3c.dom.Element;<BR>import org.w3c.dom.Document;<BR>import org.w3c.dom.NodeList;</P>
# T5 Q4 h( }( N2 r' @+ G, v/ ]< >public ParseConfig(LinkedList input , LinkedList output) {<BR> //所要解析的xml文件<BR> fileURI = ( (String)new File(System.getProperty("user.dir")).getPath()) +<BR> "<a>\\System.xml</A>";<BR> //////////////////System.out.println(fileURI);<BR> try {<BR> DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();<BR> DocumentBuilder db = dbf.newDocumentBuilder();<BR> Document doc = db.parse(fileURI);<BR> Element elmt = doc.getDocumentElement();<BR> NodeList nl = elmt.getElementsByTagName("DataSource");//DataSource是xml文件中的一个标签</P>
; X0 _, u9 u% B' C, P7 V< > for (int i = 0; i < nl.getLength(); i++) {<BR> Element elmtCurrent = (Element) nl.item(i);<BR> NodeList nlCurrent = elmtCurrent.getElementsByTagName("DS");<BR> if (nlCurrent.item(0).getFirstChild().getNodeValue().equals(ds)) {<BR> DS = nlCurrent.item(0).getFirstChild().getNodeValue();<BR> //////////////////System.out.println(" DS is : " + DS);</P>2 T" l' J6 d) z
< > nlCurrent = elmtCurrent.getElementsByTagName("URL");<BR> URL = nlCurrent.item(0).getFirstChild().getNodeValue();<BR> //////////////////System.out.println(" URL is : " + URL);</P>+ D) a5 T, O+ G& D, e, v, G5 w
< > nlCurrent = elmtCurrent.getElementsByTagName("Driver");<BR> Driver = nlCurrent.item(0).getFirstChild().getNodeValue();<BR> //////////////////System.out.println(" Driver is : " + Driver);</P>
* I# f& N ]; t' y+ k q& P6 A< > nlCurrent = elmtCurrent.getElementsByTagName("IP");<BR> IP = nlCurrent.item(0).getFirstChild().getNodeValue();<BR> //////////////////System.out.println(" IP is : " + IP);</P>
% p0 Z! U; r" e' S< > nlCurrent = elmtCurrent.getElementsByTagName("UseID");<BR> UserID = nlCurrent.item(0).getFirstChild().getNodeValue();<BR> //////////////////System.out.println(" UseID is : " + UserID);</P>
- ^6 ]3 t$ T5 z; x& O+ e9 W< > nlCurrent = elmtCurrent.getElementsByTagName("UsePassword");<BR> UserPassword = nlCurrent.item(0).getFirstChild().getNodeValue();<BR> //////////////////System.out.println(" UsePassword is : " + UserPassword);<BR> } else<BR> continue;<BR> }<BR> } catch (IOException ie) {<BR> ie.printStackTrace();<BR> } catch (SAXException se) {<BR> se.printStackTrace();<BR> } catch (ParserConfigurationException pce) {<BR> pce.printStackTrace();<BR> }<BR> createcon();<BR> process(input , output);<BR> release();<BR> }</P>
2 Z1 S6 A& e% L$ Z) x< > public void createcon(){<BR> try {<BR> Class.forName(Driver);<BR> } catch (ClassNotFoundException cnfe) {<BR> cnfe.printStackTrace();<BR> }</P>
8 ]6 w, d q$ m9 w, G< > try {<BR> con = DriverManager.getConnection(URL, UserID, UserPassword);<BR> } catch (SQLException sqle) {<BR> sqle.printStackTrace();<BR> }<BR> }<BR></P>/ P0 v: Z; l* X
< >2.使用jdom 1.1写xml文件</P>* m" n$ b3 q! @& E( ^
< >import java.io.FileOutputStream;</P>
6 e4 x5 ?: k! Y# S0 Y< >import org.jdom.Document;<BR>import org.jdom.Element;<BR>import org.jdom.output.XMLOutputter;<BR>public class Test {<BR>/**<BR> * 生成需要的XML文件<BR> *<BR> */<BR>private boolean doCreateXMLFile(Document doc , String filePath) {<BR>try{<BR>XMLOutputter xmlout= new XMLOutputter("",true,"gb2312");//08/19 EDIT<BR>FileOutputStream output = new FileOutputStream(filePath);<BR>xmlout.output(doc,output);<BR>xmlout.clone();<BR>xmlout = null;<BR>output.close();<BR>output = null;<BR>}catch(Exception e){<BR>e.printStackTrace();<BR>return false;<BR>}<BR>return true;<BR>}</P>
6 w: [* A1 [, G4 D< >public static void main(String args[]){<BR>byte[] aa = "aa".getBytes();<BR>System.out.println(aa);<BR>Document document = new Document();<BR>Element root = new Element("Message");<BR>root.setAttribute("vendor","wiscom");<BR>Element event = new Element("Event");<BR>event.setAttribute("target" , "uid");<BR>event.setAttribute("evenTime" , "" + System.currentTimeMillis());<BR>event.setAttribute("eventType" , "cc");<BR>Element attributeValuePairElem = new Element("AttributeValuePair");<BR>Element attributeElem = new Element("Attribute");<BR>attributeElem.setAttribute("name" , "email" );<BR>Element valueElem = new Element("Value");<BR>valueElem.setText("<a href="mailttyzhg@wiscom.com.cn" target="_blank" >tyzhg@wiscom.com.cn</A>");<BR>attributeElem.addContent(valueElem);<BR>attributeValuePairElem.addContent(attributeElem);<BR>event.addContent(attributeValuePairElem);</P>
0 w$ u& s" x3 m, k5 f( n/ Y< >root.addContent(event);<BR>document.setRootElement(root);</P>
9 Q( l8 ~% X2 Y1 y7 f% y L G< >Test test = new Test();<BR>test.doCreateXMLFile(document , "D:\\aa.xml");<BR>}<BR>}<BR></P>
: B6 v/ E$ ^, q% @1 q% f9 H& P< >3.使用JDOM+XPATH</P>
( `# M) H1 j$ q< >Document doc = null;<BR> SAXBuilder sb = new SAXBuilder();<BR> doc = sb.build(xml文件);<BR></P> |
|