Java uses schema constraint XML file

Source: Internet
Author: User

To constrain an XML file with a schema:

Take the Computer.xml file as an example.

1. Create a computer.xml file.

<?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. Create a computer.xsd file.

<?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. Create a computer.xml file.

<?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 "
>
<!--the following two sentences need to be added during the nesting process.

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. Create a computer.xsd file.

<?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" >

<!--add a couple of sentences to the nesting process.

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 Create a memory.xsd file.

<?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>

Java uses schema constraint XML file

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.