How to generate XML from XML schema in java and feed data in it?
Asked Answered
H

2

7

I am developing part of a web app which takes an XML schema as input to generate an XML file. There is also data to be put into the XML tags in an ordered way.

For example, If I have an ArrayList of 100 numbers, say, 1 to 100 and the .xsd looks like the following example:

xs:element name="elt1"

xs:complexType

xs:sequence

  xs:element name="elt1-1"

  xs:element name="elt1-2"

  xs:element name="elt1-3"

  xs:element name="elt1-4"

  xs:element name="elt1-5"

xs:sequence

xs:complexType

xs:element name="elt1" 

......other elements

How can I generate an XML file like the following:

< elt1>

   < elt1-1>1< elt1-1>

   < elt1-2>2< elt1-2>

   < elt1-2>3< elt1-3>

   < elt1-4>4< elt1-4>

   < elt1-5>3< elt1-5>

< elt1>

So that data in each tag is the corresponding number in the arraylist, in the same order of the data in the arraylist?

I would really appreciate any suggestion or exmaple. Thanks in advance!

Haeres answered 22/6, 2012 at 13:0 Comment(0)
M
5

You can use the xsd2inst tool in XMLBeans to generate an xml document from a schema. If you're curious how XMLBeans does this, you can see how the xsd2inst tool is implemented:

http://svn.apache.org/viewvc/xmlbeans/trunk/src/tools/org/apache/xmlbeans/impl/xsd2inst/

Metzgar answered 25/6, 2012 at 17:48 Comment(1)
For anyone who needs to look for it, it's here in the Apache archives (found it with google): archive.apache.org/dist/xmlbeans/binaries/xmlbeans-2.6.0.zipBendick
S
0

Probably a duplicate of:

how-to-generate-sample-xml-documents-from-their-dtd-or-xsd

and/or

XML instance generation from XML schema (xsd)

Also, I'd recommend that you explain if you really want to use JAXB (since you tagged it), and explicitly state whether you want this behavior to be automated in your app, or whether it can be accommodated with manual steps / using external tools (e.g. xml editors).

Shorten answered 22/6, 2012 at 17:39 Comment(3)
Thank you Patrice. I have checked those posts but didn't find what I want. I want the generation process to be automated in the webapp, without using external tools like oxygen.Haeres
@LT_Chen: Did you want JAXB specifically ? (you tagged your question with it)Shorten
No it doesn't have to be JAXB.Haeres

© 2022 - 2024 — McMap. All rights reserved.