- 在线时间
- 1957 小时
- 最后登录
- 2024-6-29
- 注册时间
- 2004-4-26
- 听众数
- 49
- 收听数
- 0
- 能力
- 60 分
- 体力
- 40960 点
- 威望
- 6 点
- 阅读权限
- 255
- 积分
- 23863
- 相册
- 0
- 日志
- 0
- 记录
- 0
- 帖子
- 20501
- 主题
- 18182
- 精华
- 5
- 分享
- 0
- 好友
- 140
TA的每日心情 | 奋斗 2024-6-23 05:14 |
|---|
签到天数: 1043 天 [LV.10]以坛为家III
群组: 万里江山 群组: sas讨论小组 群组: 长盛证券理财有限公司 群组: C 语言讨论组 群组: Matlab讨论组 |
网上看到的几个代码,不知道有没有用,没有测试过!
< >1.使用parseconfig方法</P>& T! i1 u% `' j; b' ?( A
< >import javax.xml.parsers.*;<BR>import org.w3c.dom.Element;<BR>import org.w3c.dom.Document;<BR>import org.w3c.dom.NodeList;</P>$ W: z! ]! N& m, u4 O+ R% b
< >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>0 n( I/ @8 [( N) T' I1 K
< > 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>
" r; y% F5 c5 K9 C! V8 p< > nlCurrent = elmtCurrent.getElementsByTagName("URL");<BR> URL = nlCurrent.item(0).getFirstChild().getNodeValue();<BR> //////////////////System.out.println(" URL is : " + URL);</P>
& b: U: `' ?: Q0 A8 F< > nlCurrent = elmtCurrent.getElementsByTagName("Driver");<BR> Driver = nlCurrent.item(0).getFirstChild().getNodeValue();<BR> //////////////////System.out.println(" Driver is : " + Driver);</P>/ T& E# q% M7 b9 l! {
< > nlCurrent = elmtCurrent.getElementsByTagName("IP");<BR> IP = nlCurrent.item(0).getFirstChild().getNodeValue();<BR> //////////////////System.out.println(" IP is : " + IP);</P>
5 `, x; m7 T% @* n% J< > nlCurrent = elmtCurrent.getElementsByTagName("UseID");<BR> UserID = nlCurrent.item(0).getFirstChild().getNodeValue();<BR> //////////////////System.out.println(" UseID is : " + UserID);</P>! F( E4 L6 o' d$ q. \3 k
< > 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>& @4 o" I. N, l) z- p% a" m
< > public void createcon(){<BR> try {<BR> Class.forName(Driver);<BR> } catch (ClassNotFoundException cnfe) {<BR> cnfe.printStackTrace();<BR> }</P>: s# l# z4 ^ R7 P8 a4 g" a
< > try {<BR> con = DriverManager.getConnection(URL, UserID, UserPassword);<BR> } catch (SQLException sqle) {<BR> sqle.printStackTrace();<BR> }<BR> }<BR></P>7 k8 T% s, S: V7 |2 T" v
< >2.使用jdom 1.1写xml文件</P>* R( _# U$ D2 n4 }8 x
< >import java.io.FileOutputStream;</P>. V W2 E9 r& L O
< >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>- Q3 I. }' [6 O9 H( @! H
< >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>
7 s4 a3 |& F/ P: D: D" H( d< >root.addContent(event);<BR>document.setRootElement(root);</P>8 w3 `+ n3 c9 ?% G1 \1 J2 Z K
< >Test test = new Test();<BR>test.doCreateXMLFile(document , "D:\\aa.xml");<BR>}<BR>}<BR></P>7 e5 a8 a3 D' `0 n) T) N8 q
< >3.使用JDOM+XPATH</P>
7 `, g% R4 e7 B# }" _/ l! \4 ?; ]6 w< >Document doc = null;<BR> SAXBuilder sb = new SAXBuilder();<BR> doc = sb.build(xml文件);<BR></P> |
|