I have been searching for 3 days but i can't find a solution.I want to parse the XSD file to create mySql tables in java.I don't want to validate xml files with the xsd's by the way.
Firstly, I used XSOM but I can't fixed the error NoClassDefFoundError. I think I couldn't set the libraries. Something was missing. If you can give me to whole libraries necessary, it can be fixed maybe.
Secondly, I tried to use org.eclipse.xsd libraries but I couldn't make it again. I couldn't find out how to use the classes to parse the xsd and get its attributes, elements etc. and then create the sql tables.
Finally, also I couldn't fixed the problem with the SAXParser.
-- by the way, what intend to do is that:
by using this schema I will create a DB table,
<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Table" type="TableType"/>
<xs:complexType name="TableType">
<xs:sequence>
<xs:element name="Rows" type="Rows"/>
</xs:sequence>
<xs:attribute fixed="Students" name="name" type="xs:string"/>
<xs:attribute fixed="id" name="Primarykey" type="xs:string"/>
</xs:complexType&开发者_运维知识库gt;
<xs:complexType name="Rows">
<xs:sequence>
<xs:element minOccurs="0" maxOccurs="unbounded" name="Row" type="RowType"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="RowType">
<xs:sequence>
<xs:element name="id" type="xs:integer"/>
<xs:element name="name" type="xs:string"/>
<xs:element name="surname" type="xs:string"/>
<xs:element name="department" type="xs:string"/>
<xs:element name="year" type="xs:integer"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
then,
by using this xml, I will make an insertion to the DB,
<?xml version="1.0" encoding="UTF-8"?>
<Table name="Students" Primarykey= "id">
<Rows>
<Row>
<id>100000</id>
<name>Ali</name>
<surname>Yilmaz</surname>
<department>CENG</department>
<year>1</year>
</Row>
<Row>
<id>100001</id>
<name>Deniz</name>
<surname>Bayraktar</surname>
<department>EE</department>
<year>3</year>
</Row>
</Rows>
</Table>
Waiting for your helps.
Thanks.
XSD is XML, so any XML parser will do. Use the one that's built into the JDK, or perhaps you'll find JDOM or DOM4J easier to use.
Once you have the XSD parsed, you'll have to generate SQL DDL (e.g. CREATE TABLE) statements for MySQL. It's a two-step process for you.
XSD and XML are hierarchical; SQL databases are relational. You're likely to have to do more work on the MySQL schema to make it usable (e.g. primary keys, indexes, etc.)
For parsing XSD file, i would recommend using JDOM which is very straightforward and intuitive. Here is a good read on it. Inserting data into SQL should be trivial using SQL statements.
精彩评论