3

Probably this question might have been asked. I am new to the conversion of xml to java classes. I have an xml like this:

<Root>
    <Book name="harel" price="5" />
    <Book name="xml" price="9" />
</Root>

IS there a way to generate java classes dynamicaly for a structure like this ? A small correction, i don't have an xsd for the xml

Parameswar
  • 1,951
  • 9
  • 34
  • 57
  • Dynamically? What do you want to do with the classes if you don't know them at compile-time? – Puce Jun 26 '12 at 18:39

5 Answers5

6

Note: I'm the EclipseLink JAXB (MOXy) lead and a member of the JAXB (JSR-222) expert group.

IS there a way to generate java classes dynamicaly for a structure like this ?

JAXB implementations provide the ability to generate a Java model from an XML schema. The reference implementation which is included in the JDK starting in Java SE 6 is available at:

<JAVA_HOME>/bin/xjc

An example of generating an object model from an XML schema can be found here:

A small correction, i don't have an xsd for the xml

If you don't have an XML schema you could find a utility to generate an XML schema from an XML document:

Or start from code.


STARTING FROM CODE

You can also start from code and annotate your model to map to the existing XML structure.

Root

package forum11213872;

import java.util.List;
import javax.xml.bind.annotation.*;

@XmlRootElement(name="Root")
@XmlAccessorType(XmlAccessType.FIELD)
public class Root {

    @XmlElement(name="Book")
    private List<Book> books;

}

Book

package forum11213872;

import javax.xml.bind.annotation.*;

@XmlAccessorType(XmlAccessType.FIELD)
public class Book {

    @XmlAttribute
    private String name;

    @XmlAttribute
    private int price;

}

Demo

package forum11213872;

import java.io.File;
import javax.xml.bind.*;

public class Demo {

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(Root.class);

        Unmarshaller unmarshaller = jc.createUnmarshaller();
        File xml = new File("src/forum11213872/input.xml");
        Root root = (Root) unmarshaller.unmarshal(xml);

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(root, System.out);
    }

}

input.xml/Output

<Root>
    <Book name="harel" price="5" />
    <Book name="xml" price="9" />
</Root>
Community
  • 1
  • 1
bdoughan
  • 147,609
  • 23
  • 300
  • 400
  • 2
    Hi Blaise. I wanted to say thank you for your efforts. I have used JAXB before for a Jersey REST service and it is a great tool (except for a minor thing, we were using Hibernate and I had to deal with a number of infinite loops in the conversion caused by the way our objects were interlinked - our fault) – mihaisimi Jun 26 '12 at 18:53
  • 2
    @mihaisimi - I'm happy to hear I've been of help. You may be interested in the `@XmlInverseReference` annotation we have in EclipseLink JAXB (MOXy) to handle that use case: http://blog.bdoughan.com/2010/07/jpa-entities-to-xml-bidirectional.html – bdoughan Jun 26 '12 at 18:57
  • 2
    Thank you, I will take a look into your suggestion. I have used @XmlId and XmlIdRef to get rid of the problem. – mihaisimi Jun 26 '12 at 19:02
  • Thanks for your program, what i want is, i want to access the attributes of the xml through a java class. I don't know whether it is possible or not. I will explain my need in detail. I am having java object with some values. I want to compare the object with an xml file . SO, basicaly if i can convert the xml file to java object, it would be helpful – Parameswar Jun 27 '12 at 04:54
3

Yes, see the castor framework (http://www.castor.org/) or jaxb (http://www.oracle.com/technetwork/articles/javase/index-140168.html)

mihaisimi
  • 1,911
  • 13
  • 15
2

Try Java Castor. You can specify a xsd and convert it to object. There is also a plugin for Eclipse.

http://www.castor.org/

franklins
  • 3,710
  • 6
  • 41
  • 56
1

Have a look at XStream.

It converts between XML and Java and between Java and XML.

Zéychin
  • 4,135
  • 2
  • 28
  • 27
1

Use JAXB, it is included in JavaSE now and you can use XJC to generate the classes from an XSD. However if you truly mean dynamically as in the structure of the XML isn't known till runtime you will need to use something like JDOM.

LINEMAN78
  • 2,562
  • 16
  • 19