<p>What is the Java analogue of .NET's XML serialization?</p>

[{'answer_id': 35793, 'author': 'Theo', 'author_id': 1109, 'author_profile': 'https://Stackoverflow.com/users/1109', 'pm_score': 0, 'selected': False, 'text': '<p>If you\'re talking about automatic XML serialization of objects, check out <a href="http://www.castor.org/" rel="nofollow noreferrer">Castor</a>:</p>\n\n<blockquote>\n  <p>Castor is an Open Source data binding framework for Java[tm]. It\'s the shortest path between Java objects, XML documents and relational tables. Castor provides Java-to-XML binding, Java-to-SQL persistence, and more.</p>\n</blockquote>\n'}, {'answer_id': 35796, 'author': 'Barak Schiller', 'author_id': 1651, 'author_profile': 'https://Stackoverflow.com/users/1651', 'pm_score': 6, 'selected': False, 'text': '<p><a href="http://x-stream.github.io/" rel="noreferrer">XStream</a> is pretty good at serializing object to XML without much configuration and money! (it\'s under BSD license).</p>\n\n<p>We used it in one of our project to replace the plain old java-serialization and it worked almost out of the box.</p>\n'}, {'answer_id': 35821, 'author': 'Cheekysoft', 'author_id': 1820, 'author_profile': 'https://Stackoverflow.com/users/1820', 'pm_score': 7, 'selected': True, 'text': '<p><strong>2008 Answer</strong>\nThe "Official" Java API for this is now JAXB - Java API for XML Binding. See <a href="http://docs.oracle.com/javase/tutorial/jaxb/TOC.html" rel="noreferrer">Tutorial by Oracle</a>. The reference implementation lives at <a href="http://jaxb.java.net/" rel="noreferrer">http://jaxb.java.net/</a></p>\n\n<p><strong>2018 Update</strong> \nNote that <a href="http://openjdk.java.net/jeps/320" rel="noreferrer">the Java EE and CORBA Modules are deprecated in SE in JDK9 and to be removed from SE in JDK11</a>. Therefore, to use JAXB it will either need to be in your existing enterprise class environment bundled by your e.g. app server, or you will need to bring it in manually.</p>\n'}, {'answer_id': 35898, 'author': 'John Meagher', 'author_id': 3535, 'author_profile': 'https://Stackoverflow.com/users/3535', 'pm_score': 2, 'selected': False, 'text': '<p><a href="http://xmlbeans.apache.org/" rel="nofollow noreferrer">XMLBeans</a> works great if you have a schema for your XML.  It creates Java objects for the schema and creates easy to use parse methods.  </p>\n'}, {'answer_id': 36776, 'author': 'Bartosz Bierkowski', 'author_id': 3666, 'author_profile': 'https://Stackoverflow.com/users/3666', 'pm_score': 0, 'selected': False, 'text': '<p>Usually I use <a href="http://jaxb.java.net/" rel="nofollow noreferrer">jaxb</a> or <a href="http://xmlbeans.apache.org/" rel="nofollow noreferrer">XMLBeans</a> if I need to create objects serializable to XML. Now, I can see that <a href="http://xstream.codehaus.org/" rel="nofollow noreferrer">XStream</a> might be very useful as it\'s nonintrusive and has really simple api. I\'ll play with it soon and probably use it. The only drawback I noticed is that I can\'t create object\'s id on my own for cross referencing. </p>\n\n<p>@Barak Schiller<br/>\nThanks for posting link to XStream!</p>\n'}, {'answer_id': 469213, 'author': 'ARKBAN', 'author_id': 11889, 'author_profile': 'https://Stackoverflow.com/users/11889', 'pm_score': 4, 'selected': False, 'text': '<h1>"Simple XML Serialization" Project</h1>\n\n<p>You may want to look at the <a href="http://simple.sourceforge.net/" rel="noreferrer">Simple XML Serialization</a> project. It is the closest thing I\'ve found to the System.Xml.Serialization in .Net.</p>\n'}, {'answer_id': 927076, 'author': 'Cheeso', 'author_id': 48082, 'author_profile': 'https://Stackoverflow.com/users/48082', 'pm_score': 0, 'selected': False, 'text': '<p>Don\'t forget <a href="http://jibx.sourceforge.net/" rel="nofollow noreferrer">JiBX</a>.</p>\n'}, {'answer_id': 4067429, 'author': 'so_mv', 'author_id': 186858, 'author_profile': 'https://Stackoverflow.com/users/186858', 'pm_score': 4, 'selected': False, 'text': '<p>JAXB is part of JDK standard edition version 1.6+. So it is <code>FREE</code> and no extra libraries to download and manage.\nA simple example can be found <a href="http://www.java2s.com/Code/Java/JDK-6/MarshalJavaobjecttoxmlandoutputtoconsole.htm" rel="noreferrer">here</a> </p>\n\n<p>XStream seems to be dead. Last update was on Dec 6 2008.\n<code>Simple</code> seems as easy and simpler as JAXB but I could not find any licensing information to evaluate it for enterprise use.</p>\n'}, {'answer_id': 5506572, 'author': 'm.genova', 'author_id': 590689, 'author_profile': 'https://Stackoverflow.com/users/590689', 'pm_score': 0, 'selected': False, 'text': '<p>if you want a <strong>structured solution</strong> (like ORM) then JAXB2 is a good solution.</p>\n\n<p>If you want a serialization like DOT NET then you could use <a href="http://java.sun.com/products/jfc/tsc/articles/persistence3/" rel="nofollow">Long Term Persistence of JavaBeans Components</a></p>\n\n<p>The choice depends on use of serialization.</p>\n'}, {'answer_id': 18681762, 'author': 'Mishax', 'author_id': 1766655, 'author_profile': 'https://Stackoverflow.com/users/1766655', 'pm_score': 3, 'selected': False, 'text': '<p>Worth mentioning that since version 1.4, Java had the classes java.beans.XMLEncoder and java.beans.XMLDecoder. These classes perform XML encoding which is at least very comparable to XML Serialization and in some circumstances might do the trick for you. </p>\n\n<p>If your class sticks to the JavaBeans specification for its getters and setters, this method is straightforward to use and you don\'t need a schema. With the following caveats:</p>\n\n<ul>\n<li>As with normal Java serialization\n\n<ul>\n<li>coding and decoding run over a InputStream and OutputStream</li>\n<li>the process uses the familar writeObject and readObject methods</li>\n</ul></li>\n<li>In contrast to normal Java serialization\n\n<ul>\n<li>the encoding but also decoding causes constructors and initializers to be invoked</li>\n<li>encoding and decoding work regardless if your class implements Serializable or not</li>\n<li>transient modifiers are not taken into account</li>\n<li>works only for public classes, that have public constructors</li>\n</ul></li>\n</ul>\n\n<p>For example, take the following declaration:</p>\n\n<pre><code>public class NPair {\n  public NPair() { }\n  int number1 = 0;\n  int number2 = 0;\n  public void setNumber1(int value) { number1 = value;}\n  public int getNumber1() { return number1; }\n  public void setNumber2(int value) { number2 = value; }\n  public int getNumber2() {return number2;}\n}\n</code></pre>\n\n<p>Executing this code:</p>\n\n<pre><code>NPair fe = new NPair();\nfe.setNumber1(12);\nfe.setNumber2(13);\nFileOutputStream fos1 = new FileOutputStream("d:\\\\ser.xml");\njava.beans.XMLEncoder xe1 = new java.beans.XMLEncoder(fos1);\nxe1.writeObject(fe);\nxe1.close();\n</code></pre>\n\n<p>Would result in the following file:</p>\n\n<pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;\n&lt;java version="1.7.0_02" class="java.beans.XMLDecoder"&gt;\n &lt;object class="NPair"&gt;\n  &lt;void property="number1"&gt;\n   &lt;int&gt;12&lt;/int&gt;\n  &lt;/void&gt;\n  &lt;void property="number2"&gt;\n   &lt;int&gt;13&lt;/int&gt;\n  &lt;/void&gt;\n &lt;/object&gt;\n&lt;/java&gt;\n</code></pre>\n'}, {'answer_id': 26011571, 'author': 'user4067649', 'author_id': 4067649, 'author_profile': 'https://Stackoverflow.com/users/4067649', 'pm_score': -1, 'selected': False, 'text': '<pre><code>public static String genXmlTag(String tagName, String innerXml, String properties )\n{\n    return String.format("&lt;%s %s&gt;%s&lt;/%s&gt;", tagName, properties, innerXml, tagName);\n}\n\npublic static String genXmlTag(String tagName, String innerXml )\n{\n    return genXmlTag(tagName, innerXml, "");\n}\n\npublic static &lt;T&gt; String serializeXML(List&lt;T&gt; list)\n{\n    String result = "";\n    if (list.size() &gt; 0)\n    {\n        T tmp = list.get(0);\n        String clsName = tmp.getClass().getName();\n        String[] splitCls = clsName.split("\\\\.");\n        clsName = splitCls[splitCls.length - 1];\n        Field[] fields = tmp.getClass().getFields();\n\n        for (T t : list)\n        {\n            String row = "";\n            try {\n                for (Field f : fields)\n                {\n                    Object value = f.get(t);\n                    row += genXmlTag(f.getName(), value == null ? "" : value.toString());\n                }\n            } catch (IllegalAccessException e) {\n                e.printStackTrace();\n            }\n            row = genXmlTag(clsName, row);\n\n            result += row;\n        }\n    }\n\n    result = genXmlTag("root", result);\n    return result;\n}\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/35785', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/3583/']