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.
Related
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
I have about an xml files of around 500MB. I have an xsd. Is there a way to generate a java class that will parse these files using the xsd I have ?
Is there any tool that can do this? or will I have to code this thing by hand ?
JAXB+STAX is not an option. I need to do it by STAX alone.
I have a xml file of network traffic exported by Wireshark. I need to develop a command line tool to parse the exported xml file, and create a new XML file according to given XML Schema (.xsd) or in other words modify a xml file according to given XML Schema.
Any ideas, how could I do that? most preferably in Java.
There are a few technologies that you could use here. Often XML-to-XML translation is defined using some sort of intermediate language and then actual translation is wired together in a general purpose programming language, like Java.
Take a look at these:
XSLT, Java and XSLT
Schematron
You can also do it in code, though I would adivise against this as it gets messy. However, for completeness, have a look at:
SAX
Is there a way in Java to create an XSD schema? I am trying to create a new schema .xsd file according to the number of attributes and their type given to me as input. Is there a package that can help me to do this job?
JAXB Schemagen can generate XSD Schema from Java Classes.
try jlibs library. you can see you to create how to create xml schema using jlibs.xml.xsd.XSDocument at the end of the wiki
Use the eclipse's xsd api has the ability to create, parse and update a xsd. Refer to their documentation for how-to details.
With Apache XmlSchema you can construct XML schemata in-memory and convert them to string.
In my project I've used the JAXB compiler to generate an object model "home made API" taking the XML schema as XJC input. You can find the file here: http://www.w3.org/2001/XMLSchema.xsd
Another solution is to use the Eclipse project MDT XSD (http://www.eclipse.org/modeling/mdt/?project=xsd#xsd) but I found it not well documented and easily integratable in a Maven build.
Given a few hundred java source files how can I generate an xsd schema that describes their class hierarchy, attributes and methods?
Most of the resources I've seen describe how to convert java to xml given the schema but how is that schema created? I did find Simple but that requires me to alter the source which I am not allowed to do.
Apache axis provides the java2wsdl tool. Sure, you didn't ask for a wsdl but this tool should generate schemas (as needed) too.
So it's worth trying: implement some dummy interfaces with methods, that just use the types from your 'hundreds' of files, generate a wsdl and delete everything but the schemas.
XSD schemas in themselves are recipies allowing a program to say whether a given XML document "conforms"/"does not conform" to the schema.
For this to make sense for Java classes, you have to define a conversion scheme from Java source to XML, and that XML format can then have an XSD, which may or may not be easily derivable programatically.
What Java -> XML mapping do you use?