Create XML file according to XSD schema - java

I know that there is a lot of such a topics online but the problem is, that none of solutions work for me.
Language I'm using: Java
IDE: Intellij
Just to make clear, I'm using community edition, maybe that's why none of plugins, like JAXB doesn't work.
From the other file I extracted data (values) and I need to create XML file with this data. Also there is XSD schema here: http://www.bpsim.org/schemas/1.0/
I'm thinking maybe there is any third-party solutions what I can use?
Cause I really don't want to code all the XML file by hands cause it's thousands of values and code.
Anyone knows a good solution?

On Linux you can use xsd2inst from xmlbeans, xmlbeans-tools packages to generate and instance out of the xsd
XMLBEANS_LIB='/usr/share/java/xmlbeans/' xsd2inst test.xsd -name shiporder> test.xml
xsd2inst -h
Generates a document based on the given Schema file
having the given element as root.
The tool makes reasonable attempts to create a valid document,
but this is not always possible since, for example,
there are schemas for which no valid instance document
can be produced.
Usage: xsd2inst [flags] schema.xsd -name element_name
Flags:
-name the name of the root element
-dl enable network downloads for imports and includes
-nopvr disable particle valid (restriction) rule
-noupa disable unique particle attribution rule

Related

What is the best way to validate multiple XML files against a XSD?

I am working on a project that requires the validation of many XML files against their XSD, the trouble I am having is that many of the XSD files depend on others XSDs, making the usual validation kind of troublesome, is there an elegant way to resolve this issue?
I would prefer if possible to work with those files in memory, the files are not in a concise directory structure that conforms with their importation paths.
Just to note I am working with the Java language.
Assuming here that you work with JAXP, so that you can setSchema() on either SAXParserFactory or `DocumentBuilderFactory.
One solution I was part of, was to read all XSD sources into an aggregated Schema object using SchemaFactory.newSchema(Source[] schemas). This aggregated Schema was then able to validate any XML document that referenced any "top" schema; all imported schemas had to be part of the aggregated schema. As I remember it, it was necessary to order the Source array by dependency, so that if Schema A imported Schema B, Schema B had to occur befor Schema A in the array.
Also, as I recall, <include> didn't work very well with this mechanism.
Another solution would be to set an LSResourceResolver on the ShemaFactory. You would have to implement your own LSResourceresolver that serves byte- or character streams based on the input to the resolver. I haven't personally used or researched this solution.
The first solution has of course the benefit that schema parsing and processing can be done once and reused for all validations that follows; something that will probably be difficult to achieve with the second option.
Another thing to keep in mind (depending on your context): It is a good design choice to control the whole "resolving" process (i.e. control how the parsers get access to external resources), from a performance as well as a security perspective.

How to read .EAP file using java

Within an Enterprise Architect file I have the definition of an XML (The definition specify which attributes are mandatory or not) , My goal is to read this definition, and afterwards validate the actual XMl file.
Is there a way to read an .Eap file using JAVA with Eclipse ?
PS: The definition might changes , this why I need to do it programmatically.
Any help would be appreciated.
No. Or: not directly. EAP files are actually Mickeysoft Access databases with just another suffix. To read them you need to use the EA API. Or use an ODBC driver for Access.
I manage a way around it.With the program Entreprise Architect,I exported the model into XML format using the highlighted option below :
Once the XML is generated,I am able to read the definitions easily with a couple of Xpath queries.
Before that I was using the viewer version of the program so I didn't have the option to export the model into an XML file. Once I downloaded the trial version , I got this option.
For those who still want to read the actual .EAP, file you can refer to the answer of Thomas Kilian.

extract an XML schema (or equivalent) from a large set of xml files

I have 46000 xml files that all share a common structure but there are variations and the number of files makes it impossible to figure out a common xml schema for all of them. Is there a tool that may help me to extract a schema from all these files or at least something that may give me a close enough idea of what is mandatory and what is optional?
Preferably, of course, a schema according to some standard or a DTD.
And, as I work entirely in Linux, a Linux tool or a program that works in Linux is OK. I am quite fluent in C, Java, Javascript, Groovy, Python (2.7 and 3) and a number of other languages.
Looking deeper into the problem and after scanning other posts I found out that inst2xsd is the tool for this kind of tasks.

creating a .xml file from .xsd with java

I'm a quite new to java world and I have a requirement of generating an .xml file from an .xsd file
I did some research and found that 'jaxb' could do it. And I found some example too, but the problem is, almost all the examples uses 'xjc' tool to do this. But I want a way to do this through my java code.
Os this possible?
if yes, I'm thinking something like this, from my java code
load the .xsd file
generate the .xml
save the .xml file
Can someone direct me to a good resource and or tell me if my thinking is wrong
I've had good experiences using XMLBeans, however I've always had the XSD available at compile time. It integrates nicely with Maven (plus potentially other build systems). The compilation produces a series of Java classes that can be used to construct an XML document that conforms to the XSD or process an XML file you've received.
You can potentially do some runtime processing of an XSD using the org.apache.xmlbeans.XmlBeans.compileXsd class, but I've never experimented with it. Just seen a reference from an FAQ.
I think the main problem is that to do it in a clean way you should have classes reflecting your xsd. Xsd defines a data model, so the important part is to recreate it with classes. If you want to do it dynamically it could be rather difficult. If you want to do it at compile time- jaxb is the way to go. There is very interesting article talking about problems related with parsing xml (it goes from a different perspective than you describe), but I think there is a wealth of knowledge to be learned from here:
http://elegantcode.com/2010/08/07/dont-parse-that-xml/

How to generate JAXB classes from just XML

I need to generate classes from xml that doesn't provide a schema. I understand this is near useless, but the fact is we have xml, it's structured, and we should be able to create a model from the xml. In the past I've done it by hand, but the current xml documents I am working with are quite large and my time would probably be better spent building something that does what I need. But, I am guessing it has already been done, and I just can't find it.
Any pointers?
There are many tools available (a quick google search should fetch you some) that can generate XSD from XML assuming string type for almost everything. You should be able to use that XSD to run JAXB to get classes.
Here's an online tool that lets you do that.
And here is a screen cap:
From your xml file, you can create a XML Schema Definition (XSD) file. Once you have the XSD, you'll be able to generate the code, need it be for java, C#, C++, or all of the above.
If you have Visual Studio, you can use xsd.exe to generate the XSD file.
References:
www.xmlforasp.net/CodeBank/System_Xml_Schema/BuildSchema/BuildXMLSchema.aspx
msdn.microsoft.com/en-us/library/x6c1kb0s(v=vs.71).aspx
Command Syntax: xsd file.xml [/outputdir:directory]
so "C:\Program Files\Microsoft SDKs\Windows\v6.0A\Bin\x64\xsd.exe"
example.xml yields a example.xsd file in the same directory as the
xml file.
XSD to Java:
Reference:
http://www.javaworld.com/javaworld/jw-06-2006/jw-0626-jaxb.html
Command Syntax: xjc your.xsd -p com.example.package -d
src/generatedOutputDirectory
XSD to C++:
References:
stackoverflow.com/questions/445905/xml-schema-to-c-classes
www.codesynthesis.com/products/xsd/
XSD to C#:
Reference:
quickstart.developerfusion.co.uk/quickstart/howto/doc/xmlserialization/XSDToCls.aspx
Command Syntax: "C:\Program Files\Microsoft
SDKs\Windows\v6.0A\Bin\x64\xsd.exe" -c -l:c# -n:SomeNameSpace
example.xsd
If the XML was created by JAXB, it can easily be converted back into objects. There's a tutorial over at oracle which illustrates one way to do this. Spring framework offers similiar features using JAXB which are very conveniant.

Categories