+49 228 5552576-0


info@predic8.com

Create XML Schema with Java

The code in listing 1 creates a XML schema and dupms it to the console.

package sample.schema;

import com.predic8.schema.*;
import static com.predic8.schema.Schema.*;

public class GenerateSchema {

  public static void main(String[] args) {

    Schema schema = new Schema("http://predic8.com/schema/person/");
    
    schema.newElement("person", "personType");
   
    ComplexType personType = schema.newComplexType("personType");
    personType.newAttribute("id", INT);
    Sequence seq = personType.newSequence();
    seq.newElement("name", STRING);
    seq.newElement("lastname", STRING);
    seq.newElement("date-of-birth", DATE);
    seq.newElement("address").newComplexType().newSequence().newElement("country", STRING);
    
    System.out.println(schema.getAsString());
  }
}
      
Listing 1: GenerateSchema

Listing 2 shows the created schema.

<xsd:schema targetNamespace='http://predic8.com/schema/person/' attributeFormDefault='unqualified' elementFormDefault='unqualified' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:tns='http://predic8.com/schema/person/'>
  <xsd:element name='person' type='tns:personType' />
  <xsd:complexType name='personType'>
    <xsd:sequence>
      <xsd:element name='name' type='xsd:string' />
      <xsd:element name='lastname' type='xsd:string' />
      <xsd:element name='date-of-birth' type='xsd:date' />
      <xsd:element name='address'>
        <xsd:complexType>
          <xsd:sequence>
            <xsd:element name='country' type='xsd:string' />
          </xsd:sequence>
        </xsd:complexType>
      </xsd:element>
    </xsd:sequence>
    <xsd:attribute name='id' use='' type='xsd:int' />
  </xsd:complexType>
</xsd:schema>
      
Listing 2: Generated Schema

Copyright © 2008-2017 predic8 GmbH
Koblenzerstr. 65, 53173 Bonn, Tel. +49 (228) 555 25 76-0