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>
<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.
Related
I want to get the datatypes of an XSD. It have to be static, cause the XSD could be variate.
In my case I know the element names.
Small sample of XSD, but it could be go deeper:
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Order">
<xs:complexType>
<xs:sequence>
<xs:element type="xs:float" name="OrderNo"/>
<xs:element type="xs:string" name="OrderDate"/>
<xs:element type="xs:string" name="Name"/>
<xs:element type="xs:float" name="NameNo"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
I tried with org.apache.xerces.xs but don´t know how to get the Object XSParticleDecl with Interface XSElementDeclaration from root Elem
I expect the output for each individual element name to be the type.
It's generally best to work with a schema processor that gives you some kind of access to the "schema component model" rather than working directly with source XSD documents.
The Xerces schema API is one approach but I don't have experience with that and can't help you with it.
Saxon offers two alternatives:
(a) you can generate an SCM file representing the compiled schema. This is an XML file, so it can be processed easily using XSLT or XQuery.
(b) there's a set of extension functions, starting with saxon:schema() that allow you to explore the schema directly from XPath.
I would personally work with an SCM file. The SCM file for your schema is:
<?xml version="1.0" encoding="UTF-8"?>
<scm:schema xmlns:scm="http://ns.saxonica.com/schema-component-model"
generatedAt="2019-08-22T18:14:23.59+01:00"
xsdVersion="1.1">
<scm:element id="C0"
name="Order"
type="C1"
global="true"
nillable="false"
abstract="false"/>
<scm:complexType id="C1"
base="#anyType"
derivationMethod="restriction"
abstract="false"
variety="element-only">
<scm:modelGroupParticle minOccurs="1" maxOccurs="1">
<scm:sequence>
<scm:elementParticle minOccurs="1" maxOccurs="1" ref="C2"/>
<scm:elementParticle minOccurs="1" maxOccurs="1" ref="C3"/>
<scm:elementParticle minOccurs="1" maxOccurs="1" ref="C4"/>
<scm:elementParticle minOccurs="1" maxOccurs="1" ref="C5"/>
</scm:sequence>
</scm:modelGroupParticle>
<scm:finiteStateMachine initialState="0">
<scm:state nr="0">
<scm:edge term="C2" to="1"/>
</scm:state>
<scm:state nr="1">
<scm:edge term="C3" to="2"/>
</scm:state>
<scm:state nr="2">
<scm:edge term="C4" to="3"/>
</scm:state>
<scm:state nr="3">
<scm:edge term="C5" to="4"/>
</scm:state>
<scm:state nr="4" final="true"/>
</scm:finiteStateMachine>
</scm:complexType>
<scm:element id="C2"
name="OrderNo"
type="#float"
global="false"
containingComplexType="C1"
nillable="false"
abstract="false"/>
<scm:element id="C3"
name="OrderDate"
type="#string"
global="false"
containingComplexType="C1"
nillable="false"
abstract="false"/>
<scm:element id="C4"
name="Name"
type="#string"
global="false"
containingComplexType="C1"
nillable="false"
abstract="false"/>
<scm:element id="C5"
name="NameNo"
type="#float"
global="false"
containingComplexType="C1"
nillable="false"
abstract="false"/>
</scm:schema>
<?Σ 954c7f5b?>
Built-in types are represented using, for example, type="#float", whereas user-defined types will be represented by a reference such as type="C89" where C89 is a reference to the #id attribute of an scm:simpleType or scm:complexType child of the scm:schema element.
My desired outcome is to get an XSD file generated from an XML straight in oracle
I'm using an SQL query in a PL/SQL procedure to generate an XMLType from a table.
With that XMLtype I then do a .GetClobVal() and return the clob version of that which I am currently copying into the following online tool
http://www.freeformatter.com/xsd-generator.html and then am able to generate an XSD out of this.
I know that this XSD is not perfect and will not be exactly how I want it, but it is pretty close.
I am wondering if anyone knows of a tool in Oracle that can do this, I can only find this for generating XSD from an Oracle type, but I don't use an Oracle Type in this situation, so please do not suggest using one.
Here is a sample of the code I use to create xml
With Accounts As ( Select XMLAgg(
XMLElement("AccountDetail",
XMLForest(1234 || Level As "UID",
'Test' || Level As "Name"))) As xmlData,
Count(*) as dataCount
From Dual
Connect By Level <= 2
)
Select XMLElement("GetAccountDataResponse",
XMLElement("ResponseInfo",
XMLElement("Code", 'Success'),
XMLElement("Message", 'Normal Successful Completion'),
XMLElement("DebugInfo",
XMLElement("DBVersion", 'V01.01.00'))),
Accounts.xmlData
).GetClobVal()
From Accounts;
Here is the XML sample:
<GetAccountDataResponse>
<ResponseInfo>
<Code>Success</Code>
<Message>Normal Successful Completion</Message>
<DebugInfo>
<DBVersion>V01.01.00</DBVersion>
</DebugInfo>
</ResponseInfo>
<AccountDetail>
<UID>12341</UID>
<Name>Test1</Name>
</AccountDetail>
<AccountDetail>
<UID>12342</UID>
<Name>Test2</Name>
</AccountDetail>
</GetAccountDataResponse>
When you copy the code into the xsd-generator that I mentioned earlier, you get the output of:
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="GetAccountDataResponse">
<xs:complexType>
<xs:sequence>
<xs:element name="ResponseInfo">
<xs:complexType>
<xs:sequence>
<xs:element type="xs:string" name="Code"/>
<xs:element type="xs:string" name="Message"/>
<xs:element name="DebugInfo">
<xs:complexType>
<xs:sequence>
<xs:element type="xs:string" name="DBVersion"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="AccountDetail" maxOccurs="unbounded" minOccurs="0">
<xs:complexType>
<xs:sequence>
<xs:element type="xs:short" name="UID"/>
<xs:element type="xs:string" name="Name"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
Which is pretty close and would allow me to do a replace on type="xs:short" to type="xs:int" within PL/SQL etc to get my desired outputs.
I would also be happy if there was a JAVA program that could do this, as Oracle has the support for running JAVA natively
There is no Oracle Tool that generate XSD from an XML.
You can try this Apache XMLBeans inst2xsd tool: http://xmlbeans.apache.org/docs/2.0.0/guide/tools.html#inst2xsd
I need to create an XML on JAVA but for multi fields on one element:
<cities>
<city_insert city_id="123" city_name="São Paulo" />
<city_insert city_id="456" city_name="Rio de Janeiro"/>
</cities>
As you can see on the example above, the element city_insert need to have city_id, and city_name , one element can have multiples fields.
How this can be done on Java?
I've searched for DOM and JDOM parsers but still don't know how this works.
Thank you!
Refer to this question for creating an XML using DOM parser.
Create XML file using java
In order to create an attribute (which you mentioned as fields), call setAttribute() method.
nodelist = doc.getElementsByTagName("city_insert");
for (Element element : nodelist) {
Element parent = element.getParentNode()
parent.setAttribute("city_id", "123");
parent.setAttribute("city_name", "São Paulo");
}
I allways do this things using jaxb
First, generate an xsd from your xml (there are many free online generators on the net)
For your xml, an online generated xsd look as follows:
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="cities">
<xs:complexType>
<xs:sequence>
<xs:element name="city_insert" maxOccurs="unbounded" minOccurs="0">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute type="xs:short" name="city_id" use="optional"/>
<xs:attribute type="xs:string" name="city_name" use="optional"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
Then, using jaxb (many IDE's like Eclipse have an easy way of doing it) generate jaxb classes from an xsd:
Click finish, then this is java console output:
parsing a schema...
compiling a schema...
com\erax\xml\test\xsd\Cities.java
com\erax\xml\test\xsd\ObjectFactory.java
And the generated classes:
Then just use jaxb marshalling to serialize and deserialize
I am trying to construct an XML document using org.w3c.dom.Document to create some XML for another existing tool. The problem I am having is that the tool seems to have the XML namespaces in a strange way. How can I replicate this using the Java API's?
<ns1:myroot xmlns:ns1="http://foo.com/ns1/">
<ns2:bar xmlns:ns2="http://foo.com/xml/bar/">
<ns2:bar_thing>abc</ns2:bar_thing>
</ns2:bar>
<ns3:data xmlns:ns3="http://foo.com/xml/special-ns">
<!--These are not namespaced for some reason. If I use the ns3 prefix, or
use a default xmls="...", the tool fails to load the document, saying the
elements have invalid values.
-->
<a>Element without namespace</a>
<b>
<bi>1</bi>
<bii>2</bii>
</b>
</ns3:data>
</ns1:myroot>
I can build most of the document easily with createElementNS and setAttributeNS. However I can't get the ns3:data contents to be correct.
Trying to use the non-namespace createElement still left an xmlns="http://foo.com/xml/special-ns"> on my a and b elements, as did using createElementNS with an empty namespace, and obviously a non-empty namespace puts them in a namespace.
The schema for http://foo.com/xml/special-ns has a bunch of declarations like these below, not sure what the tns thing is about, but otherwise does not seem special (although I am not 100% sure the tool actually does anything with the XSD and I don't have access to the source).
<xs:schema version="1.0" targetNamespace="http://foo.com/xml/special-ns">
<!--Bunch of xs:element such as this-->
<xs:element name="data" type="tns:data" />
<!--types declared after-->
<xs:complexType name="data">
<xs:sequence>
<xs:element name="a" type="tns:aDataObj" minOccurs="0"/>
<xs:element name="b" type="tns:bDataObj" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
<xs:simpleType name="aDataObj">
<xs:restriction base="xs:string">
<xs:pattern value="[a-zA-Z0-9 ]+" />
</xs:restriction>
</xs:simpleType>
<xs:complexType name="bDataObj">
<xs:sequence>
<xs:element name="bi" type="xs:string"/>
<xs:element name="bii" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
I want to generate the xml file that holds its schema as well as the xml data contain using java,as per my knowledge it is possible in C# .NET.Is it possible in java???
My XML file should be look like as given below.
<transaction>
<xs:schema id="transaction" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xs:element name="transaction" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="id">
<xs:complexType>
<xs:sequence>
<xs:element name="in" type="xs:string" minOccurs="0" />
<xs:element name="sn" type="xs:string" minOccurs="0" />
<xs:element name="book" type="xs:string" minOccurs="0" />
<xs:element name="author" type="xs:string" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="data">
<xs:complexType>
<xs:sequence>
<xs:element name="dateTime" type="xs:dateTime" minOccurs="0" />
<xs:element name="key" type="xs:string" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="productData">
<xs:complexType>
<xs:sequence>
<xs:element name="dateTime" type="xs:dateTime" minOccurs="0" />
<xs:element name="key" type="xs:string" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
<id>
<in>computer</in>
<sn>1234567</sn>
<book>JAVA</book>
<author>klen</author>
</id>
<data>
<dateTime>2011-06-24T17:08:36.3727674+05:30</dateTime>
<key>Err</key>
</data>
</transaction>
In my given example my xml file contain data as well as schema I need to generate this type of file from schema using java.
I can only create the xml part using jaxb and main part of my code is look like as
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,true);
jaxbMarshaller.setProperty(Marshaller.JAXB_FRAGMENT,true);
jaxbMarshaller.marshal(transaction, file);
jaxbMarshaller.marshal(transaction, System.out);
but I can not add the inline xml schema part with my xml file.
#jtahlborn ok I will try to dig it up thanks for your help.I have another question I heard about that stax is better than dom to xml write,so I want to use stax is it possible to set namespace and other thing. I have another question is it true that jaxb is only use to convert xml to xml schema(un marshaling) and xml schema to xml(marshaling) and if I need to write xml file then we need to use jaxb[DOM,STAX(stream based reading writing),SAX(stream only reading)] .
You would:
create a DOM document from your schema (e.g. parse the schema file)
create a new DOM document
add the root node to your new DOM document (e.g. "transaction")
append the schema document from step 1. as the first child of the "transaction" element
append the actual document data as subsequent children of the "transaction" element
Alternately, if you want to use JAXB to generate the "main" xml output, then you can:
populate jaxb models (created from the schema)
marshal the jaxb models to a DOM document
create a DOM document from your schema (e.g. parse the schema file)
insert the schema document from step 3. as the first child of the "transaction" element in your DOM document
(with a few jaxb config tricks, you could probably get your Transaction model to have an Element "schema" property, and then you could set that property from the parsed schema doc and marshal the whole model at one time)