fork(1) download
  1. import java.io.StringWriter;
  2. import java.lang.reflect.Field;
  3. import java.lang.reflect.Method;
  4.  
  5. import javax.xml.parsers.DocumentBuilderFactory;
  6. import javax.xml.transform.OutputKeys;
  7. import javax.xml.transform.Transformer;
  8. import javax.xml.transform.TransformerFactory;
  9. import javax.xml.transform.dom.DOMSource;
  10. import javax.xml.transform.stream.StreamResult;
  11.  
  12. import org.w3c.dom.Document;
  13. import org.w3c.dom.Element;
  14. import org.w3c.dom.Node;
  15. import org.w3c.dom.NodeList;
  16.  
  17.  
  18. class Ideone {
  19.  
  20. static class Container {
  21. Container Container1;
  22. Container Container2;
  23. AnotherContainer AnotherContainer;
  24. String name;
  25. }
  26.  
  27. static class AnotherContainer {
  28. String name;
  29. int value;
  30. }
  31.  
  32. static Document save(Object obj) throws Exception {
  33. Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
  34.  
  35. save(obj, obj.getClass().getSimpleName(), doc, doc);
  36.  
  37. return doc;
  38. }
  39.  
  40. static void save(Object obj, String name, Node parent, Document doc) throws Exception {
  41. Class<?> objCls = obj.getClass();
  42.  
  43. Element elt = doc.createElement(name);
  44. parent.appendChild(elt);
  45. if (objCls.getCanonicalName().startsWith(Ideone.class.getSimpleName())) {
  46. for (Field field : objCls.getDeclaredFields()) {
  47. Object value = field.get(obj);
  48. if (value != null) {
  49. save(value, field.getName(), elt, doc);
  50. }
  51. }
  52. } else {
  53. elt.setTextContent(obj.toString());
  54. }
  55. }
  56.  
  57. static <E> E load(Document doc, Class<E> cls) throws Exception {
  58. Element elt = doc.getDocumentElement();
  59. E obj = cls.newInstance();
  60. load(obj, elt);
  61. return obj;
  62. }
  63.  
  64. static void load(Object obj, Element elt) throws Exception {
  65. Class<?> cls = obj.getClass();
  66. NodeList children = elt.getChildNodes();
  67. for (int i = 0; i < children.getLength(); i++) {
  68. if (children.item(i).getNodeType() == Node.ELEMENT_NODE) {
  69. Element childElt = (Element) children.item(i);
  70. Field field = cls.getDeclaredField(childElt.getTagName());
  71. Class<?> childCls = field.getType();
  72. if (childCls.getCanonicalName().startsWith(Ideone.class.getSimpleName())) {
  73. Object childObj = childCls.newInstance();
  74. field.set(obj, childObj);
  75. load(childObj, childElt);
  76. } else {
  77. String value = childElt.getTextContent();
  78. if (childCls == String.class) {
  79. field.set(obj, value);
  80. } else {
  81. childCls = toWrapper(childCls);
  82. Method method = childCls.getMethod("valueOf", String.class);
  83. field.set(obj, method.invoke(null, value));
  84. }
  85. }
  86. }
  87. }
  88. }
  89.  
  90. static Class<?> toWrapper(Class<?> cls) {
  91. if (cls.isPrimitive()) {
  92. if (Integer.TYPE == cls) {
  93. cls = Integer.class;
  94. } else if (Double.TYPE == cls) {
  95. cls = Double.class;
  96. }
  97. }
  98. return cls;
  99. }
  100.  
  101. public static void main(String[] args) throws Exception {
  102. Container rootContainer = new Container();
  103. rootContainer.name = "root";
  104. rootContainer.Container1 = new Container();
  105. rootContainer.Container1.name = "root.c1";
  106. rootContainer.Container2 = new Container();
  107. rootContainer.Container2.name = "root.c2";
  108. rootContainer.AnotherContainer = new AnotherContainer();
  109. rootContainer.AnotherContainer.name = "root.c3";
  110. rootContainer.AnotherContainer.value = 42;
  111. rootContainer.Container2.AnotherContainer = new AnotherContainer();
  112. rootContainer.Container2.AnotherContainer.name = "root.c2.c3";
  113. rootContainer.Container2.AnotherContainer.value = 21;
  114.  
  115. Document doc = save(rootContainer);
  116.  
  117. StreamResult result = new StreamResult(new StringWriter());
  118. TransformerFactory transformerFactory = TransformerFactory.newInstance();
  119. transformerFactory.setAttribute("indent-number", 2);
  120. Transformer transform = transformerFactory.newTransformer();
  121. transform.setOutputProperty(OutputKeys.INDENT, "yes");
  122. transform.transform(new DOMSource(doc), result);
  123.  
  124. String xml = result.getWriter().toString();
  125.  
  126. System.out.println("Saved to xml:");
  127. System.out.println(xml);
  128.  
  129. Container loadedContainer = load(doc, Container.class);
  130. System.out.println(loadedContainer.Container2.AnotherContainer.name);
  131. System.out.println(loadedContainer.Container2.AnotherContainer.value);
  132. }
  133.  
  134. }
  135.  
Success #stdin #stdout 0.14s 380928KB
stdin
Standard input is empty
stdout
Saved to xml:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<Container>
  <Container1>
    <name>root.c1</name>
  </Container1>
  <Container2>
    <AnotherContainer>
      <name>root.c2.c3</name>
      <value>21</value>
    </AnotherContainer>
    <name>root.c2</name>
  </Container2>
  <AnotherContainer>
    <name>root.c3</name>
    <value>42</value>
  </AnotherContainer>
  <name>root</name>
</Container>

root.c2.c3
21