- 在线时间
- 1957 小时
- 最后登录
- 2024-6-29
- 注册时间
- 2004-4-26
- 听众数
- 49
- 收听数
- 0
- 能力
- 60 分
- 体力
- 40957 点
- 威望
- 6 点
- 阅读权限
- 255
- 积分
- 23862
- 相册
- 0
- 日志
- 0
- 记录
- 0
- 帖子
- 20501
- 主题
- 18182
- 精华
- 5
- 分享
- 0
- 好友
- 140
TA的每日心情 | 奋斗 2024-6-23 05:14 |
|---|
签到天数: 1043 天 [LV.10]以坛为家III
群组: 万里江山 群组: sas讨论小组 群组: 长盛证券理财有限公司 群组: C 语言讨论组 群组: Matlab讨论组 |
网上看到的几个代码,不知道有没有用,没有测试过!
< >1.使用parseconfig方法</P>
7 s, u R6 e, l& s8 X% d$ r< >import javax.xml.parsers.*;<BR>import org.w3c.dom.Element;<BR>import org.w3c.dom.Document;<BR>import org.w3c.dom.NodeList;</P>
* y4 N" L* z2 |0 E U0 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>' C) e' s+ a* P6 ]% s
< > 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>+ v' `) F" V/ ]/ I) J; N, B/ R
< > nlCurrent = elmtCurrent.getElementsByTagName("URL");<BR> URL = nlCurrent.item(0).getFirstChild().getNodeValue();<BR> //////////////////System.out.println(" URL is : " + URL);</P>* b6 |' v; r$ o- g" T
< > nlCurrent = elmtCurrent.getElementsByTagName("Driver");<BR> Driver = nlCurrent.item(0).getFirstChild().getNodeValue();<BR> //////////////////System.out.println(" Driver is : " + Driver);</P>
0 |, x* b/ m# e/ d. j< > nlCurrent = elmtCurrent.getElementsByTagName("IP");<BR> IP = nlCurrent.item(0).getFirstChild().getNodeValue();<BR> //////////////////System.out.println(" IP is : " + IP);</P>7 A0 a8 S0 l0 E2 S3 ~
< > nlCurrent = elmtCurrent.getElementsByTagName("UseID");<BR> UserID = nlCurrent.item(0).getFirstChild().getNodeValue();<BR> //////////////////System.out.println(" UseID is : " + UserID);</P>
& Q7 E m* ]$ y- Y( q< > 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>
3 s2 n* H' W3 J1 R/ g* Q" ~; X< > public void createcon(){<BR> try {<BR> Class.forName(Driver);<BR> } catch (ClassNotFoundException cnfe) {<BR> cnfe.printStackTrace();<BR> }</P>2 E7 A& O' g& h* x! f
< > try {<BR> con = DriverManager.getConnection(URL, UserID, UserPassword);<BR> } catch (SQLException sqle) {<BR> sqle.printStackTrace();<BR> }<BR> }<BR></P>: n. k+ I4 }: N8 ?0 A
< >2.使用jdom 1.1写xml文件</P>
" n$ `" ^ F* h6 g" j< >import java.io.FileOutputStream;</P>+ }3 S: ?+ v7 t
< >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>
9 o4 } g( _$ {$ R0 o. L9 b< >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>
) M: C5 s. m3 y4 {# m1 O$ k< >root.addContent(event);<BR>document.setRootElement(root);</P>
4 P. n8 g7 N F+ B. n( f+ ]< >Test test = new Test();<BR>test.doCreateXMLFile(document , "D:\\aa.xml");<BR>}<BR>}<BR></P>6 ]3 M2 y6 `: h; P) ?" x; V) B8 I! ^
< >3.使用JDOM+XPATH</P>2 O' Z) ? N w1 y5 A8 D1 `' q# i
< >Document doc = null;<BR> SAXBuilder sb = new SAXBuilder();<BR> doc = sb.build(xml文件);<BR></P> |
|