??xml version="1.0" encoding="utf-8" standalone="yes"?>国产精品激情av在线播放,亚洲国产精品日韩,91精品久久久久久久99蜜桃http://www.aygfsteel.com/landraxee/category/8020.htmlzh-cnWed, 28 Feb 2007 08:20:33 GMTWed, 28 Feb 2007 08:20:33 GMT60JDOM处理XML快速上?/title><link>http://www.aygfsteel.com/landraxee/articles/33464.html</link><dc:creator>么么?/dc:creator><author>么么?/author><pubDate>Fri, 03 Mar 2006 07:05:00 GMT</pubDate><guid>http://www.aygfsteel.com/landraxee/articles/33464.html</guid><wfw:comment>http://www.aygfsteel.com/landraxee/comments/33464.html</wfw:comment><comments>http://www.aygfsteel.com/landraxee/articles/33464.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.aygfsteel.com/landraxee/comments/commentRss/33464.html</wfw:commentRss><trackback:ping>http://www.aygfsteel.com/landraxee/services/trackbacks/33464.html</trackback:ping><description><![CDATA[?JDOM 中,XML 元素是 Element 的实例,XML 属性就?Attribute 的实例,XML 文档本n是 Document 的实例?BR>    因ؓ JDOM 对象是?Document、Element ?Attribute q些cȝ直接实例Q因此创Z个新 JDOM 对象如?Java 语言中?new 操作W一样容易。JDOM 的用是直截了当的?BR>    JDOM 使用标准?Java ~码模式。只要有可能Q它使用 Java new 操作W而不故弄玄虚使用复杂的工厂化模式Q对象操作即便对于初学用户也很方便?BR>    <BR>    本文分两步对JDOM的应用加以介l:XML创徏 ?XML解析<BR>一、XML文档创徏<BR>    我们由零开始利用JDOM生成一个XML文档。最后的l果(h文)看v来象q样Q?BR> <DIV style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><IMG src="http://www.aygfsteel.com/images/OutliningIndicators/None.gif" align=top><SPAN style="COLOR: #000000">    </SPAN><SPAN style="COLOR: #0000ff"><?</SPAN><SPAN style="COLOR: #ff00ff">xml version="1.0" encoding="UTF-8"</SPAN><SPAN style="COLOR: #0000ff">?></SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.aygfsteel.com/images/OutliningIndicators/None.gif" align=top>    </SPAN><SPAN style="COLOR: #0000ff"><</SPAN><SPAN style="COLOR: #800000">MyInfo </SPAN><SPAN style="COLOR: #ff0000">comment</SPAN><SPAN style="COLOR: #0000ff">="introduce myself"</SPAN><SPAN style="COLOR: #0000ff">></SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.aygfsteel.com/images/OutliningIndicators/None.gif" align=top>        </SPAN><SPAN style="COLOR: #0000ff"><</SPAN><SPAN style="COLOR: #800000">name</SPAN><SPAN style="COLOR: #0000ff">></SPAN><SPAN style="COLOR: #000000">kingwong</SPAN><SPAN style="COLOR: #0000ff"></</SPAN><SPAN style="COLOR: #800000">name</SPAN><SPAN style="COLOR: #0000ff">></SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.aygfsteel.com/images/OutliningIndicators/None.gif" align=top>        </SPAN><SPAN style="COLOR: #0000ff"><</SPAN><SPAN style="COLOR: #800000">sex </SPAN><SPAN style="COLOR: #ff0000">value</SPAN><SPAN style="COLOR: #0000ff">="male"</SPAN><SPAN style="COLOR: #0000ff">/></SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.aygfsteel.com/images/OutliningIndicators/None.gif" align=top>        </SPAN><SPAN style="COLOR: #0000ff"><</SPAN><SPAN style="COLOR: #800000">contact</SPAN><SPAN style="COLOR: #0000ff">></SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.aygfsteel.com/images/OutliningIndicators/None.gif" align=top>            </SPAN><SPAN style="COLOR: #0000ff"><</SPAN><SPAN style="COLOR: #800000">telephone</SPAN><SPAN style="COLOR: #0000ff">></SPAN><SPAN style="COLOR: #000000">87654321</SPAN><SPAN style="COLOR: #0000ff"></</SPAN><SPAN style="COLOR: #800000">telephone</SPAN><SPAN style="COLOR: #0000ff">></SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.aygfsteel.com/images/OutliningIndicators/None.gif" align=top>        </SPAN><SPAN style="COLOR: #0000ff"></</SPAN><SPAN style="COLOR: #800000">contact</SPAN><SPAN style="COLOR: #0000ff">></SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.aygfsteel.com/images/OutliningIndicators/None.gif" align=top>    </SPAN><SPAN style="COLOR: #0000ff"></</SPAN><SPAN style="COLOR: #800000">MyInfo</SPAN><SPAN style="COLOR: #0000ff">></SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.aygfsteel.com/images/OutliningIndicators/None.gif" align=top></SPAN></DIV>    1.?MyInfo 为根元素创徏文档<BR>        Element rootElement = new Element("MyInfo");//所有的XML元素都是 Element 的实例。根元素也不例外Q)<BR>        Document myDocument = new Document(rootElement);//以根元素作ؓ参数创徏Document对象。一个Document只有一个根Q即root元素?BR>    2.l根元素d属?BR>        Attribute rootAttri = new Attribute("comment","introduce myself");//创徏名ؓ commnet,gؓ introduce myself 的属性?BR>        rootElement.setAttribute(rootAttri);//刚创徏的属性添加到根元素?BR>        q两行代码你也可以合成一行来写,象这P<BR>        rootElement.setAttribute(new Attribute("comment","introduce myself"));<BR>        或?BR>        rootElement.setAttribute("comment","introduce myself");<BR>    3.d元素和子元素<BR>        JDOM里子元素是作?contentQ内容)d到父元素里面ȝ,所谓content是cM上面h文档?lt;name></name>之间的东东,即kingwong。罗嗦了Ҏ吧:Q?BR>        Element nameElement = new Element("name");//创徏 name 元素<BR>        nameElement.addContent("kingwong");//kingwong作ؓcontentd到name元素<BR> rootElement.addContent(nameElement);//name元素作ؓcontentd到根元素<BR> <BR> q三行你也可以合Z句,象这P<BR> rootElement.addContent((Content)(new Element("name").addContent("kingwong")));//因ؓaddContent(Content child)Ҏq回的是一个Parent接口Q而Elementcd时承了Contentcd实现了Parent接口Q所以我们把它造型成Content?BR> <BR>        我们用同LҎd带属性的子元?lt;sex value="male"/><BR>        rootElement.addContent(new Element("sex").setAttribute("value","male"));//注意q里不需要{型,因ؓaddAttribute(String name,String value)q回值就是一?Element?BR>        <BR>        同样的,我们d<contract />元素到根元素下,用法上一P只是E微复杂了一些:<BR>        rootElement.addContent((Content)(new Element("contact").addContent((Content)(new Element("telephone").addContent("87654321")))));<BR>        如果你对q种写Ş式还不太习惯Q你完全可以分步来做Q就象本节刚开始的时候一栗事实上如果层次比较多,写成分步的Ş式更清晰些,也不Ҏ出错?BR>    4.删除子元?BR>        q个操作比较单:<BR>        rootElement.removeChild("sex");//该方法返回一个布?BR>        <BR>        到目前ؓ止,我们学习了一下JDOM文生成操作。上面徏立了一个样本文,可是我们怎么知道对不对呢Q因此需要输出来看一下。我们将JDOM生成的文输出到控制収ͼ使用 JDOM ?XMLOutputter cR?BR>    5.  ?JDOM 转化?XML 文本<BR> <DIV style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><IMG src="http://www.aygfsteel.com/images/OutliningIndicators/None.gif" align=top><SPAN style="COLOR: #000000">        XMLOutputter xmlOut </SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #0000ff">new</SPAN><SPAN style="COLOR: #000000"> XMLOutputter(</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">  </SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #0000ff">true</SPAN><SPAN style="COLOR: #000000">);<BR><IMG id=Codehighlighter1_64_107_Open_Image onclick="this.style.display='none'; Codehighlighter1_64_107_Open_Text.style.display='none'; Codehighlighter1_64_107_Closed_Image.style.display='inline'; Codehighlighter1_64_107_Closed_Text.style.display='inline';" src="http://www.aygfsteel.com/images/OutliningIndicators/ExpandedBlockStart.gif" align=top><IMG id=Codehighlighter1_64_107_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_64_107_Closed_Text.style.display='none'; Codehighlighter1_64_107_Open_Image.style.display='inline'; Codehighlighter1_64_107_Open_Text.style.display='inline';" src="http://www.aygfsteel.com/images/OutliningIndicators/ContractedBlock.gif" align=top> </SPAN><SPAN style="COLOR: #0000ff">try</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN id=Codehighlighter1_64_107_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG src="http://www.aygfsteel.com/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_64_107_Open_Text><SPAN style="COLOR: #000000">{<BR><IMG src="http://www.aygfsteel.com/images/OutliningIndicators/InBlock.gif" align=top>  xmlOut.output(myDocument,System.out);<BR><IMG id=Codehighlighter1_131_157_Open_Image onclick="this.style.display='none'; Codehighlighter1_131_157_Open_Text.style.display='none'; Codehighlighter1_131_157_Closed_Image.style.display='inline'; Codehighlighter1_131_157_Closed_Text.style.display='inline';" src="http://www.aygfsteel.com/images/OutliningIndicators/ExpandedBlockStart.gif" align=top><IMG id=Codehighlighter1_131_157_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_131_157_Closed_Text.style.display='none'; Codehighlighter1_131_157_Open_Image.style.display='inline'; Codehighlighter1_131_157_Open_Text.style.display='inline';" src="http://www.aygfsteel.com/images/OutliningIndicators/ContractedBlock.gif" align=top> }</SPAN></SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #0000ff">catch</SPAN><SPAN style="COLOR: #000000"> (IOException e) </SPAN><SPAN id=Codehighlighter1_131_157_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG src="http://www.aygfsteel.com/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_131_157_Open_Text><SPAN style="COLOR: #000000">{<BR><IMG src="http://www.aygfsteel.com/images/OutliningIndicators/InBlock.gif" align=top>  e.printStackTrace();<BR><IMG src="http://www.aygfsteel.com/images/OutliningIndicators/ExpandedBlockEnd.gif" align=top> }</SPAN></SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.aygfsteel.com/images/OutliningIndicators/None.gif" align=top></SPAN></DIV> XMLOutputter 有几个格式选项。这里我们已指定希望子元素从父元素羃q两个空|q且希望元素间有I?BR> new XMLOutputter(java.lang.String indent, boolean newlines)q个Ҏ在最新版本中已经不徏议用。JDOM有一个专门的用来定义格式化输出的c:org.jdom.output.FormatQ如果你没有Ҏ的要求,有时候用里面的几个静态方法(应该可以说是预定义格式)?getPrettyFormat()可以了。我们把上面的输出格式稍微改一下,pq样Q?BR> XMLOutputter xmlOut = new XMLOutputter(Format.getPrettyFormat());  <BR>    6.JDOM文档转化为其他Ş?BR>        XMLOutputter q可输出?Writer ?OutputStream。ؓ了输出JDOM文档C个文本文Ӟ我们可以q样做:<BR> <DIV style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><IMG src="http://www.aygfsteel.com/images/OutliningIndicators/None.gif" align=top><SPAN style="COLOR: #000000">        FileWriter writer </SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #0000ff">new</SPAN><SPAN style="COLOR: #000000"> FileWriter(</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">/some/directory/myFile.xml</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">);<BR><IMG src="http://www.aygfsteel.com/images/OutliningIndicators/None.gif" align=top>        outputter.output(myDocument, writer);<BR><IMG src="http://www.aygfsteel.com/images/OutliningIndicators/None.gif" align=top>        writer.close();<BR><IMG src="http://www.aygfsteel.com/images/OutliningIndicators/None.gif" align=top></SPAN></DIV>        <BR>        XMLOutputter q可输出到字W串,以便E序后面q行再处?<BR>        Strng outString = xmlOut.outputString(myDocument);<BR>        <BR>        当然Q在输出的时候你不一定要输出所有的整个文档Q你可以选择元素q行输出Q?BR>        xmlOut.output(rootElement.getChild("name"),System.out);<BR>        一句话QJDOM非常灉|方便Q如果你惌一步研IJDOMQ请到官方网站去看一看:<A >http://www.jdom.org</A> <P>    本节CZ源码Q?BR></P> <DIV style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><IMG src="http://www.aygfsteel.com/images/OutliningIndicators/None.gif" align=top><SPAN style="COLOR: #0000ff">package</SPAN><SPAN style="COLOR: #000000"> com.cyberobject.study;<BR><IMG src="http://www.aygfsteel.com/images/OutliningIndicators/None.gif" align=top><BR><IMG src="http://www.aygfsteel.com/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #0000ff">import</SPAN><SPAN style="COLOR: #000000"> java.io.IOException;<BR><IMG src="http://www.aygfsteel.com/images/OutliningIndicators/None.gif" align=top><BR><IMG src="http://www.aygfsteel.com/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #0000ff">import</SPAN><SPAN style="COLOR: #000000"> org.jdom.Attribute;<BR><IMG src="http://www.aygfsteel.com/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #0000ff">import</SPAN><SPAN style="COLOR: #000000"> org.jdom.Content;<BR><IMG src="http://www.aygfsteel.com/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #0000ff">import</SPAN><SPAN style="COLOR: #000000"> org.jdom.Document;<BR><IMG src="http://www.aygfsteel.com/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #0000ff">import</SPAN><SPAN style="COLOR: #000000"> org.jdom.Element;<BR><IMG src="http://www.aygfsteel.com/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #0000ff">import</SPAN><SPAN style="COLOR: #000000"> org.jdom.output.Format;<BR><IMG src="http://www.aygfsteel.com/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #0000ff">import</SPAN><SPAN style="COLOR: #000000"> org.jdom.output.XMLOutputter;<BR><IMG src="http://www.aygfsteel.com/images/OutliningIndicators/None.gif" align=top><BR><IMG id=Codehighlighter1_233_392_Open_Image onclick="this.style.display='none'; Codehighlighter1_233_392_Open_Text.style.display='none'; Codehighlighter1_233_392_Closed_Image.style.display='inline'; Codehighlighter1_233_392_Closed_Text.style.display='inline';" src="http://www.aygfsteel.com/images/OutliningIndicators/ExpandedBlockStart.gif" align=top><IMG id=Codehighlighter1_233_392_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_233_392_Closed_Text.style.display='none'; Codehighlighter1_233_392_Open_Image.style.display='inline'; Codehighlighter1_233_392_Open_Text.style.display='inline';" src="http://www.aygfsteel.com/images/OutliningIndicators/ContractedBlock.gif" align=top></SPAN><SPAN id=Codehighlighter1_233_392_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff">/** */</SPAN><SPAN id=Codehighlighter1_233_392_Open_Text><SPAN style="COLOR: #008000">/**</SPAN><SPAN style="COLOR: #008000"><BR><IMG src="http://www.aygfsteel.com/images/OutliningIndicators/InBlock.gif" align=top> * </SPAN><SPAN style="COLOR: #808080">@author</SPAN><SPAN style="COLOR: #008000"> kingwong<BR><IMG src="http://www.aygfsteel.com/images/OutliningIndicators/InBlock.gif" align=top> *<BR><IMG src="http://www.aygfsteel.com/images/OutliningIndicators/InBlock.gif" align=top> * TODO To change the template for this generated type comment go to<BR><IMG src="http://www.aygfsteel.com/images/OutliningIndicators/InBlock.gif" align=top> * Window - Preferences - Java - Code Style - Code Templates<BR><IMG src="http://www.aygfsteel.com/images/OutliningIndicators/ExpandedBlockEnd.gif" align=top> </SPAN><SPAN style="COLOR: #008000">*/</SPAN></SPAN><SPAN style="COLOR: #000000"><BR><IMG id=Codehighlighter1_416_1653_Open_Image onclick="this.style.display='none'; Codehighlighter1_416_1653_Open_Text.style.display='none'; Codehighlighter1_416_1653_Closed_Image.style.display='inline'; Codehighlighter1_416_1653_Closed_Text.style.display='inline';" src="http://www.aygfsteel.com/images/OutliningIndicators/ExpandedBlockStart.gif" align=top><IMG id=Codehighlighter1_416_1653_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_416_1653_Closed_Text.style.display='none'; Codehighlighter1_416_1653_Open_Image.style.display='inline'; Codehighlighter1_416_1653_Open_Text.style.display='inline';" src="http://www.aygfsteel.com/images/OutliningIndicators/ContractedBlock.gif" align=top></SPAN><SPAN style="COLOR: #0000ff">public</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #0000ff">class</SPAN><SPAN style="COLOR: #000000"> TestJDOM </SPAN><SPAN id=Codehighlighter1_416_1653_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG src="http://www.aygfsteel.com/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_416_1653_Open_Text><SPAN style="COLOR: #000000">{<BR><IMG src="http://www.aygfsteel.com/images/OutliningIndicators/InBlock.gif" align=top><BR><IMG src="http://www.aygfsteel.com/images/OutliningIndicators/InBlock.gif" align=top> </SPAN><SPAN style="COLOR: #0000ff">public</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #0000ff">static</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #0000ff">void</SPAN><SPAN style="COLOR: #000000"> main(String[] args)<BR><IMG id=Codehighlighter1_460_1651_Open_Image onclick="this.style.display='none'; Codehighlighter1_460_1651_Open_Text.style.display='none'; Codehighlighter1_460_1651_Closed_Image.style.display='inline'; Codehighlighter1_460_1651_Closed_Text.style.display='inline';" src="http://www.aygfsteel.com/images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><IMG id=Codehighlighter1_460_1651_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_460_1651_Closed_Text.style.display='none'; Codehighlighter1_460_1651_Open_Image.style.display='inline'; Codehighlighter1_460_1651_Open_Text.style.display='inline';" src="http://www.aygfsteel.com/images/OutliningIndicators/ContractedSubBlock.gif" align=top> </SPAN><SPAN id=Codehighlighter1_460_1651_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG src="http://www.aygfsteel.com/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_460_1651_Open_Text><SPAN style="COLOR: #000000">{<BR><IMG src="http://www.aygfsteel.com/images/OutliningIndicators/InBlock.gif" align=top>  Element rootElement </SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #0000ff">new</SPAN><SPAN style="COLOR: #000000"> Element(</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">MyInfo</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">);<BR><IMG src="http://www.aygfsteel.com/images/OutliningIndicators/InBlock.gif" align=top>  Document myDocument </SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #0000ff">new</SPAN><SPAN style="COLOR: #000000"> Document(rootElement);<BR><IMG src="http://www.aygfsteel.com/images/OutliningIndicators/InBlock.gif" align=top>  <BR><IMG src="http://www.aygfsteel.com/images/OutliningIndicators/InBlock.gif" align=top></SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">  Attribute rootAttri = new Attribute("comment","introduce myself");<BR><IMG src="http://www.aygfsteel.com/images/OutliningIndicators/InBlock.gif" align=top></SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">  rootElement.setAttribute(rootAttri);</SPAN><SPAN style="COLOR: #008000"><BR><IMG src="http://www.aygfsteel.com/images/OutliningIndicators/InBlock.gif" align=top></SPAN><SPAN style="COLOR: #000000">  <BR><IMG src="http://www.aygfsteel.com/images/OutliningIndicators/InBlock.gif" align=top>  rootElement.setAttribute(</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">comment</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">introduce myself</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">);<BR><IMG src="http://www.aygfsteel.com/images/OutliningIndicators/InBlock.gif" align=top>  </SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">rootElement.setAttribute(new Attribute("comment","introduce myself"));<BR><IMG src="http://www.aygfsteel.com/images/OutliningIndicators/InBlock.gif" align=top></SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">  Element sexElement = new Element("sex");<BR><IMG src="http://www.aygfsteel.com/images/OutliningIndicators/InBlock.gif" align=top></SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">  rootElement.addContent(sexElement);<BR><IMG src="http://www.aygfsteel.com/images/OutliningIndicators/InBlock.gif" align=top>  <BR><IMG src="http://www.aygfsteel.com/images/OutliningIndicators/InBlock.gif" align=top></SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">  Element nameElement = new Element("name");<BR><IMG src="http://www.aygfsteel.com/images/OutliningIndicators/InBlock.gif" align=top></SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">  nameElement.addContent("kingwong");<BR><IMG src="http://www.aygfsteel.com/images/OutliningIndicators/InBlock.gif" align=top></SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">  rootElement.addContent(nameElement);</SPAN><SPAN style="COLOR: #008000"><BR><IMG src="http://www.aygfsteel.com/images/OutliningIndicators/InBlock.gif" align=top></SPAN><SPAN style="COLOR: #000000">  <BR><IMG src="http://www.aygfsteel.com/images/OutliningIndicators/InBlock.gif" align=top>  rootElement.addContent((Content)(</SPAN><SPAN style="COLOR: #0000ff">new</SPAN><SPAN style="COLOR: #000000"> Element(</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">name</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">).addContent(</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">kingwong</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">)));<BR><IMG src="http://www.aygfsteel.com/images/OutliningIndicators/InBlock.gif" align=top>  rootElement.addContent(</SPAN><SPAN style="COLOR: #0000ff">new</SPAN><SPAN style="COLOR: #000000"> Element(</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">sex</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">).setAttribute(</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">value</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">male</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">));<BR><IMG src="http://www.aygfsteel.com/images/OutliningIndicators/InBlock.gif" align=top>  rootElement.addContent((Content)(</SPAN><SPAN style="COLOR: #0000ff">new</SPAN><SPAN style="COLOR: #000000"> Element(</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">contract</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">).addContent((Content)(</SPAN><SPAN style="COLOR: #0000ff">new</SPAN><SPAN style="COLOR: #000000"> Element(</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">telephone</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">).addContent(</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">87654321</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">)))));<BR><IMG src="http://www.aygfsteel.com/images/OutliningIndicators/InBlock.gif" align=top>  <BR><IMG src="http://www.aygfsteel.com/images/OutliningIndicators/InBlock.gif" align=top>  rootElement.removeChild(</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">sex</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">);<BR><IMG src="http://www.aygfsteel.com/images/OutliningIndicators/InBlock.gif" align=top>  <BR><IMG src="http://www.aygfsteel.com/images/OutliningIndicators/InBlock.gif" align=top>  XMLOutputter xmlOut </SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #0000ff">new</SPAN><SPAN style="COLOR: #000000"> XMLOutputter(Format.getPrettyFormat());<BR><IMG id=Codehighlighter1_1433_1596_Open_Image onclick="this.style.display='none'; Codehighlighter1_1433_1596_Open_Text.style.display='none'; Codehighlighter1_1433_1596_Closed_Image.style.display='inline'; Codehighlighter1_1433_1596_Closed_Text.style.display='inline';" src="http://www.aygfsteel.com/images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><IMG id=Codehighlighter1_1433_1596_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_1433_1596_Closed_Text.style.display='none'; Codehighlighter1_1433_1596_Open_Image.style.display='inline'; Codehighlighter1_1433_1596_Open_Text.style.display='inline';" src="http://www.aygfsteel.com/images/OutliningIndicators/ContractedSubBlock.gif" align=top>  </SPAN><SPAN style="COLOR: #0000ff">try</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN id=Codehighlighter1_1433_1596_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG src="http://www.aygfsteel.com/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_1433_1596_Open_Text><SPAN style="COLOR: #000000">{<BR><IMG src="http://www.aygfsteel.com/images/OutliningIndicators/InBlock.gif" align=top>   xmlOut.output(myDocument,System.out);<BR><IMG src="http://www.aygfsteel.com/images/OutliningIndicators/InBlock.gif" align=top>   </SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">xmlOut.output(rootElement.getChild("name"),System.out);<BR><IMG src="http://www.aygfsteel.com/images/OutliningIndicators/InBlock.gif" align=top>   </SPAN><SPAN style="COLOR: #008000">//</SPAN><SPAN style="COLOR: #008000">String outString = xmlOut.outputString(myDocument);</SPAN><SPAN style="COLOR: #008000"><BR><IMG id=Codehighlighter1_1620_1648_Open_Image onclick="this.style.display='none'; Codehighlighter1_1620_1648_Open_Text.style.display='none'; Codehighlighter1_1620_1648_Closed_Image.style.display='inline'; Codehighlighter1_1620_1648_Closed_Text.style.display='inline';" src="http://www.aygfsteel.com/images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><IMG id=Codehighlighter1_1620_1648_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_1620_1648_Closed_Text.style.display='none'; Codehighlighter1_1620_1648_Open_Image.style.display='inline'; Codehighlighter1_1620_1648_Open_Text.style.display='inline';" src="http://www.aygfsteel.com/images/OutliningIndicators/ContractedSubBlock.gif" align=top></SPAN><SPAN style="COLOR: #000000">  }</SPAN></SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #0000ff">catch</SPAN><SPAN style="COLOR: #000000"> (IOException e) </SPAN><SPAN id=Codehighlighter1_1620_1648_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG src="http://www.aygfsteel.com/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_1620_1648_Open_Text><SPAN style="COLOR: #000000">{<BR><IMG src="http://www.aygfsteel.com/images/OutliningIndicators/InBlock.gif" align=top>   e.printStackTrace();<BR><IMG src="http://www.aygfsteel.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>  }</SPAN></SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.aygfsteel.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top> }</SPAN></SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.aygfsteel.com/images/OutliningIndicators/ExpandedBlockEnd.gif" align=top>}</SPAN></SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.aygfsteel.com/images/OutliningIndicators/None.gif" align=top><BR><IMG src="http://www.aygfsteel.com/images/OutliningIndicators/None.gif" align=top></SPAN></DIV> <P>        <BR>二、XML文解析<BR>    JDOM 不光可以很方便的建立XML文Q它的另一个用处是它能够读取ƈ操作现有?XML 数据?BR>    JDOM的解析器在org.jdom.input.*q个包里Q其中的DOMBuilder的功能是DOM模型的Document解析成JDOM模型的DocumentQSAXBuilder的功能是从文件或中解析出符合JDOM模型的XML树。由于我们经常要从一个文仉d数据Q因此我们应该采用后者作析工兗?BR>解析一个xml文Q基本可以看成以下几个步骤:<BR>    1.实例化一个合适的解析器对?BR>        本例中我们用SAXBuilder:<BR>        SAXBuilder sb = new SAXBuilder();<BR>    2.以包含XML数据的文件ؓ参数Q构Z个文对象myDocument<BR>        Document myDocument = sb.build(/some/directory/myFile.xml);<BR>    3.获到根元?BR>        Element rootElement = myDocument.getRootElement();<BR>        <BR>        一旦你获取了根元素Q你可以很方便地对它下面的子元素进行操作了Q下面对Element对象的一些常用方法作一下简单说明:<BR>        getChild("childname") q回指定名字的子节点,如果同一U有多个同名子节点,则只q回W一个;如果没有q回null倹{?BR>        getChildren("childname") q回指定名字的子节点List集合。这样你可以遍历所有的同一U同名子节点?<BR>        getAttributeValue("name") q回指定属性名字的倹{如果没有该属性则q回null,有该属性但是gؓI,则返回空字符丌Ӏ?BR>        getChildText("childname") q回指定子节点的内容文本倹{?BR>        getText() q回该元素的内容文本倹{?BR>        <BR>        q有其他没有|列出来的方法,如果需要的话,可以随时查阅JDOM的在U文档:<A >http://www.jdom.org/docs/apidocs/index.html</A>。当然你可以在你需要的地方d、删除元素操作,q记得上面的创徏XML的方法吗Q呵呵~~~<BR>        <BR>        学习C东还是从实例学v最为快P下面单D个例子,׃上面的XMLh代码来学习JDOM的XML解析。本例中d了样本XML文g里一些属性和contentQ最后我们还在contact元素里插入了一个新元素<email value="<A href="mailto:wanghua@cyberobject.com">wanghua@cyberobject.com</A>" />。尽我们实C对于XML的基本操作,l心的朋友可能会<BR>有疑问:如果XML文档的层ơ稍微复杂一些,如果嵌套多达几十上百层的话(开个玩W)Q如果靠q样从根元素一U一U地通过getChild("childname")来访问子元素的话Q将会非常痛苦!是的Q的是q样Q但是我们有另一个有力的工具XPathQؓ什么不用呢Q这是后话!先卖个关子(手敲累啦Q下回吧Q呵呵)?/P> <DIV style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee"><IMG id=Codehighlighter1_0_154_Open_Image onclick="this.style.display='none'; Codehighlighter1_0_154_Open_Text.style.display='none'; Codehighlighter1_0_154_Closed_Image.style.display='inline'; Codehighlighter1_0_154_Closed_Text.style.display='inline';" src="http://www.aygfsteel.com/images/OutliningIndicators/ExpandedBlockStart.gif" align=top><IMG id=Codehighlighter1_0_154_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_0_154_Closed_Text.style.display='none'; Codehighlighter1_0_154_Open_Image.style.display='inline'; Codehighlighter1_0_154_Open_Text.style.display='inline';" src="http://www.aygfsteel.com/images/OutliningIndicators/ContractedBlock.gif" align=top><SPAN id=Codehighlighter1_0_154_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff">/**/</SPAN><SPAN id=Codehighlighter1_0_154_Open_Text><SPAN style="COLOR: #008000">/*</SPAN><SPAN style="COLOR: #008000"><BR><IMG src="http://www.aygfsteel.com/images/OutliningIndicators/InBlock.gif" align=top> * Created on 2004-8-21<BR><IMG src="http://www.aygfsteel.com/images/OutliningIndicators/InBlock.gif" align=top> *<BR><IMG src="http://www.aygfsteel.com/images/OutliningIndicators/InBlock.gif" align=top> * TODO To change the template for this generated file go to<BR><IMG src="http://www.aygfsteel.com/images/OutliningIndicators/InBlock.gif" align=top> * Window - Preferences - Java - Code Style - Code Templates<BR><IMG src="http://www.aygfsteel.com/images/OutliningIndicators/ExpandedBlockEnd.gif" align=top> </SPAN><SPAN style="COLOR: #008000">*/</SPAN></SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.aygfsteel.com/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #0000ff">package</SPAN><SPAN style="COLOR: #000000"> com.cyberobject.study;<BR><IMG src="http://www.aygfsteel.com/images/OutliningIndicators/None.gif" align=top><BR><IMG src="http://www.aygfsteel.com/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #0000ff">import</SPAN><SPAN style="COLOR: #000000"> org.jdom.Document;<BR><IMG src="http://www.aygfsteel.com/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #0000ff">import</SPAN><SPAN style="COLOR: #000000"> org.jdom.Element;<BR><IMG src="http://www.aygfsteel.com/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #0000ff">import</SPAN><SPAN style="COLOR: #000000"> org.jdom.input.SAXBuilder;<BR><IMG src="http://www.aygfsteel.com/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #0000ff">import</SPAN><SPAN style="COLOR: #000000"> org.jdom.output.Format;<BR><IMG src="http://www.aygfsteel.com/images/OutliningIndicators/None.gif" align=top></SPAN><SPAN style="COLOR: #0000ff">import</SPAN><SPAN style="COLOR: #000000"> org.jdom.output.XMLOutputter;<BR><IMG src="http://www.aygfsteel.com/images/OutliningIndicators/None.gif" align=top><BR><IMG id=Codehighlighter1_342_501_Open_Image onclick="this.style.display='none'; Codehighlighter1_342_501_Open_Text.style.display='none'; Codehighlighter1_342_501_Closed_Image.style.display='inline'; Codehighlighter1_342_501_Closed_Text.style.display='inline';" src="http://www.aygfsteel.com/images/OutliningIndicators/ExpandedBlockStart.gif" align=top><IMG id=Codehighlighter1_342_501_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_342_501_Closed_Text.style.display='none'; Codehighlighter1_342_501_Open_Image.style.display='inline'; Codehighlighter1_342_501_Open_Text.style.display='inline';" src="http://www.aygfsteel.com/images/OutliningIndicators/ContractedBlock.gif" align=top></SPAN><SPAN id=Codehighlighter1_342_501_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff">/** */</SPAN><SPAN id=Codehighlighter1_342_501_Open_Text><SPAN style="COLOR: #008000">/**</SPAN><SPAN style="COLOR: #008000"><BR><IMG src="http://www.aygfsteel.com/images/OutliningIndicators/InBlock.gif" align=top> * </SPAN><SPAN style="COLOR: #808080">@author</SPAN><SPAN style="COLOR: #008000"> kingwong<BR><IMG src="http://www.aygfsteel.com/images/OutliningIndicators/InBlock.gif" align=top> *<BR><IMG src="http://www.aygfsteel.com/images/OutliningIndicators/InBlock.gif" align=top> * TODO To change the template for this generated type comment go to<BR><IMG src="http://www.aygfsteel.com/images/OutliningIndicators/InBlock.gif" align=top> * Window - Preferences - Java - Code Style - Code Templates<BR><IMG src="http://www.aygfsteel.com/images/OutliningIndicators/ExpandedBlockEnd.gif" align=top> </SPAN><SPAN style="COLOR: #008000">*/</SPAN></SPAN><SPAN style="COLOR: #000000"><BR><IMG id=Codehighlighter1_526_1604_Open_Image onclick="this.style.display='none'; Codehighlighter1_526_1604_Open_Text.style.display='none'; Codehighlighter1_526_1604_Closed_Image.style.display='inline'; Codehighlighter1_526_1604_Closed_Text.style.display='inline';" src="http://www.aygfsteel.com/images/OutliningIndicators/ExpandedBlockStart.gif" align=top><IMG id=Codehighlighter1_526_1604_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_526_1604_Closed_Text.style.display='none'; Codehighlighter1_526_1604_Open_Image.style.display='inline'; Codehighlighter1_526_1604_Open_Text.style.display='inline';" src="http://www.aygfsteel.com/images/OutliningIndicators/ContractedBlock.gif" align=top></SPAN><SPAN style="COLOR: #0000ff">public</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #0000ff">class</SPAN><SPAN style="COLOR: #000000"> TestJDOM2 </SPAN><SPAN id=Codehighlighter1_526_1604_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG src="http://www.aygfsteel.com/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_526_1604_Open_Text><SPAN style="COLOR: #000000">{<BR><IMG id=Codehighlighter1_567_1602_Open_Image onclick="this.style.display='none'; Codehighlighter1_567_1602_Open_Text.style.display='none'; Codehighlighter1_567_1602_Closed_Image.style.display='inline'; Codehighlighter1_567_1602_Closed_Text.style.display='inline';" src="http://www.aygfsteel.com/images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><IMG id=Codehighlighter1_567_1602_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_567_1602_Closed_Text.style.display='none'; Codehighlighter1_567_1602_Open_Image.style.display='inline'; Codehighlighter1_567_1602_Open_Text.style.display='inline';" src="http://www.aygfsteel.com/images/OutliningIndicators/ContractedSubBlock.gif" align=top> </SPAN><SPAN style="COLOR: #0000ff">public</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #0000ff">static</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #0000ff">void</SPAN><SPAN style="COLOR: #000000"> main(String[] args)</SPAN><SPAN id=Codehighlighter1_567_1602_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG src="http://www.aygfsteel.com/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_567_1602_Open_Text><SPAN style="COLOR: #000000">{<BR><IMG src="http://www.aygfsteel.com/images/OutliningIndicators/InBlock.gif" align=top> SAXBuilder sb </SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #0000ff">new</SPAN><SPAN style="COLOR: #000000"> SAXBuilder();<BR><IMG src="http://www.aygfsteel.com/images/OutliningIndicators/InBlock.gif" align=top>    </SPAN><SPAN style="COLOR: #0000ff">try</SPAN><SPAN style="COLOR: #000000"><BR><IMG id=Codehighlighter1_616_1536_Open_Image onclick="this.style.display='none'; Codehighlighter1_616_1536_Open_Text.style.display='none'; Codehighlighter1_616_1536_Closed_Image.style.display='inline'; Codehighlighter1_616_1536_Closed_Text.style.display='inline';" src="http://www.aygfsteel.com/images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><IMG id=Codehighlighter1_616_1536_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_616_1536_Closed_Text.style.display='none'; Codehighlighter1_616_1536_Open_Image.style.display='inline'; Codehighlighter1_616_1536_Open_Text.style.display='inline';" src="http://www.aygfsteel.com/images/OutliningIndicators/ContractedSubBlock.gif" align=top>    </SPAN><SPAN id=Codehighlighter1_616_1536_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG src="http://www.aygfsteel.com/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_616_1536_Open_Text><SPAN style="COLOR: #000000">{        <BR><IMG src="http://www.aygfsteel.com/images/OutliningIndicators/InBlock.gif" align=top>     Document doc </SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000"> sb.build(</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">myFile.xml</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">);<BR><IMG src="http://www.aygfsteel.com/images/OutliningIndicators/InBlock.gif" align=top>  Element root </SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000"> doc.getRootElement();<BR><IMG src="http://www.aygfsteel.com/images/OutliningIndicators/InBlock.gif" align=top>  <BR><IMG src="http://www.aygfsteel.com/images/OutliningIndicators/InBlock.gif" align=top>  String str1 </SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000"> root.getAttributeValue(</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">comment</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">);<BR><IMG src="http://www.aygfsteel.com/images/OutliningIndicators/InBlock.gif" align=top>  System.out.println(</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">Root Element's comment attribute is : </SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #000000">+</SPAN><SPAN style="COLOR: #000000"> str1);<BR><IMG src="http://www.aygfsteel.com/images/OutliningIndicators/InBlock.gif" align=top>  String str2 </SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000"> root.getChild(</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">sex</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">).getAttributeValue(</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">value</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">);<BR><IMG src="http://www.aygfsteel.com/images/OutliningIndicators/InBlock.gif" align=top>  System.out.println(</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">sex Element's value attribute is : </SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #000000">+</SPAN><SPAN style="COLOR: #000000"> str2);<BR><IMG src="http://www.aygfsteel.com/images/OutliningIndicators/InBlock.gif" align=top>  String str3 </SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000"> root.getChildText(</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">name</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">);<BR><IMG src="http://www.aygfsteel.com/images/OutliningIndicators/InBlock.gif" align=top>  System.out.println(</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">name Element's content is :</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #000000">+</SPAN><SPAN style="COLOR: #000000"> str3);<BR><IMG src="http://www.aygfsteel.com/images/OutliningIndicators/InBlock.gif" align=top>  String str4 </SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000"> root.getChild(</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">contact</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">).getChildText(</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">telephone</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">);<BR><IMG src="http://www.aygfsteel.com/images/OutliningIndicators/InBlock.gif" align=top>  System.out.println(</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">contact Element's telephone subelement content is : </SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #000000">+</SPAN><SPAN style="COLOR: #000000"> str4 </SPAN><SPAN style="COLOR: #000000">+</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">\n</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">);<BR><IMG src="http://www.aygfsteel.com/images/OutliningIndicators/InBlock.gif" align=top>  Element inputElement </SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000"> root.getChild(</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">contact</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">); <BR><IMG src="http://www.aygfsteel.com/images/OutliningIndicators/InBlock.gif" align=top>  inputElement.addContent(</SPAN><SPAN style="COLOR: #0000ff">new</SPAN><SPAN style="COLOR: #000000"> Element(</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">email</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">).setAttribute(</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">value</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">,</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">wanghua@cyberobject.com</SPAN><SPAN style="COLOR: #000000">"</SPAN><SPAN style="COLOR: #000000">));<BR><IMG src="http://www.aygfsteel.com/images/OutliningIndicators/InBlock.gif" align=top>  <BR><IMG src="http://www.aygfsteel.com/images/OutliningIndicators/InBlock.gif" align=top>  XMLOutputter xmlOut </SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #0000ff">new</SPAN><SPAN style="COLOR: #000000"> XMLOutputter(Format.getPrettyFormat());<BR><IMG src="http://www.aygfsteel.com/images/OutliningIndicators/InBlock.gif" align=top>     String outStr </SPAN><SPAN style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000"> xmlOut.outputString(root);<BR><IMG src="http://www.aygfsteel.com/images/OutliningIndicators/InBlock.gif" align=top>     System.out.println(outStr);<BR><IMG src="http://www.aygfsteel.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>    }</SPAN></SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.aygfsteel.com/images/OutliningIndicators/InBlock.gif" align=top>    </SPAN><SPAN style="COLOR: #0000ff">catch</SPAN><SPAN style="COLOR: #000000">(Exception e)<BR><IMG id=Codehighlighter1_1565_1600_Open_Image onclick="this.style.display='none'; Codehighlighter1_1565_1600_Open_Text.style.display='none'; Codehighlighter1_1565_1600_Closed_Image.style.display='inline'; Codehighlighter1_1565_1600_Closed_Text.style.display='inline';" src="http://www.aygfsteel.com/images/OutliningIndicators/ExpandedSubBlockStart.gif" align=top><IMG id=Codehighlighter1_1565_1600_Closed_Image style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_1565_1600_Closed_Text.style.display='none'; Codehighlighter1_1565_1600_Open_Image.style.display='inline'; Codehighlighter1_1565_1600_Open_Text.style.display='inline';" src="http://www.aygfsteel.com/images/OutliningIndicators/ContractedSubBlock.gif" align=top>    </SPAN><SPAN id=Codehighlighter1_1565_1600_Closed_Text style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff"><IMG src="http://www.aygfsteel.com/images/dot.gif"></SPAN><SPAN id=Codehighlighter1_1565_1600_Open_Text><SPAN style="COLOR: #000000">{<BR><IMG src="http://www.aygfsteel.com/images/OutliningIndicators/InBlock.gif" align=top>        e.printStackTrace();<BR><IMG src="http://www.aygfsteel.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>    }</SPAN></SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.aygfsteel.com/images/OutliningIndicators/ExpandedSubBlockEnd.gif" align=top>}</SPAN></SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.aygfsteel.com/images/OutliningIndicators/ExpandedBlockEnd.gif" align=top>}</SPAN></SPAN><SPAN style="COLOR: #000000"><BR><IMG src="http://www.aygfsteel.com/images/OutliningIndicators/None.gif" align=top></SPAN></DIV> <P><BR> </P><BR><img src ="http://www.aygfsteel.com/landraxee/aggbug/33464.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.aygfsteel.com/landraxee/" target="_blank">么么?/a> 2006-03-03 15:05 <a href="http://www.aygfsteel.com/landraxee/articles/33464.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>10Ƒָ用Java试工具http://www.aygfsteel.com/landraxee/articles/33239.html么么?/dc:creator>么么?/author>Thu, 02 Mar 2006 08:28:00 GMThttp://www.aygfsteel.com/landraxee/articles/33239.html1.        Segue公司?/B>Silkpd产品
Segue公司一直专注于软g质量优化领域。在S egue的品套件中Q拥有业内最强劲且最Ҏ使用的、用于企业应用测试、调优和监测的自动化工具Q能够帮助用户保障应用在其生命周期内的可靠性和性能?/DIV>
(1)     SilkPerformer——企业性能试工具
u      企业U自动化试工具能够支持多种pȝQ如Java?Net、Wireless、COM、CORBA、Oracle、Citrix、MetaFrame、客h/服务器、以及各UERP/CRM应用
u      多项专利技术精模拟各U复杂的企业环境
u      可视化脚本记录功能及自定义工L化了试创徏工作
u      SilkPerformer的Java/.NET览器以及JUnit/NUnit试输入功能化了对ƈ发访问情况下q程应用lg的早期负载测试工?
u      方便易用Q工作流向导会逐步引导用户完成整个试程
(2)     SilkTest International——业内唯一的Unicode功能试工具
u      SilkBean 充分利用 Java 语言的“编写一ơ,随处使用”的优点Q让用户不必修改现有的脚本而能够在多种Z Unix 的系l上q行能够识别多种开发^収ͼ如Java、JavaScript、HTML、ActiveX、Visual Basic 和C/C++{?/DIV>
u      一套脚本可供所有支持的语言使用
u      内置的错误恢复系l不仅具有自定义功能Q可q行无h看守的自动测?/DIV>
赛格?Segue)公司是全球范围内专注于Y件质量优化解x案的领导者?005q_赛格?Segue)公司在中国设立了专门的销售服务公司,因此Q赛格瑞(Segue)公司的Y件测试品在中国有了更好的技术支持?/DIV>
参考网站:http://www.segue.com.cn/
推荐指数Q★★★★★
2.        MaxQ
MaxQ是一个免费的功能试工具。它包括一个HTTP代理工具Q可以录制测试脚本,q提供回放测试过E的命o行工兗测试结果的l计图表cM于一些较昂贵的商用测试工兗MaxQ希望能够提供一些关键的功能Q比如HTTP试录制回放功能Qƈ支持脚本?/DIV>
参考网站:http://maxq.tigris.org/  
推荐指数Q★★★☆☆
3.        Httpunit
HttpUnit是一个开源的试工具Q是ZJUnit的一个测试框Ӟ主要x于测试Web应用Q解决用JUnit框架无法对远EWeb内容q行试的弊端?/DIV>
HttpUnit提供的帮助类让测试者可以通过Javacd服务器进行交互,q且服务器端的响应当作文本或者DOM对象q行处理。HttpUnitq提供了一个模拟Servlet容器Q让试者不需要发布ServletQ就可以对Servlet的内部代码进行测试。本文中作者将详细的介l如何用HttpUnit提供的类完成集成试?/DIV>
参考网站:http://www.httpunit.org/
推荐指数Q★★★☆☆
4.        Junit 
是通用的测?nbsp;java E序的测试框架JUnit可以对Java代码q行白盒试。通过JUnitk可以用mock objectsq行隔离试Q用Cactusq行容器内测试;用Ant和Mavenq行自动构徏Q在Eclipse内进行测试;对Java应用E序、Filter、Servlet、EJB、JSP、数据库应用E序、Taglib{进行单元测试?/DIV>
参考网站:http://www.junit.org/
推荐指数Q★★★★★
5.        Jtest
Jtest是Parasoft公司推出的一N对java语言的自动化白盒试工具,它通过自动实现java的单元测试和代码标准校验Q来提高代码的可靠性。Jtest先分析每个javac,然后自动生成junit试用例q执行用例,从而实C码的最大覆盖,q将代码q行时未处理的异常暴露出来;另外Q它q可以检查以DbCQDesign by ContractQ规范开发的代码的正性。用戯可以通过扩展试用例的自动生成器来添加更多的junit用例。Jtestq能按照现有的超q?50个编码标准来查ƈ自动U正大多数常见的~码规则上的偏差Q用户可自定义这些标准,通过单的几个点击Q就能预防类g未处理异常、函数错误、内存泄漏、性能问题、安全隐患这L代码问题?/DIV>
JTest最大的优势在于静态代码分析,至于自动生成试代码Q当然生成测试代码框架也是不错的Q但要做好单元测试用戯要做大量的工作?/DIV>
参考网站:http://www.parasoft.com/jsp/aep/aep.jsp
推荐指数Q★★★★☆
6.        Hansel
Hansel 是一个测试覆盖率的工具——与用于单元试?JUnit framework 盔R成,很容易检查单元测试套件的覆盖情况?/DIV>
参考网站:http://hansel.sourceforge.net/
推荐指数Q★★☆☆☆
7.        Cactus
Cactus是一个基于JUnit框架的简单测试框Ӟ用来单元试服务端Java代码。Cactus框架的主要目标是能够单元试服务端的使用Servlet对象的JavaҎ如HttpServletRequest,HttpServletResponse,HttpSession{?/DIV>
针对外部可测试组件运行时Q需要把JUnit试q行为发送HTTPhl组件的客户端进E。ؓ了在服务器容器内部运行JUnit试Q可以用Cactus框架Q它是一个免费的开源框Ӟ是Apache Jakarta目的一部分。Cactus 包含了关于JUnit客户端如何连接到服务器,然后使测试运行的详细信息?/DIV>
参考网站:http://jakarta.apache.org/cactus/
推荐指数Q★★★★☆
8.        JFCUnit
JFCUnit使得你能够ؓJava偏移应用E序~写试例子。它Z用代码打开的窗口上获得句柄提供了支持;为在一个部件层ơ定位部件提供支持;为在部g中发起事Ӟ例如按一个按钮)以及以线E安全方式处理部件测试提供支持?/DIV>
参考网站:http://jfcunit.sourceforge.net/
推荐指数Q★★★☆☆
9.        StrutsTestCase
StrutsTestCaseQSTCQ框架是一个开源框Ӟ用来试Z Struts ?Web 应用E序。这个框架允许您在以下方面进行测试:
u      ?ActionForm cM的验证逻辑Qvalidate() ҎQ?/DIV>
u      ?Action cM的业务逻辑Qexecute() ҎQ?
u      动作转发QAction ForwardsQ?
u      转发 JSP
STC 支持两种试cdQ?/DIV>
u      Mock Ҏ —?在这U方法中Q通过模拟容器提供的对象(HttpServletRequest?HttpServletResponse ?ServletContextQ,STC 不用把应用程序部|在应用服务器中Q就可以对其q行试?/DIV>
u      Cactus Ҏ —?q种Ҏ用于集成试阶段Q在q种Ҏ中,应用E序要部|在容器中,所以可以像q行其他 JUnit 试用例那样q行试用例?/DIV>
参考网站:http:// strutstestcase.sourceforge.net/
推荐指数Q★★★★☆
10.    TestNG
TestNG是根据JUnit ?NUnit思想而构建的一个测试框Ӟ但是TestNG增加了许多新的功能得它变得更加强大与容易用比如:
u      支持JSR 175注释QJDK 1.4利用JavaDoc注释同样也支持)
u      灉|的Test配置
u      支持默认的runtime和logging JDK功能
u      强大的执行模型(不再TestSuiteQ?/DIV>
u      支持独立的测试方?/DIV>
参考网站:http://testng.org/
推荐指数Q★★★★☆


]]> վ֩ģ壺 | | | | | | | ˷| | | | | Ͻ| | | | | Һ| ɾ| | Ǩ| ݳ| | | | | | | | | | ͼ| ʡ| ʡ| Ӻ| | Ӧñر| | | ɽ| ۳|