This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
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.
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.
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?
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.
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?
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.
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.
Joined: Sep 21, 2011
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?
Joined: Jan 18, 2011
>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.
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.
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.