国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

從XML元素結構到JAVA實現

2019-11-18 13:54:38
字體:
來源:轉載
供稿:網友

前幾個月,做項目時整理過一些xml操作的程序。這里根據自己的編程習慣再做一下整理。XML操作最基本的是SAX,DOM了。但這里不是談SAX,DOM怎么使用。而是從XML元素的角度談其java的實現。XML是由多個元素組成,可以分成XMLElement、XMLSimpleElement、XMLComplexElement、XMLCollection、XMLCollectionElement等幾種基本類型,從類名你基本就可以判定出該類所描述的XML對象了。



下面以一個例子來做描述:



<?XML version="1.0" encoding="GB2312"?>



<Package name = "abc">



    <File name = "file">



        <Sheet  name = "sheet">



           <Styles>



               <style id = "0" name = "a">



                   <align>2</align>



                   <borders>



                      <border id = "0" type = "left" value = "1" />



                      <border id = "1" type = "right" value = "3" />



                   </borders>



                  <font name = "宋體" color = "3" height = "20" />



               </style>



           </Styles>



           <Columns>



               <Column id = "0"  columnid = "0" width = "10" />



           </Columns>



           <Regions>



               <Region id = "0" rowid = "1" columnform = "0" columnto = "3" />



           </Regions>



           <Cells>



               <cell  id = "1" row="0" column = "0" style = "a"  value ="測試"/>



               <cell  id = "2" row="2" column = "2" value =" 測試2" />



               </Cells>



        </Sheet>



    </File>



</Package>



 



該配置文件是個XML—>Excel的XML文件,描述了Excel中的一些對象,比如文件名,字體,行,列等。其中Package是一個XMLComplexElement(混合類型),Cells(單元格集)是個XMLCollection(容器類),cell (單元格)是XMLCollectionElement(容器中的元素)<cell  id = "1" row="0" column = "0" style = "a"  value ="測試"/>



中的id 就是XMLAttribute(屬性)。所有的XML文件都是由這些基本的元素組成。定義出最基本的XML元素后,那么在程式中怎么也把它們之間的關系定義出來呢?以cell元素為例子代碼如下:



public class Cell extends XMLCollectionElement {



       PRivate XMLAttribute attrRow=new XMLAttribute("row");



       private XMLAttribute attrStyle=new XMLAttribute("style");



       private XMLAttribute attrColumn=new XMLAttribute("column");



       private XMLAttribute attrValue=new XMLAttribute("value");



       private XMLInterface XMLInterface = null ;



      



    public Cell (Cells ass) {



             super(ass);



             fillStrUCture();



       }



       protected void fillStructure() {



                 super.fillStructure();



              attrId.setReadOnly(true);



                 isRequired=true;



                 complexStructure.add(attrStyle);



                 complexStructure.add(attrRow);



                 complexStructure.add(attrColumn);



                 complexStructure.add(attrValue);



       }



}



 



源代碼下載 http://www.51sports.org/XML.rar

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 沁源县| 射阳县| 舟山市| 宜城市| 杭锦后旗| 中宁县| 高雄市| 闽清县| 高淳县| 博湖县| 昆山市| 云浮市| 根河市| 江门市| 巩留县| 阿拉善盟| 毕节市| 民丰县| 乡城县| 汶川县| 巫溪县| 隆昌县| 台前县| 溧阳市| 彭山县| 永清县| 邵阳县| 额济纳旗| 雷山县| 靖江市| 丰顺县| 岳池县| 崇阳县| 全南县| 湘潭县| 天门市| 神池县| 商河县| 花垣县| 偏关县| 洪洞县|