- 在线时间
- 1957 小时
- 最后登录
- 2024-6-29
- 注册时间
- 2004-4-26
- 听众数
- 49
- 收听数
- 0
- 能力
- 60 分
- 体力
- 40934 点
- 威望
- 6 点
- 阅读权限
- 255
- 积分
- 23855
- 相册
- 0
- 日志
- 0
- 记录
- 0
- 帖子
- 20501
- 主题
- 18182
- 精华
- 5
- 分享
- 0
- 好友
- 140
TA的每日心情 | 奋斗 2024-6-23 05:14 |
---|
签到天数: 1043 天 [LV.10]以坛为家III
群组: 万里江山 群组: sas讨论小组 群组: 长盛证券理财有限公司 群组: C 语言讨论组 群组: Matlab讨论组 |
网上看到的几个代码,不知道有没有用,没有测试过!
< >1.使用parseconfig方法</P>
( w- q5 Y3 K& v! Z" G< >import javax.xml.parsers.*;<BR>import org.w3c.dom.Element;<BR>import org.w3c.dom.Document;<BR>import org.w3c.dom.NodeList;</P>
( }. J& p& d- d< >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>( \; ~2 W8 k3 M# I* r4 d# E m
< > 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>
! }( x+ X. D' m0 M3 c< > nlCurrent = elmtCurrent.getElementsByTagName("URL");<BR> URL = nlCurrent.item(0).getFirstChild().getNodeValue();<BR> //////////////////System.out.println(" URL is : " + URL);</P>4 j, i& m; H. _" N5 @/ x
< > nlCurrent = elmtCurrent.getElementsByTagName("Driver");<BR> Driver = nlCurrent.item(0).getFirstChild().getNodeValue();<BR> //////////////////System.out.println(" Driver is : " + Driver);</P>: r" U1 J- U8 v. u: C
< > nlCurrent = elmtCurrent.getElementsByTagName("IP");<BR> IP = nlCurrent.item(0).getFirstChild().getNodeValue();<BR> //////////////////System.out.println(" IP is : " + IP);</P>
4 S# ]6 ?5 U% k< > nlCurrent = elmtCurrent.getElementsByTagName("UseID");<BR> UserID = nlCurrent.item(0).getFirstChild().getNodeValue();<BR> //////////////////System.out.println(" UseID is : " + UserID);</P>$ h8 H: n: d2 |0 W$ B
< > 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>6 X; p V2 n. Q% R. u4 @9 D
< > public void createcon(){<BR> try {<BR> Class.forName(Driver);<BR> } catch (ClassNotFoundException cnfe) {<BR> cnfe.printStackTrace();<BR> }</P>/ K/ A B6 ]5 w( |: _" |0 R
< > try {<BR> con = DriverManager.getConnection(URL, UserID, UserPassword);<BR> } catch (SQLException sqle) {<BR> sqle.printStackTrace();<BR> }<BR> }<BR></P>! a N& H# T4 f( G0 r0 M
< >2.使用jdom 1.1写xml文件</P>7 ]: l$ u, u! t% }9 j
< >import java.io.FileOutputStream;</P>2 d" J) \% F0 D% X. ~: l
< >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>
" i% T: l0 k8 a: O- [% A8 s( A+ c< >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>
, w; A& h3 p& M% g6 b< >root.addContent(event);<BR>document.setRootElement(root);</P>
/ R0 h0 n, J. n2 z7 V4 J) n1 b< >Test test = new Test();<BR>test.doCreateXMLFile(document , "D:\\aa.xml");<BR>}<BR>}<BR></P>
0 O7 L" }( Z5 l5 z/ i/ ~4 W: P< >3.使用JDOM+XPATH</P>. b c1 s4 X1 f, w6 L- `
< >Document doc = null;<BR> SAXBuilder sb = new SAXBuilder();<BR> doc = sb.build(xml文件);<BR></P> |
|