{"id":40,"date":"2005-11-21T21:48:38","date_gmt":"2005-11-22T02:48:38","guid":{"rendered":"http:\/\/www.juixe.com\/techknow\/?p=40"},"modified":"2005-11-21T21:48:38","modified_gmt":"2005-11-22T02:48:38","slug":"using-dom4j-writing-an-xml","status":"publish","type":"post","link":"http:\/\/juixe.com\/techknow\/index.php\/2005\/11\/21\/using-dom4j-writing-an-xml\/","title":{"rendered":"Using Dom4J: Writing An XML"},"content":{"rendered":"<p>In a previous post, see <a href='http:\/\/www.juixe.com\/techknow\/index.php\/2005\/11\/08\/using-dom4j-reading-an-xml-file\/' title='Using Dom4J: Reading An XML'>Using Dom4J: Reading An XML<\/a>, I went over how to read in an XML document using Dom4J.  Here I&#8217;ll show how to write an XML document using Dom4J.  The first part to writing out an XML document is to have an in memory representation of it.  Here is how you construct and XML document.<\/p>\n<pre>\nDocument doc = DocumentFactory.getInstance()\n\t.createDocument();\nElement root = doc.addElement(&quot;myroot&quot;);\nroot.addAttribute(&quot;myattr&quot;, &quot;myval&quot;);\nElement elem = root.addElement(&quot;myelem&quot;);\nelem.addText(&quot;my text&quot;);\n<\/pre>\n<p>In this code you see how to create elements with child elements, how to add attributes and text data to elements.  Once we have an in memory representation of the XML file, we can serialize it to the file system like this:<\/p>\n<pre>\nFileOutputStream fos = new FileOutputStream(&quot;simple.xml&quot;);\nOutputFormat format = OutputFormat.createPrettyPrint();\nXMLWriter writer = new XMLWriter(fos, format);\nwriter.write(doc);\nwriter.flush();\n<\/pre>\n<p>All this code will produce the following XML document:<\/p>\n<pre>\n&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;\n\n&lt;myroot myattr=&quot;myval&quot;&gt;\n&lt;myelem&gt;my text&lt;\/myelem&gt;\n&lt;\/myroot&gt;\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>In a previous post, see Using Dom4J: Reading An XML, I went over how to read in an XML document using Dom4J. Here I&#8217;ll show how to write an XML document using Dom4J. The first part to writing out an XML document is to have an in memory representation of it. Here is how you [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","footnotes":""},"categories":[13,15,3],"tags":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p902K-E","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/posts\/40"}],"collection":[{"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/comments?post=40"}],"version-history":[{"count":0,"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/posts\/40\/revisions"}],"wp:attachment":[{"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/media?parent=40"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/categories?post=40"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/juixe.com\/techknow\/index.php\/wp-json\/wp\/v2\/tags?post=40"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}