XmlBeans 学习

1、编写xml文件(addTaskBody.xml)
    <?xml version="1.0" encoding="UTF-8"?>
    <addTaskBody>
    <appid>001</appid>
    <taskname>机要(2009)0189 晋政办发〔2009〕173号</taskname>
    <apptaskid>351303</apptaskid>
    <tasktpyeid>1001</tasktpyeid>
    <appsenduid>xxxx</appsenduid>
    <appreceiveuid>xxxx</appreceiveuid>
    <sendtime>2009-12-06 16:33:12</sendtime>
    <endtime>2009-12-06 16:33:12</endtime>
    <handletime>2009-12-06 16:33:12</handletime>
    <url>http://xx.com.cn</url>
    <taskdesc>机要(2009)0189 晋政办发〔2009〕173号</taskdesc>
    <stateid>0</stateid>
    <priorityid>2</priorityid>
    <noticeflag>0</noticeflag>
    <noticetimes>3</noticetimes>
    <noticeinterval>1</noticeinterval>
    <mobliephone>15112345678</mobliephone>
    <email>[email protected]</email>
    <remark>xxxxxxxxx</remark>
    </addTaskBody>
 2、使用XMLSPY根据xml 生成xsd文件(addTaskBody.xsd)
  <?xml version="1.0" encoding="UTF-8"?>
  <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
  <xs:element name="addTaskBody">
  <xs:complexType>
   <xs:sequence>
    <xs:element name="appid" type="xs:string" />
    <xs:element name="taskname" type="xs:string" />
    <xs:element name="apptaskid" type="xs:string" />
    <xs:element name="tasktpyeid" type="xs:string" />
    <xs:element name="appsenduid" type="xs:string" />
    <xs:element name="appreceiveuid" type="xs:string" />
    <xs:element name="sendtime" type="xs:string" />
    <xs:element name="endtime" type="xs:string" />
    <xs:element name="handletime" type="xs:string" />
    <xs:element name="url" type="xs:string" />
    <xs:element name="taskdesc" type="xs:string" />
    <xs:element name="stateid" type="xs:string" />
    <xs:element name="priorityid" type="xs:string" />
    <xs:element name="noticeflag" type="xs:string" />
    <xs:element name="noticetimes" type="xs:string" />
    <xs:element name="noticeinterval" type="xs:string" />
    <xs:element name="mobliephone" type="xs:string" />
    <xs:element name="email" type="xs:string" />
    <xs:element name="remark" type="xs:string" />  
   </xs:sequence>
  </xs:complexType>
  </xs:element>
  </xs:schema>
 3、根据xsd文档利用scomp来生成Java Classes(jar)
  打开java命令运行对话框使用org.apache.xmlbeans.impl.tool.SchemaCompiler
  参数是:-out web/WEB-INF/lib/addTaskBodyXmlBean.jar config/addTaskBody.xsd
        4、编写测试代码
             1、读取XML
                   public void TestReadXML(){
                             AddTaskBodyDocument doc = AddTaskBodyDocument.Factory.parse(addTaskString);
                             AddTaskBody addTaskBody = doc.getAddTaskBody();
                             String appid = addTaskBody.getAppid();
                             String taskname = addTaskBody.getTaskname();
                             String apptaskid = addTaskBody.getApptaskid();
                             String tasktpyeid = addTaskBody.getTasktpyeid();
                               ...
                  }
   
  2、生成XML
              @Test
              public void TestCreateXML(){
                         AddTaskBodyDocument doc = AddTaskBodyDocument.Factory.newInstance();
                         AddTaskBody addTaskBody = doc.addNewAddTaskBody();
                         addTaskBody.setAppid("001");
                         addTaskBody.setTaskname("机要(2009)0189 晋政办发〔2009〕173号");
                          ...
                        try {
                              doc.save(new File("D:\\test.xml"));
                        } catch (IOException e) {
                              e.printStackTrace();
                        }
                }

你可能感兴趣的:(apache,xml,Web,Gmail)