sonum kumar
sonum kumar

Reputation: 183

XML to Object conversion process other than xstream

I was using xstream api as shown below but now please advise can I achieve the same thing of xml to java object conversion process with API other than xstream in java itself like JAXB. Please adivse if it is possible then how can I convert this other than using xstream..

Suppose we have a requirement to load configuration from xml file:

01  <config>
02      <inputFile>/Users/tomek/work/mystuff/input.csv</inputFile>
03      <truststoreFile>/Users/tomek/work/mystuff/truststore.ts</truststoreFile>
04      <keystoreFile>/Users/tomek/work/mystuff/CN-user.jks</keystoreFile>
05   
06      <!-- ssl stores passwords-->
07      <truststorePassword>password</truststorePassword>
08      <keystorePassword>password</keystorePassword>
09   
10      <!-- user credentials -->
11      <user>user</user>
12      <password>secret</password>
13  </config>

And we want to load it into Configuration object:

01  public class Configuration {
02   
03      private String inputFile;
04      private String user;
05      private String password;
06   
07      private String truststoreFile;
08      private String keystoreFile;
09      private String keystorePassword;
10      private String truststorePassword;
11   
12      // getters, setters, etc.
13  }

So basically what we have to do is:

1   FileReader fileReader = new FileReader("config.xml");  // load our xml file 
2       XStream xstream = new XStream();     // init XStream
3       // define root alias so XStream knows which element and which class are equivalent
4       xstream.alias("config", Configuration.class);   
5       Configuration loadedConfig = (Configuration) xstream.fromXML(fileReader);

Upvotes: 2

Views: 405

Answers (2)

bdoughan
bdoughan

Reputation: 148977

Here is how it can be done with JAXB (JSR-222). An implementation of JAXB Is included in Java SE 6 and above.

Java Model (Configuration)

JAXB does not required any annotations (see: http://blog.bdoughan.com/2012/07/jaxb-no-annotations-required.html), but mapping a root element with @XmlRootElement does make things easier. By default JAXB will derive the mappings from the public properties, but I have used @XmlAccessorType(XmlAccessType.FIELD) so I could exclude them so I could post a smaller working class (see: http://blog.bdoughan.com/2011/06/using-jaxbs-xmlaccessortype-to.html).

import javax.xml.bind.annotation.*;

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

      private String inputFile;
      private String user;
      private String password;

      private String truststoreFile;
      private String keystoreFile;
      private String keystorePassword;
      private String truststorePassword;

      // getters, setters, etc.
}

Demo Code (Demo)

The following demo code will covert the XML to object form and then write it back to XML.

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

public class Demo {

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

        Unmarshaller unmarshaller = jc.createUnmarshaller();
        File xml = new File("src/forum19407064/input.xml");
        Configuration configuration = (Configuration) unmarshaller.unmarshal(xml);

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

}

Additional Info

Since you are familiar with XStream here is an article I wrote that maps an object model with both JAXB and XStream to see what the differences are.

Upvotes: 2

tom
tom

Reputation: 2714

Jackson is great for this. At its most basic, you can simply do this:

XmlMapper mapper = new XmlMapper();
mapper.writeValue(myFile, myObject)

Upvotes: 0

Related Questions