2
<complexType name="spThread">
        <sequence>
            <element name="SPThreadID" type="int" />
            <element name="durtime" minOccurs="0" default="0">
                <simpleType>
                    <restriction base="int">
                        <minInclusive value="0" />
                    </restriction>
                </simpleType>
            </element>
            <element name="minexecutions" minOccurs="0" default="0">
                <simpleType>
                    <restriction base="int">
                        <minInclusive value="0" />
                    </restriction>
                </simpleType>
            </element>
            <element name="numThreads" type="int" />
            <element name="procedures" type="spm:procedure" minOccurs="1"
                maxOccurs="unbounded" />
        </sequence>
    </complexType>

i want to generate this type of .xsd file using java code..? How can i do that.?

Specially how to generate Simple type elements and put restrictions on it ?

2
  • What is the source of the schema? Are you writing a GUI tool that is used to create Schemas? Or are only some parts of the above XML dynamic? Commented Jul 26, 2012 at 10:41
  • I do have Jaxb annotated java classes to generate schema Commented Jul 26, 2012 at 10:42

4 Answers 4

1

Instead of creating your own simple type to represent integers starting with 0, you could leverage the existing xs:nonNegativeInteger type. I'll demonstrate with an example.

SpThread

You can use the @XmlSchemaType annotation to specify what type should be generated in the XML schema for a field/property.

package forum11667335;

import javax.xml.bind.annotation.XmlSchemaType;

public class SpThread {

    private int durTime;

    @XmlSchemaType(name="nonNegativeInteger")
    public int getDurTime() {
        return durTime;
    }

    public void setDurTime(int durTime) {
        this.durTime = durTime;
    }

}

Demo

You can use the generateSchema method on JAXBContext to generate an XML schema:

package forum11667335;

import java.io.IOException;
import javax.xml.bind.*;
import javax.xml.transform.Result;
import javax.xml.transform.stream.StreamResult;

public class Demo {

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(SpThread.class);

        jc.generateSchema(new SchemaOutputResolver() {

            @Override
            public Result createOutput(String namespaceUri, String suggestedFileName) throws IOException {
                StreamResult result = new StreamResult(System.out);
                result.setSystemId(suggestedFileName);
                return result;
            }

        });
    }

}

Output

Below is the XML schema that was generated.

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xs:schema version="1.0" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:complexType name="spThread">
    <xs:sequence>
      <xs:element name="durTime" type="xs:nonNegativeInteger"/>
    </xs:sequence>
  </xs:complexType>
</xs:schema>
Sign up to request clarification or add additional context in comments.

Comments

0

You can use any XML-handling API to achieve this. JDOM is one of them. If you'd like an API specific to building XML Schemas which you then serialize into XML, you might want to check out Eclipse MDT API.

2 Comments

Hi Marko i am doing reading and writing of xml using jaxb , so is there any aanotaton or any other way to generate above type of schema ?
The schemagen tool seems to be what you are looking for.
0

You can use Java2Schema tool for generating schema from java classes, and also you can try JaxB 2.0

Comments

0

I recommend you JAXB to any XML jobs you do. But normally XSD files are generated manually and then XML files are generated programatically using the XSD files. What are you trying to develop?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.