• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Convert XSD to XML

 
Greenhorn
Posts: 24
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Saloon Keeper
Posts: 7582
176
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 24
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.

 
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 24
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 24
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Bartender
Posts: 1051
5
Hibernate Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 24
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Saloon Keeper
Posts: 7582
176
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is the "depth" of an XML file?
 
Paul Clapham
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 2187
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 734
7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Saloon Keeper
Posts: 7582
176
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 734
7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
>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
Posts: 24
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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..:)
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hello Sunni,
could you provide more details how did you solve this problem?
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I can explain the reason I need to accomplish this. I am a developer for a company that supplies tools for higher education for managing and transferring large amounts of data. Sometimes the transfers consist of uploading a doc, parsing the data, and updating it the correct student/staff/etc. Well we now support the ability to transfer data from xml files. In order to do that they need to use our tool to build the definition of the xml file we will be reading. We let them pick a sample xml file and choose which nodes/attributes to include in the definition. Well now they want the ability to build those definitions from xsd files. In order to do that we need to be able to read xsd files so we can display the available nodes/attributes in the tree panel as we do with sample xml files. Well I figured instead of reinventing what we already wrote..lets convert the xsd to a sample xml with default values even if its just "" for every single value.

Do you understand the reason? Obviously I could explain it with better detail but its also Proprietary so attempted to do it without going to far in. If anyone knows of a way to do this please let me know.
 
g tsuji
Ranch Hand
Posts: 734
7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have an opportunity to deal with a thread on this problem again. Check this if this would help you somehow.
https://coderanch.com/t/597870/XML/Generating-XML-instance-XSD
It contains a working demo through the links in it.
 
Nolan Brassard
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was able to find a solution else where and implemented it with ease into my product. Much more bearable then any of the other suggestions I've found.

Here is a link to the project: http://code.google.com/p/jlibs/wiki/XSInstance

Here is my solution after referring to that link:



 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I know this is an old thread, but hopefully it helps anyone else stumbling onto this post. I just wanted to add another open-source solution for this (xsd->xml instance gen)
We just open-sourced xsd2xml (https://github.com/mkris/xsd2xml) a java-based API that takes an XML Schema and produces an XML instance for the specified root element in it.
hope it helps.
 
reply
    Bookmark Topic Watch Topic
  • New Topic