• 软件测试技术
  • 软件测试博客
  • 软件测试视频
  • 开源软件测试技术
  • 软件测试论坛
  • 软件测试沙龙
  • 软件测试资料下载
  • 软件测试杂志
  • 软件测试人才招聘
    暂时没有公告

字号: 小 中 大 | 推荐给好友 上一篇 | 下一篇

从XML元素结构到JAVA实现

发布: 2007-7-01 18:47 | 作者: admin | 来源: | 查看: 12次 | 进入软件测试论坛讨论

领测软件测试网
前几个月,做项目时整理过一些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


文章来源于领测软件测试网 https://www.ltesting.net/


关于领测软件测试网 | 领测软件测试网合作伙伴 | 广告服务 | 投稿指南 | 联系我们 | 网站地图 | 友情链接
版权所有(C) 2003-2010 TestAge(领测软件测试网)|领测国际科技(北京)有限公司|软件测试工程师培训网 All Rights Reserved
北京市海淀区中关村南大街9号北京理工科技大厦1402室 京ICP备10010545号-5
技术支持和业务联系:[email protected] 电话:010-51297073

软件测试 | 领测国际 | ISTQB | ISTQB官网 | TMMi | TMMi认证 | 国际软件测试工程师认证 | 领测软件测试网