Thursday, March 19, 2009

Create XSD file from XML file

There are many tools to generate XSD from XML file, but I am using here trang.

STEP 1: First download trang from here (choose
STEP 2: Unzip this file at any location say C:\jar\trang-20030619\trang-20030619
STEP 3: Create one xml file at any location say C:\workspaceAll\XSD\XMLTOXSD\src\StudentInfo.xml


<?xml version="1.0" encoding="UTF-8"?><StudentData xmlns=";
<Location>South Corner</Location>

STEP 4: Now use this command

C:\workspaceAll\XSD\XMLTOXSD\src>java -jar C:\jar\trang-20030619\trang-20030619\trang.jar StudentInfo.xml StudentRecord.xsd

STEP 5: Now you will get StudentRecord.xsd at C:\workspaceAll\XSD\XMLTOXSD\src location. And StudentRecord.xsd will like this

<?xml version="1.0" encoding="UTF-8"?><xs:schema xmlns:xs="" elementFormDefault="qualified" targetNamespace="" xmlns:schemas=";
<xs:element name="StudentData">
<xs:element ref="schemas:Student"/>
<xs:element ref="schemas:Hostel"/>
<xs:element name="Student">
<xs:element ref="schemas:RollNo"/>
<xs:element ref="schemas:FirstName"/>
<xs:element ref="schemas:LastName"/>
<xs:element ref="schemas:ContactNo"/>
<xs:element name="RollNo" type="xs:integer"/>
<xs:element name="FirstName" type="xs:NCName"/>
<xs:element name="LastName" type="xs:NCName"/>
<xs:element name="ContactNo" type="xs:integer"/>
<xs:element name="Hostel">
<xs:element ref="schemas:Name"/>
<xs:element ref="schemas:Location"/>
<xs:element ref="schemas:RoomNo"/>

</xs:element> <xs:element name="Name" type="xs:NCName"/>
<xs:element name="Location" type="xs:string"/>
<xs:element name="RoomNo" type="xs:integer"/>

It is very easy :)

IMP: If you want to develop any XSD file, so it would be better idea that first write XML file then using trang utility create XSD file. For example, I have below xml (persons.xml)
<?xml version="1.0" ?>
<person id="1">

<person id="2">

<person id="3">
Now use the trang command and get your XSD file. I kind suggestion, never dig your head to develop XSD file by using XSD tags. Always use trang command. :)
java -jar c:\jar\trang-20030619\trang-20030619\trang.jar persons.xml personsFormat.xsd

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="" elementFormDefault="qualified">
<xs:element name="information">
<xs:element maxOccurs="unbounded" ref="person"/>

<xs:element name="person">
<xs:element ref="name"/>
<xs:element ref="age"/>
<xs:element ref="gender"/>
<xs:attribute name="id" use="required" type="xs:integer"/>
<xs:element name="name" type="xs:NCName"/>
<xs:element name="age" type="xs:integer"/>
<xs:element name="gender" type="xs:NCName"/>


  1. First time I got this much easy way to create XSD schema from XML files.

  2. i am working on spring framework mvc project, i get difficulities while making config the XML file ,and tell me what for the xsd is used,
    i am a java enthusiast

    Reply me here->

  3. I have an xsd file. i need to read the file and store the contents in an array. I.m new to java and hence need some assistance..... please help me out


You can put your comments here (Either feedback or your Question related to blog)