IntelliJ Java IDE
The moose likes XML and Related Technologies and the fly likes Convert XSD to XML Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Reply Bookmark "Convert XSD to XML" Watch "Convert XSD to XML" New topic
Author

Convert XSD to XML

Sunni srivastav
Greenhorn

Joined: Feb 15, 2011
Posts: 19

I want to write a java program in which I will read a XSD file and then using this I want to generate its corresponding XML file.

Please tell me how should I do it.
Tim Moores
Rancher

Joined: Sep 21, 2011
Posts: 2329
XSD is XML. Or are you asking about how to create an XML document that conforms to that XSD? That's not possible, as there will generally be an infinite number of XML documents that conform to any given schema.
Sunni srivastav
Greenhorn

Joined: Feb 15, 2011
Posts: 19

Tim Moores wrote:XSD is XML. Or are you asking about how to create an XML document that conforms to that XSD? That's not possible, as there will generally be an infinite number of XML documents that conform to any given schema.


Sorry i guess i did not framed my question properly..

Suppose if we have an XSD(taken from w3schools.com),I named it as MyXSD.xsd



Now in eclipse we can directly convert this XSD to generate its corresponding sample xml by right clicking it->Generate->select XML file.

I did the same for above XSD and got a new xml file say "file.xml" which is as below


this is a manual process.What I want is ;to generate an xml file from its XSD using my java program.is it possible..

Note: I don't want to use any tool..I want to do it using java program.

Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 13842

I have seen this question about a hundred times on various forums. Every time I see it I ask the question "Why would you want to do that?". So far I haven't received any good answers. So let me ask: Why would you want to do that?
Sunni srivastav
Greenhorn

Joined: Feb 15, 2011
Posts: 19

Paul Clapham wrote:I have seen this question about a hundred times on various forums. Every time I see it I ask the question "Why would you want to do that?". So far I haven't received any good answers. So let me ask: Why would you want to do that?

I don't think that I posted this question to answer to anyone that why would I need to do it.If you know the answer please post it.A solution or guidance will be highly appreciated.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 13842

Or to look at it the other way around: if there's no reason to do it, then there's no reason to help you to do it.

(And my record is still perfect: nobody has yet stated a reason for doing this.)
Sunni srivastav
Greenhorn

Joined: Feb 15, 2011
Posts: 19

Paul Clapham wrote:Or to look at it the other way around: if there's no reason to do it, then there's no reason to help you to do it.

(And my record is still perfect: nobody has yet stated a reason for doing this.)

Thanks for the assistance.
Anyone else who knows how to do it.???Thanks in advance
James Boswell
Ranch Hand

Joined: Nov 09, 2011
Posts: 344
abhinav

You would need to parse the XSD file to pick out, at a minimum, the mandatory elements required in the XML file.

The JAXP tutorial may help you here: http://docs.oracle.com/javase/tutorial/jaxp/index.html

I do agree with Paul though. I see no reason why you would want to do this.
Sunni srivastav
Greenhorn

Joined: Feb 15, 2011
Posts: 19

James Boswell wrote:abhinav

You would need to parse the XSD file to pick out, at a minimum, the mandatory elements required in the XML file.

The JAXP tutorial may help you here: http://docs.oracle.com/javase/tutorial/jaxp/index.html

I do agree with Paul though. I see no reason why you would want to do this.

Thanks for link.

I want to do it cause I am working on something in which I have to know the depth of XML files.But I have only XSD files available and the depth of an XSD and XML is not same.

In case you all know any other way to determine the depth of XMl files conforming to a particular XSD then please tell me that also.But remember I have only XSDs available for this problem...
Tim Moores
Rancher

Joined: Sep 21, 2011
Posts: 2329
What is the "depth" of an XML file?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 13842

Let's start by guessing that the "depth" of an XML file is, when you consider it as a tree structure, the maximum value of the depth of all nodes. Where "depth" is the distance from a node to the root. (I don't see why you would care about that value, either, but whatever.)

So the schema might tell you that a "Thing" element could optionally contain a "Thing" element. In this case there could be any number of nested "Thing" elements, and you would have to scan the XML document and consider each node. But you can't tell anything about that from the schema, you would have to look at each individual document.

This whole thing sounds like a badly-thought-out mess. You have to find the "depth" of an XML document -- what for? And you aren't allowed to look at the documents to find their "depth" -- whose idea was that?
Jimmy Clark
Ranch Hand

Joined: Apr 16, 2008
Posts: 2159
It seems like (with some individuals) there is confusion concerning the purpose and usage of XML-related tools. In past experience, individuals mistakenly think that XML instance files can be programmatically created using the XML Schema which defines the XML-based language. For any language with a mild range of complexity, this is not possible.

The fact that some programming APIs use schemas for creating classes and objects derived from information in the schema most likely contributes to confusion and misunderstanding. XML Schemas and XML Document Type Definitions are for creating a XML-based markup language and for "validation", i.e. parsing, of documents. They are not used for "creating" the documents. In other words, creating an XML-based markup language (with a XML Schema or DTD) and creating related documents/instances (with programming API) are not the same.

Creation of XML-based documents should be based on specific written requirements, not generalized structures specified in schema or DTD.

Aside, there are a bunch of areas to get confused (unfortunately, it happens often.) Validation using XML DTD was good enough in my opinion. It helped separate document validation and processing. XML Schema muddied the waters quite a bit and helps foster areas of confusion and misunderstanding.
g tsuji
Ranch Hand

Joined: Jan 18, 2011
Posts: 225
I am actually quite disappointed on how this thread turns into.

The only problem of the original question asked is that it is asking too much and showing too little his own competence so that people can at least anchor a starting point to help. It is too involved a question. It is not simple, it is advanced that is why so many utilities have tools with command line for that purpose. If it is done is all generality correctly, you've to understand the schema recommendation inside-out. That alone is not declarative, by verbiage. The question in itself is 100% meaningful. Existence is not something to be taken upon lightly. But, I am not saying everybody should take it seriously - each for his own inclination, though it won't helo you much making bread and butter at the end.

Apart from the original question, I just want to record that I have strong disagreement with some opinions expressed so as to let other readers (present or future) know those opinions are in fact quite awkward, and/or in fact quite misleading in the least.
Tim Moores
Rancher

Joined: Sep 21, 2011
Posts: 2329
g tsuji wrote:Apart from the original question, I just want to record that I have strong disagreement with some opinions expressed so as to let other readers (present or future) know those opinions are in fact quite awkward, and/or in fact quite misleading in the least.

Why don't you point out what, specifically, you take issue with?
g tsuji
Ranch Hand

Joined: Jan 18, 2011
Posts: 225
>Why don't you point out what, specifically, you take issue with?
That's what I did on the issue on-topic. For those not really on-topic and from respectable members, I tell you, it is very distressing - it is no fun. But I draw no pleasure to be felt like grilling others nor stuck my head out for being grilled. However, as I were already doing too often holding back, if everybody keeps silent about, it makes the forum look bad, again to my opinion only. I let those who have long history of posting here, as insiders, to manage that side.
Sunni srivastav
Greenhorn

Joined: Feb 15, 2011
Posts: 19

James Boswell wrote:abhinav

You would need to parse the XSD file to pick out, at a minimum, the mandatory elements required in the XML file.

The JAXP tutorial may help you here: http://docs.oracle.com/javase/tutorial/jaxp/index.html

I do agree with Paul though. I see no reason why you would want to do this.


Thanks james,link provided by you really helped me..:)
 
 
subject: Convert XSD to XML
 
Threads others viewed
XSD to Table Structure(Dojo Grid)
Can I check whether a particular pattern of character is there in the attribute
help - Question on XSD and XSL
validate xml using xsd
xml and JaxB without xsd
WebSphere development made easy
without the weight of IBM tools
http://www.myeclipseide.com