Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here is how it can be done with <a href="http://jcp.org/en/jsr/detail?id=222" rel="nofollow"><strong>JAXB (JSR-222)</strong></a>. An implementation of JAXB Is included in Java SE 6 and above.</p> <p><strong>Java Model (<code>Configuration</code>)</strong></p> <p>JAXB does not required any annotations (see: <a href="http://blog.bdoughan.com/2012/07/jaxb-no-annotations-required.html" rel="nofollow">http://blog.bdoughan.com/2012/07/jaxb-no-annotations-required.html</a>), but mapping a root element with <code>@XmlRootElement</code> does make things easier. By default JAXB will derive the mappings from the public properties, but I have used <code>@XmlAccessorType(XmlAccessType.FIELD)</code> so I could exclude them so I could post a smaller working class (see: <a href="http://blog.bdoughan.com/2011/06/using-jaxbs-xmlaccessortype-to.html" rel="nofollow">http://blog.bdoughan.com/2011/06/using-jaxbs-xmlaccessortype-to.html</a>).</p> <pre><code>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. } </code></pre> <p><strong>Demo Code (<code>Demo</code>)</strong></p> <p>The following demo code will covert the XML to object form and then write it back to XML.</p> <pre><code>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); } } </code></pre> <p><strong>Additional Info</strong></p> <p>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.</p> <ul> <li><a href="http://blog.bdoughan.com/2010/10/how-does-jaxb-compare-to-xstream.html" rel="nofollow">http://blog.bdoughan.com/2010/10/how-does-jaxb-compare-to-xstream.html</a></li> </ul>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload