How to generate JAXB classes from just XML

前端 未结 3 1240
执笔经年
执笔经年 2020-11-30 21:20

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 t

相关标签:
3条回答
  • 2020-11-30 21:43

    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.

    0 讨论(0)
  • 2020-11-30 21:44

    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: enter image description here

    0 讨论(0)
  • 2020-11-30 21:44

    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
    0 讨论(0)
提交回复
热议问题