XML Data modeling tools

ぃ、小莉子 提交于 2020-01-25 10:52:10

问题


I want to create model classes from XML that I have written for my Java application. What are some good tools to convert auto generated classes from the XML I am writing ? A sample from my XML file.

  <list name="NameValuePairListDefinition">
    <member target="NameValuePair" />
  </list>

  <structure name="PositiveIntegerList">
    <member name="list" target="PositiveIntegerListDefinition" />
  </structure>

回答1:


If you're working with Java and want to generate classes I would highly suggest going with XMLbeans. It comes with some nice utilities, including:

  • inst2xsd : given the xml , it generates xsd

    EXAMPLE

    inst2xsd -enumerations never message.xml

    (will generate xsd file and disable any enumerations of identical type)

  • scomp : compiles the schema into a jar file, which means it generates a pretty nice reusable library for you which contains a class (or multiple classes) based on the namespace of the schema (or starting with noNamespace.something) if you didn't define any in your XML

    EXAMPLE

    scomp -jar yourschema mySchema.xsd

    (which creates xmltypes.jar - which is the jar you want, just rename it to something clever and use it for two-way parsing for getting & setting, reading & writing values)

  • lastly, just launch your java IDE and add all the xmlbeans JARS and JAR for your schema created by scomp

You can then load,create,store,manipulate, with your xmldata as it would just be java objects in your projects ;-)

Based on the Element of your small XML snippet, you'd be accessing with code that looks roughly as follows:

 MyModel myModel = null;
 try {
    //full path to XML file
    File modelXML = new File("Model.xml");

    // Bind the incoming XML to an XMLBeans type.
    myModel = MyModel.Factory.parse(modelXML);
 } 
 catch (XmlException e) { e.printStackTrace(); } 
 catch (IOException e) { e.printStackTrace(); }
 catch (FileNotFoundException e) { e.printStackTrace();  }

 // Using the <structure> element.
 Structure structure = myModel.getStructure();
 Member member = structure.getMember();
 String target = member.getTarget(); // "PositiveIntegerListDefinition" 

NOTE: Something I'd also suggest is using Element names that are not reserved Java keywords such as list, I'm not exactly sure how XMLBeans will handle it but I'm assuming it would either rename it slightly rather than overload.

Here's a full tutorial which could come in handy: http://xmlbeans.apache.org/documentation/tutorial_getstarted.html

I made the assumption you are using simple direct XML calling or REST-based Web Services, but if you decided to use something more verbose like SOAP there are also Apache CXF and Apache Axis2 which can easily generate Java classes from SOAP XML messages or WSDLs.



来源:https://stackoverflow.com/questions/13150166/xml-data-modeling-tools

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!