{"id":4471,"date":"2024-03-14T01:30:45","date_gmt":"2024-03-14T01:30:45","guid":{"rendered":"https:\/\/www.silicloud.com\/blog\/how-to-remove-empty-lines-when-deleting-xml-in-dom4j\/"},"modified":"2025-07-31T08:26:13","modified_gmt":"2025-07-31T08:26:13","slug":"how-to-remove-empty-lines-when-deleting-xml-in-dom4j","status":"publish","type":"post","link":"https:\/\/www.silicloud.com\/blog\/how-to-remove-empty-lines-when-deleting-xml-in-dom4j\/","title":{"rendered":"dom4j Remove Empty Lines in XML"},"content":{"rendered":"<p>To remove empty lines when parsing an XML file with dom4j, you can achieve this by setting the output format to remove empty lines. Here are the specific steps:<\/p>\n<ol>\n<li>Create an XML document object.<\/li>\n<\/ol>\n<pre class=\"post-pre\"><code><span class=\"hljs-type\">Document<\/span> <span class=\"hljs-variable\">document<\/span> <span class=\"hljs-operator\">=<\/span> DocumentHelper.createDocument();\r\n<\/code><\/pre>\n<ol>\n<li>Add elements such as root elements and child elements.<\/li>\n<\/ol>\n<pre class=\"post-pre\"><code><span class=\"hljs-type\">Element<\/span> <span class=\"hljs-variable\">root<\/span> <span class=\"hljs-operator\">=<\/span> document.addElement(<span class=\"hljs-string\">\"root\"<\/span>);\r\n<span class=\"hljs-type\">Element<\/span> <span class=\"hljs-variable\">child1<\/span> <span class=\"hljs-operator\">=<\/span> root.addElement(<span class=\"hljs-string\">\"child1\"<\/span>);\r\n<span class=\"hljs-type\">Element<\/span> <span class=\"hljs-variable\">child2<\/span> <span class=\"hljs-operator\">=<\/span> root.addElement(<span class=\"hljs-string\">\"child2\"<\/span>);\r\n<\/code><\/pre>\n<ol>\n<li>Output the XML file and set the formatting for output.<\/li>\n<\/ol>\n<pre class=\"post-pre\"><code><span class=\"hljs-type\">OutputFormat<\/span> <span class=\"hljs-variable\">format<\/span> <span class=\"hljs-operator\">=<\/span> OutputFormat.createPrettyPrint();\r\nformat.setTrimText(<span class=\"hljs-literal\">true<\/span>); <span class=\"hljs-comment\">\/\/ \u53bb\u6389\u6587\u672c\u8282\u70b9\u4e24\u8fb9\u7684\u7a7a\u767d<\/span>\r\nformat.setNewlines(<span class=\"hljs-literal\">true<\/span>); <span class=\"hljs-comment\">\/\/ \u662f\u5426\u6362\u884c<\/span>\r\nformat.setIndent(<span class=\"hljs-literal\">true<\/span>); <span class=\"hljs-comment\">\/\/ \u662f\u5426\u7f29\u8fdb<\/span>\r\nformat.setIndentSize(<span class=\"hljs-number\">2<\/span>); <span class=\"hljs-comment\">\/\/ \u7f29\u8fdb\u7a7a\u683c\u6570<\/span>\r\n\r\n<span class=\"hljs-type\">XMLWriter<\/span> <span class=\"hljs-variable\">writer<\/span> <span class=\"hljs-operator\">=<\/span> <span class=\"hljs-keyword\">new<\/span> <span class=\"hljs-title class_\">XMLWriter<\/span>(<span class=\"hljs-keyword\">new<\/span> <span class=\"hljs-title class_\">FileWriter<\/span>(<span class=\"hljs-string\">\"output.xml\"<\/span>), format);\r\nwriter.write(document);\r\nwriter.close();\r\n<\/code><\/pre>\n<p>By setting format.setTrimText(true), you can remove the whitespace on both sides of the text node, thereby achieving the purpose of removing empty lines.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>To remove empty lines when parsing an XML file with dom4j, you can achieve this by setting the output format to remove empty lines. Here are the specific steps: Create an XML document object. Document document = DocumentHelper.createDocument(); Add elements such as root elements and child elements. Element root = document.addElement(&#8220;root&#8221;); Element child1 = root.addElement(&#8220;child1&#8221;); [&hellip;]<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_import_markdown_pro_load_document_selector":0,"_import_markdown_pro_submit_text_textarea":"","footnotes":""},"categories":[1],"tags":[4073,189,4075,4074,188],"class_list":["post-4471","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-dom4j","tag-java-xml","tag-remove-empty-lines","tag-xml-formatting","tag-xml-parsing"],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v21.5 (Yoast SEO v21.5) - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>dom4j Remove Empty Lines in XML - Blog - Silicon Cloud<\/title>\n<meta name=\"description\" content=\"Learn how to clean XML output by removing blank lines in dom4j. Optimize Java XML formatting.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.silicloud.com\/blog\/how-to-remove-empty-lines-when-deleting-xml-in-dom4j\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"dom4j Remove Empty Lines in XML\" \/>\n<meta property=\"og:description\" content=\"Learn how to clean XML output by removing blank lines in dom4j. Optimize Java XML formatting.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.silicloud.com\/blog\/how-to-remove-empty-lines-when-deleting-xml-in-dom4j\/\" \/>\n<meta property=\"og:site_name\" content=\"Blog - Silicon Cloud\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/SiliCloudGlobal\/\" \/>\n<meta property=\"article:published_time\" content=\"2024-03-14T01:30:45+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-07-31T08:26:13+00:00\" \/>\n<meta name=\"author\" content=\"Emily Johnson\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@SiliCloudGlobal\" \/>\n<meta name=\"twitter:site\" content=\"@SiliCloudGlobal\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Emily Johnson\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/how-to-remove-empty-lines-when-deleting-xml-in-dom4j\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/how-to-remove-empty-lines-when-deleting-xml-in-dom4j\/\"},\"author\":{\"name\":\"Emily Johnson\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/3b041b19cffc258705478ecfab895378\"},\"headline\":\"dom4j Remove Empty Lines in XML\",\"datePublished\":\"2024-03-14T01:30:45+00:00\",\"dateModified\":\"2025-07-31T08:26:13+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/how-to-remove-empty-lines-when-deleting-xml-in-dom4j\/\"},\"wordCount\":86,\"publisher\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/#organization\"},\"keywords\":[\"dom4j\",\"Java XML\",\"remove empty lines\",\"XML formatting\",\"XML parsing\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/how-to-remove-empty-lines-when-deleting-xml-in-dom4j\/\",\"url\":\"https:\/\/www.silicloud.com\/blog\/how-to-remove-empty-lines-when-deleting-xml-in-dom4j\/\",\"name\":\"dom4j Remove Empty Lines in XML - Blog - Silicon Cloud\",\"isPartOf\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/#website\"},\"datePublished\":\"2024-03-14T01:30:45+00:00\",\"dateModified\":\"2025-07-31T08:26:13+00:00\",\"description\":\"Learn how to clean XML output by removing blank lines in dom4j. Optimize Java XML formatting.\",\"breadcrumb\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/how-to-remove-empty-lines-when-deleting-xml-in-dom4j\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.silicloud.com\/blog\/how-to-remove-empty-lines-when-deleting-xml-in-dom4j\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/how-to-remove-empty-lines-when-deleting-xml-in-dom4j\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.silicloud.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"dom4j Remove Empty Lines in XML\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#website\",\"url\":\"https:\/\/www.silicloud.com\/blog\/\",\"name\":\"Silicon Cloud Blog\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/#organization\"},\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#organization\",\"name\":\"Silicon Cloud Blog\",\"url\":\"https:\/\/www.silicloud.com\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.silicloud.com\/blog\/wp-content\/uploads\/2023\/11\/EN-SILICON-Full.png\",\"contentUrl\":\"https:\/\/www.silicloud.com\/blog\/wp-content\/uploads\/2023\/11\/EN-SILICON-Full.png\",\"width\":1024,\"height\":1024,\"caption\":\"Silicon Cloud Blog\"},\"image\":{\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/SiliCloudGlobal\/\",\"https:\/\/twitter.com\/SiliCloudGlobal\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/3b041b19cffc258705478ecfab895378\",\"name\":\"Emily Johnson\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/a5cb4e73d02ab1d79f2dfe919389ff7c1de072baa97686392031c03d858cc358?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/a5cb4e73d02ab1d79f2dfe919389ff7c1de072baa97686392031c03d858cc358?s=96&d=mm&r=g\",\"caption\":\"Emily Johnson\"},\"url\":\"https:\/\/www.silicloud.com\/blog\/author\/emilyjohnson\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"dom4j Remove Empty Lines in XML - Blog - Silicon Cloud","description":"Learn how to clean XML output by removing blank lines in dom4j. Optimize Java XML formatting.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.silicloud.com\/blog\/how-to-remove-empty-lines-when-deleting-xml-in-dom4j\/","og_locale":"en_US","og_type":"article","og_title":"dom4j Remove Empty Lines in XML","og_description":"Learn how to clean XML output by removing blank lines in dom4j. Optimize Java XML formatting.","og_url":"https:\/\/www.silicloud.com\/blog\/how-to-remove-empty-lines-when-deleting-xml-in-dom4j\/","og_site_name":"Blog - Silicon Cloud","article_publisher":"https:\/\/www.facebook.com\/SiliCloudGlobal\/","article_published_time":"2024-03-14T01:30:45+00:00","article_modified_time":"2025-07-31T08:26:13+00:00","author":"Emily Johnson","twitter_card":"summary_large_image","twitter_creator":"@SiliCloudGlobal","twitter_site":"@SiliCloudGlobal","twitter_misc":{"Written by":"Emily Johnson","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.silicloud.com\/blog\/how-to-remove-empty-lines-when-deleting-xml-in-dom4j\/#article","isPartOf":{"@id":"https:\/\/www.silicloud.com\/blog\/how-to-remove-empty-lines-when-deleting-xml-in-dom4j\/"},"author":{"name":"Emily Johnson","@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/3b041b19cffc258705478ecfab895378"},"headline":"dom4j Remove Empty Lines in XML","datePublished":"2024-03-14T01:30:45+00:00","dateModified":"2025-07-31T08:26:13+00:00","mainEntityOfPage":{"@id":"https:\/\/www.silicloud.com\/blog\/how-to-remove-empty-lines-when-deleting-xml-in-dom4j\/"},"wordCount":86,"publisher":{"@id":"https:\/\/www.silicloud.com\/blog\/#organization"},"keywords":["dom4j","Java XML","remove empty lines","XML formatting","XML parsing"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/www.silicloud.com\/blog\/how-to-remove-empty-lines-when-deleting-xml-in-dom4j\/","url":"https:\/\/www.silicloud.com\/blog\/how-to-remove-empty-lines-when-deleting-xml-in-dom4j\/","name":"dom4j Remove Empty Lines in XML - Blog - Silicon Cloud","isPartOf":{"@id":"https:\/\/www.silicloud.com\/blog\/#website"},"datePublished":"2024-03-14T01:30:45+00:00","dateModified":"2025-07-31T08:26:13+00:00","description":"Learn how to clean XML output by removing blank lines in dom4j. Optimize Java XML formatting.","breadcrumb":{"@id":"https:\/\/www.silicloud.com\/blog\/how-to-remove-empty-lines-when-deleting-xml-in-dom4j\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.silicloud.com\/blog\/how-to-remove-empty-lines-when-deleting-xml-in-dom4j\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.silicloud.com\/blog\/how-to-remove-empty-lines-when-deleting-xml-in-dom4j\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.silicloud.com\/blog\/"},{"@type":"ListItem","position":2,"name":"dom4j Remove Empty Lines in XML"}]},{"@type":"WebSite","@id":"https:\/\/www.silicloud.com\/blog\/#website","url":"https:\/\/www.silicloud.com\/blog\/","name":"Silicon Cloud Blog","description":"","publisher":{"@id":"https:\/\/www.silicloud.com\/blog\/#organization"},"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.silicloud.com\/blog\/#organization","name":"Silicon Cloud Blog","url":"https:\/\/www.silicloud.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/www.silicloud.com\/blog\/wp-content\/uploads\/2023\/11\/EN-SILICON-Full.png","contentUrl":"https:\/\/www.silicloud.com\/blog\/wp-content\/uploads\/2023\/11\/EN-SILICON-Full.png","width":1024,"height":1024,"caption":"Silicon Cloud Blog"},"image":{"@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/SiliCloudGlobal\/","https:\/\/twitter.com\/SiliCloudGlobal"]},{"@type":"Person","@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/3b041b19cffc258705478ecfab895378","name":"Emily Johnson","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.silicloud.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/a5cb4e73d02ab1d79f2dfe919389ff7c1de072baa97686392031c03d858cc358?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/a5cb4e73d02ab1d79f2dfe919389ff7c1de072baa97686392031c03d858cc358?s=96&d=mm&r=g","caption":"Emily Johnson"},"url":"https:\/\/www.silicloud.com\/blog\/author\/emilyjohnson\/"}]}},"_links":{"self":[{"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/posts\/4471","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/comments?post=4471"}],"version-history":[{"count":3,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/posts\/4471\/revisions"}],"predecessor-version":[{"id":149138,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/posts\/4471\/revisions\/149138"}],"wp:attachment":[{"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/media?parent=4471"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/categories?post=4471"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.silicloud.com\/blog\/wp-json\/wp\/v2\/tags?post=4471"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}