Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java/XSD parsing

Tags:

java

dom

xml

xsd

jaxp

I doubt if there is something like this but I thought to ask though:
Does anyone know if there is a library in Java that reads an xsd file and "creates" the defined elements e.g. in a String format to use in the code?
E.g. read in the following schema:

<?xml version="1.0" encoding="utf-8"?>
        <xs:schema elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
          <xs:element name="Address">
            <xs:complexType>
              <xs:sequence>
                  <xs:element name="Street" type="xs:string" />
                  <xs:element name="Town" type="xs:string" />
                  <xs:element name="Country" type="xs:string" minOccurs="0" />
                </xs:element>
              </xs:sequence>
            </xs:complexType>
          </xs:element>
        </xs:schema>

And have a String in the following format:

<Address>  
  <Street></Street>
  <Town></Town>
  <Country></Country>
</Address>

Automatic tools do something similar, i.e. parse a WSDL and from the types section create for example JAXB classes that can be instances of the elements defined in schema.
Is there any library to do this?

UPDATE:
For example in Eclipse when creating an xml descriptor for a web application it presents a tree table with all the required elements for the users to fill in according to schema. How do they do it? I imagine they parse the xsds included in the jars
Any input is very welcome.
Thank you!

like image 235
Cratylus Avatar asked Dec 19 '10 19:12

Cratylus


People also ask

How XSD is used in Java?

xsd is the XML schema you will use as input to the JAXB binding compiler, and from which schema-derived JAXB Java classes will be generated. For the Customize Inline and Datatype Converter examples, this file contains inline binding customizations.

What is XSD parser?

XsdParser is a library that parses a XML Definition file (. xsd) into a list of java objects. Each different XSD tag has a corresponding Java class and the attributes of a given XSD type are represented as fields of that class.

How do I read an XSD file?

XSD files are stored in XML file format and can be opened or edited in any text editor such as Microsoft Notepad, Notepad++, or Microsoft XML Notepad.

Can we generate XML from XSD?

To create an XML file from an XSD file: Right-click the XML Documents folder in your data development project, and select New > XML. The New XML File wizard opens.


2 Answers

If its a WSDL file with which you want to generate Java classes, then Axis WSDL2Java (based on JAXB) can be used to get classes based on the schema defined in the WSDL.

JAXB also offers binding framework which you might want to look up.

http://publib.boulder.ibm.com/infocenter/wasinfo/v7r0/index.jsp?topic=/com.ibm.websphere.express.doc/info/exp/ae/twbs_jaxbschema2java.html

Above link should be useful.

like image 192
Fazal Avatar answered Sep 20 '22 23:09

Fazal


oXygen has an XML instance generator that can generate a set of XML document samples based on a given XML Schema.

You can also invoke it from the commandline.

like image 31
Mads Hansen Avatar answered Sep 21 '22 23:09

Mads Hansen