- 在线时间
- 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>
9 Z% G$ r1 |8 |- v m, i3 i" E5 a* E( z< >import javax.xml.parsers.*;<BR>import org.w3c.dom.Element;<BR>import org.w3c.dom.Document;<BR>import org.w3c.dom.NodeList;</P>
e0 k" W1 J3 E3 v$ G; w( 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>+ \# O2 R& B; \
< > 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>
! F$ M1 d+ s3 C1 T; q& N( o: B< > nlCurrent = elmtCurrent.getElementsByTagName("URL");<BR> URL = nlCurrent.item(0).getFirstChild().getNodeValue();<BR> //////////////////System.out.println(" URL is : " + URL);</P>! G( V) `1 A$ ~7 j' O( b
< > nlCurrent = elmtCurrent.getElementsByTagName("Driver");<BR> Driver = nlCurrent.item(0).getFirstChild().getNodeValue();<BR> //////////////////System.out.println(" Driver is : " + Driver);</P>( X: Q3 J) \* N' Q+ ]+ }5 i) p, Q, S
< > nlCurrent = elmtCurrent.getElementsByTagName("IP");<BR> IP = nlCurrent.item(0).getFirstChild().getNodeValue();<BR> //////////////////System.out.println(" IP is : " + IP);</P>
7 ~! ?# } S$ ~: g< > nlCurrent = elmtCurrent.getElementsByTagName("UseID");<BR> UserID = nlCurrent.item(0).getFirstChild().getNodeValue();<BR> //////////////////System.out.println(" UseID is : " + UserID);</P>6 }* v2 K+ g5 w) H/ @
< > 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>
* F$ S" z1 u5 J, R+ U: p" Z& S/ ^< > public void createcon(){<BR> try {<BR> Class.forName(Driver);<BR> } catch (ClassNotFoundException cnfe) {<BR> cnfe.printStackTrace();<BR> }</P>) J% h9 p+ z2 k0 I% j! g# L" B
< > try {<BR> con = DriverManager.getConnection(URL, UserID, UserPassword);<BR> } catch (SQLException sqle) {<BR> sqle.printStackTrace();<BR> }<BR> }<BR></P>
$ @; ^& g& H6 Y: s/ d5 O2 [6 V< >2.使用jdom 1.1写xml文件</P>6 S% a7 V- s9 H
< >import java.io.FileOutputStream;</P>
3 ^9 u/ H" P# X/ c< >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>
& w1 ]* A4 L% ^$ n( F< >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>
+ F4 l: t& c3 z- l< >root.addContent(event);<BR>document.setRootElement(root);</P>8 T; ^" [& Z6 \3 Y
< >Test test = new Test();<BR>test.doCreateXMLFile(document , "D:\\aa.xml");<BR>}<BR>}<BR></P>1 O0 Y+ q, r" r) `) g
< >3.使用JDOM+XPATH</P>% w2 `. J/ y1 ^" B" ^
< >Document doc = null;<BR> SAXBuilder sb = new SAXBuilder();<BR> doc = sb.build(xml文件);<BR></P> |
|