Java使用Schema约束xml文件

使用Schema约束xml文件:

以computer.xml文件为例。

1.创建一个computer.xml文件。

<?xml version="1.0" encoding="UTF-8"?>
<computers
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns="http://www.example.org/computer"
 xsi:schemaLocation="http://www.example.org/computer computer.xsd"
>

<computer>
  <memories>
   <memory>
    <size>1G</size>
   </memory>
   <memory>
    <size>2G</size>   
   </memory>
  </memories>
 </computer>

</computers>

1.2.创建一个computer.xsd文件。

<?xml version="1.0" encoding="UTF-8"?>

<schema xmlns="http://www.w3.org/2001/XMLSchema"

targetNamespace="http://www.example.org/computer"

xmlns:tns="http://www.example.org/computer"

xmlns:mem="http://www.example.org/memory" elementFormDefault="qualified"

>

 <element name="computers">   

  <complexType>    

    <sequence>     

      <element name="computer" type="tns:computerType">

    </sequence>

  </complexType>

</element>

  <complexType name="computerType">

    <all>

      <element name="memories">    

       <complexType>     

         <sequence>       

          <element name="memory" minOccurs="1" maxOccurs="2"></element>

        </sequence>      

      </complexType> 

      </element>

    </all>  

  </complexType>

</schema>

 

 

2.创建一个computer.xml文件。

<?xml version="1.0" encoding="UTF-8"?>
<computers
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns="http://www.example.org/computer"
 xmlns:mem="http://www.example.org/memory"
 xsi:schemaLocation="http://www.example.org/computer computer.xsd
      http://www.example.org/memory memory.xsd"
>
<!-- 在嵌套过程中需要加入以下两句。

xmlns:mem="http://www.example.org/memory"
 xsi:schemaLocation="http://www.example.org/memory memory.xsd"
-->
 <computer>
  <memories>
   <memory>
    <mem:size>1G</mem:size>
   </memory>
   <memory>
    <mem:size>2G</mem:size>   
   </memory>
  </memories>
 </computer>
</computers>

2.2.创建一个computer.xsd文件。

<?xml version="1.0" encoding="UTF-8"?> <schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.example.org/computer"

xmlns:tns="http://www.example.org/computer"

xmlns:mem="http://www.example.org/memory"

elementFormDefault="qualified">

<!--在嵌套过程中要加入一下两句。

xmlns:mem="http://www.example.org/memory"

<import namespace="http://www.example.org/memory" schemaLocation="memory.xsd"></import> 

-->

<import namespace="http://www.example.org/memory" schemaLocation="memory.xsd"></import>  

<element name="computers">   

<complexType>    

<sequence>     

<element name="computer" type="tns:computerType"></element>    

</sequence>   

</complexType>  

</element>    

<complexType name="computerType">   

<all>    

<element name="memories">     

<complexType>      

<sequence>       

<element name="memory" type="mem:memoryType" minOccurs="1" maxOccurs="2">       

</element>      

</sequence>     

</complexType>    

</element>   

</all>  

</complexType>

</schema>

2.3创建一个memory.xsd文件。

<?xml version="1.0" encoding="UTF-8"?>

<schema xmlns="http://www.w3.org/2001/XMLSchema"

targetNamespace="http://www.example.org/memory"

xmlns:tns="http://www.example.org/memory"

elementFormDefault="qualified">

 <complexType name="memoryType">   

<all>    

<element name="size" type="string" ></element>   

</all>  

</complexType>

</schema>

posted @ 2015-09-09 09:35  JJ急吧  阅读(806)  评论(0编辑  收藏  举报