0

I have xml file and I want to create set of classes based on it with JAXB annotations, which will be used to un-marshall XML files.

XML:

<widgetOutput>
<dataItems>
<dataItem><isLimit>false</isLimit></dataItem>
<dataItem><size>1000</size></dataItem>
<dataItem><key>Canada</key><value>1</value></dataItem>
<dataItem><key>US</key><value>2</value></dataItem>
<dataItem><key>Italy</key><value>3</value></dataItem>
<dataItem><key>France</key><value>4</value></dataItem>
...
<dataItem><key>some_N_key_here</key><value>some_N_value_here</value></dataItem>
<status>complete</status>
</dataItems>
</widgetOutput>

I can't modify this xml, it is being generated byt he web service. Please advise how classes along with annotations may look like. Thanks in advance.

3
  • the trick here is that nodes <dataItem> have different nature: some of them are used to represent key-value pairs and others contain 'metadata'. I have generated java files, but they are not good... DataItem class coonatins only one field: isLimit. I guess this is because xsd is not good...
    – Ihor M.
    Commented Nov 15, 2011 at 20:40
  • Alright I was able to generate java classes. DataItem class which was generated contains consolidated fields: isLimit, size, key, value. What is actually is not exactly what I was looking for. I wanted to have isLimit and size as fileds of WidgetOutput class. But seems it is not possible to achieve... it doesn't make sense to have such cumbersome objects.
    – Ihor M.
    Commented Nov 15, 2011 at 20:51
  • 1
    It's hard to tell here if you still have a question or if your question has been answered. Seems like the question regarding how to generate Java classes with JAXB annotation is answered. If you have a further question, I suggest you post that specific question as a new one.
    – John B
    Commented Nov 16, 2011 at 0:00

2 Answers 2

1

Consider creating a schema for the expected XML and using XJC to create the Java classes for you.

3
  • Is it something I can quickly do in Eclipse?
    – Ihor M.
    Commented Nov 15, 2011 at 19:35
  • Yes. Eclipse has a tool for creating a schema. XJC is a command line tool that comes with the JDK.
    – John B
    Commented Nov 15, 2011 at 19:57
  • Thx, I have generated classes. Have added some extra comments.
    – Ihor M.
    Commented Nov 15, 2011 at 20:52
0

Use tools such as the ones provided by NetBeans to create all necessary classes from a WSDL file.

Or use a Maven plugin if you're using Maven.

1
  • I do not have a wsdl file, this is the file generated by the RESTful web service
    – Ihor M.
    Commented Nov 15, 2011 at 19:33

Not the answer you're looking for? Browse other questions tagged or ask your own question.