How do I parse a .xsd file using Java [duplicate] - java

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Java API to parse XSD schema file
I have a requirement to parse my XML schema file which will be in .xsd extention.
I am familiar with SAX parser , So I want to use this parser to parse my schema , but I read it in forums that I can't parse my XSD file usind SAX parser.
Please let me know which parser/API I need to use to parse my XSD file.

As Ernest mentioned you could use any XML API. But if you are specifically looking for XSD api then
Eclipse XSD
XSOM

The *.xsd format is perfectly well-formed XML, and so you can indeed parse an XML schema file with a SAX parser.
Now, why you'd do that is another story. Usually what you want to do is validate that an XML file adheres to the schema described in the *.xsd file; you do that by referencing the schema in the XML file and setting the appropriate properties on the parser factory: it doesn't involved parsing the schema file explicitly yourself.

Related

How to create XML file from XSD on the incoming records in java

I have a spreadsheet based on which I have to generate an xml file. That file should respect a particular XSD structure and I have to do from Java.
The XML mapping is not working in excel because of "list of lists " issue.
what would be an ideal approach in java to create, dom parser or jaxb?
Any reference is helpful

Reading an XSD file to get all the elements and corresponding attributes in Java

My requirement is to read an XSD file and to get all its elements with corresponding attributes in java. I've been trying to convert my XSD file to a sample XML file as parsing an XML file to get all it's elements and attributes is easy. But so far I've been unable to find a good enough tool in java to programmatically convert my XSD to sample XML.
Is there any free and good java code available to convert an XSD to its sample XML?
Or else what is the way I can read all the elements and attributes that a sample XML would contain directly from the XSD file?
Thanks in advance!
Processing a raw XSD document as XML is quite tricky except in very simple cases.
Alternatives are:
(a) use an API for accessing a compiled schema (for example, there is such an API in Xerces)
(b) Saxon's schema processor can output an XML representation of the compiled schema, which is much easier to process than the raw XSD documents (for example, it combines everything into one document, and presents the relationship of elements to types in a uniform way).
The xsd format is perfectly valid XML, so you can parse an XML schema file with any xml parser.
Check this related post to get some code samples and ideas:
Java API to parse XSD schema file
Not sure if this solves the problem. I have similar requirement of accessing elements and attributes of xsd. Using Eclipse, the solution to create XML out of xsd is quite easy. I have a dynamic web project created and put my xsd in webcontent. Right click on XSD and there is option called 'Generate'. It shows 2 options to generate XML or JAXB classes. On clicking Generate XML, XML file is created from XSD. I hope you were looking for this solution.
There is a tool in java to convert my XSD to sample XML. You need to add jaxb-xjc (executable jar file) in the build path of your project.Once you are done with it just put the .xsd file in src, right click on it and find 'Generate' option, you can now find XML and Jaxb classes against 'Generate' option, Select Xml and get your XML file generated from .xsd.

XML Parser for editing XML content in Java

I'm new to XML Development. I was wondering if anyone on here knows what's the best XML parser to use for adding contents into an XML document in Java?
If you're adding or modifying contents, I would look at a DOM parser. dom4j comes to mind.

How to get data from XSD file

I have an XML schema file. I want to use Java to open this file, find all elements, extract their names, and write them to a text file. I have tried various libraries, but I could not get this to work. Please suggest any libraries or other methods that will work.
XSD files are XML files so you can use any suitable XML processing library to parse it.
I don't think you'll find a library that exists for your purpose, you'll have to write one yourself. The XSD is simply an XML file that conform to the schema:
Traverse the file as normal using DOM or SAX or whatever your preference is.
I assume you are looking for the information conveyed by the XML Schema. I would recommend you the XSOM library, it takes away dealing with the XML itself; nonetheless, a basic understanding of what can be described by an XSD is most likely required.
Follow the user guide, there are some examples showing how to move towards what you need.

xml and java conversion

Can any one help me out with a java porogram to convert xsd file to xml file.
Thanks in advance.
A XSD file is already an XML file.
XSD is an XML only, XSD specifies the design of XML .
It sounds to me like you want to 'generate' an instance of the XML document, based on the XML Schema.
JAXB (Java Architecture for XML Binding) may be a 'reasonable' way of doing this. By generating a Java Binding from an XSD (input file) it is possible to generate an XML representation of any XML element's defined in the schema (e.g. using the JAXB Marshaller).
Another similar thread may be found on StackOverflow itself:
XML instance generation from XML schema (xsd)
Hope this helps!
BR/Brian.
XSD is a description of a type of XML document.
an XML Schema (xsd) is a XML file that follows the W3C specification of XML Schemas.

Categories

Resources