An easy way to generate your .xsd file from .xml is with trang command line tool.
Below are the steps to generate .xsd file from .xml file.
E.g suppose your .xml file is as below then It will generate .xsd
addToCart.xml
<?xml version="1.0" encoding="UTF-8"?>
<addToCart>
<cartItem>
<cart_item_id><![CDATA[156]]></cart_item_id>
<cart_id><![CDATA[9]]></cart_id>
<nid><![CDATA[167]]></nid>
<qty><![CDATA[1]]></qty>
<title><![CDATA[Born To Die (Album)]]></title>
<price><![CDATA[99]]></price>
<discountedPrice><![CDATA[89.1]]></discountedPrice>
</cartItem>
</addToCart>
addToCart.xsd
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
<xs:element name="addToCart">
<xs:complexType>
<xs:sequence>
<xs:element ref="cartItem"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="cartItem">
<xs:complexType>
<xs:sequence>
<xs:element ref="cart_item_id"/>
<xs:element ref="cart_id"/>
<xs:element ref="nid"/>
<xs:element ref="qty"/>
<xs:element ref="title"/>
<xs:element ref="price"/>
<xs:element ref="discountedPrice"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="cart_item_id" type="xs:integer"/>
<xs:element name="cart_id" type="xs:integer"/>
<xs:element name="nid" type="xs:integer"/>
<xs:element name="qty" type="xs:integer"/>
<xs:element name="title" type="xs:string"/>
<xs:element name="price" type="xs:integer"/>
<xs:element name="discountedPrice" type="xs:decimal"/>
</xs:schema>
Below are the steps to generate .xsd file from .xml file.
- Download tarng.jar file and save it at your local drive.
- Generate .xsd file using below command
- java -jar <<your local machine path of trang.jar>>trang.jar addToCart.xml addToCart.xsd
E.g suppose your .xml file is as below then It will generate .xsd
addToCart.xml
<?xml version="1.0" encoding="UTF-8"?>
<addToCart>
<cartItem>
<cart_item_id><![CDATA[156]]></cart_item_id>
<cart_id><![CDATA[9]]></cart_id>
<nid><![CDATA[167]]></nid>
<qty><![CDATA[1]]></qty>
<title><![CDATA[Born To Die (Album)]]></title>
<price><![CDATA[99]]></price>
<discountedPrice><![CDATA[89.1]]></discountedPrice>
</cartItem>
</addToCart>
addToCart.xsd
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
<xs:element name="addToCart">
<xs:complexType>
<xs:sequence>
<xs:element ref="cartItem"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="cartItem">
<xs:complexType>
<xs:sequence>
<xs:element ref="cart_item_id"/>
<xs:element ref="cart_id"/>
<xs:element ref="nid"/>
<xs:element ref="qty"/>
<xs:element ref="title"/>
<xs:element ref="price"/>
<xs:element ref="discountedPrice"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="cart_item_id" type="xs:integer"/>
<xs:element name="cart_id" type="xs:integer"/>
<xs:element name="nid" type="xs:integer"/>
<xs:element name="qty" type="xs:integer"/>
<xs:element name="title" type="xs:string"/>
<xs:element name="price" type="xs:integer"/>
<xs:element name="discountedPrice" type="xs:decimal"/>
</xs:schema>
Very Good
ReplyDelete