XML到Java,反之亦然,使用xsd模式

时间:2015-07-28 04:42:55

标签: java xml xsd

以下是我的Schema.xsd

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xs:schema version="1.0" xmlns:ns1="http://www.example.org/address" xmlns:xs="http://www.w3.org/2001/XMLSchema">

  <xs:import namespace="http://www.example.org/address" schemaLocation="www.example.org_address.xsd"/>

  <xs:element name="dependant" type="dependant"/>
  <xs:element name="employee" type="employee"/>

  <xs:complexType name="employee">
    <xs:sequence>
      <xs:element name="dependantVO" minOccurs="0">
        <xs:complexType>
          <xs:sequence>
            <xs:element ref="dependant" minOccurs="0" maxOccurs="unbounded"/>
          </xs:sequence>
        </xs:complexType>
      </xs:element>
      <xs:element name="address" type="ns1:address" minOccurs="0"/>
      <xs:element name="dob" type="xs:dateTime" minOccurs="0"/>
      <xs:element name="joiningBonus" type="xs:decimal" minOccurs="0"/>
      <xs:element name="name" type="xs:string" minOccurs="0"/>
      <xs:element name="permanent" type="xs:boolean"/>
      <xs:element name="role" type="xs:string" minOccurs="0"/>
    </xs:sequence>
    <xs:attribute name="id" type="xs:int" use="required"/>
  </xs:complexType>

  <xs:complexType name="dependant">
    <xs:sequence>
      <xs:element name="name" type="xs:string" minOccurs="0"/>
      <xs:element name="relation" type="xs:string" minOccurs="0"/>
    </xs:sequence>
  </xs:complexType>
</xs:schema>

以下是我的地址.xd

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

  <xs:complexType name="address">
    <xs:sequence>
      <xs:element name="addressLine1" type="xs:string" minOccurs="0"/>
      <xs:element name="addressLine2" type="xs:string" minOccurs="0"/>
      <xs:element name="city" type="xs:string" minOccurs="0"/>
      <xs:element name="zip" type="xs:int"/>
    </xs:sequence>
  </xs:complexType>
</xs:schema>

现在如果我整理对象&#34;员工&#34;下面是生成的employee.xml

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<employee id="12">
    <dependantVO>
        <dependant>
            <name>Vin Diesel</name>
            <relation>Friend</relation>
        </dependant>
        <dependant>
            <name>Black Ryan</name>
            <relation>GF</relation>
        </dependant>
    </dependantVO>
    <address>
        <addressLine1>My Company</addressLine1>
        <addressLine2>bangalore east</addressLine2>
        <city>Bangalore</city>
        <zip>560549</zip>
    </address>
    <dob>2015-07-28T10:04:34.599+05:30</dob>
    <joiningBonus>10234.35467000000062398612499237060546875</joiningBonus>
    <name>Paul Walker</name>
    <permanent>true</permanent>
    <role>SE</role>
</employee>

我有一个订购xml标签的问题。例如:假设我希望在&#34;员工&#34;内部生成addressline1。标签和外部&#34;地址&#34;标签,也是内部生成的依赖名称&#34;员工&#34;标签和外面&#34; dependantVO&#34;和&#34;依赖&#34;标签。下面是架构更改后应生成的xml

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <employee id="12">
    <addressLine1>My Company</addressLine1>
    <city>Bangalore</city>
        <dependantVO>
            <dependant>
                <name>Vin Diesel</name>
                <relation>Friend</relation>
            </dependant>
            <dependant>
                <name>Black Ryan</name>
                <relation>GF</relation>
            </dependant>
        </dependantVO>
        <address>
            <addressLine2>bangalore east</addressLine2>
            <zip>560549</zip>
        </address>
        <dob>2015-07-28T10:04:34.599+05:30</dob>
        <joiningBonus>10234.35467000000062398612499237060546875</joiningBonus>
        <name>Paul Walker</name>
        <permanent>true</permanent>
        <role>SE</role>
    </employee>

请注意,addressLine1标记和城市标记位于地址标记之外,并在employee标记内创建。这是必需的,因为可以从生成的xml轻松访问某些常用信息。如果有人试过这个案子或已经做过,请帮帮我......

0 个答案:

没有答案