Skip to content

Commit f2261a0

Browse files
authored
Merge pull request #18704 from Deepak-Vohra/master
BAEL-9392 Converting Formatted XML File to One Line String in Java
2 parents f52ffda + f1f90b3 commit f2261a0

File tree

3 files changed

+72
-3
lines changed

3 files changed

+72
-3
lines changed

xml-modules/xml-3/pom.xml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,27 @@
1313
<version>1.0.0-SNAPSHOT</version>
1414
</parent>
1515

16+
<dependencies>
17+
<dependency>
18+
<groupId>org.assertj</groupId>
19+
<artifactId>assertj-core</artifactId>
20+
<version>3.26.0</version>
21+
<scope>test</scope>
22+
</dependency>
23+
<dependency>
24+
<groupId>org.xmlunit</groupId>
25+
<artifactId>xmlunit-assertj</artifactId>
26+
<version>2.10.0</version>
27+
<scope>test</scope>
28+
</dependency>
29+
<dependency>
30+
<groupId>org.xmlunit</groupId>
31+
<artifactId>xmlunit-core</artifactId>
32+
<version>2.10.0</version>
33+
<scope>test</scope>
34+
</dependency>
35+
</dependencies>
36+
1637
<build>
1738
<finalName>xml-3</finalName>
1839
<resources>

xml-modules/xml-3/src/test/java/com/baeldung/xml/XmlDocumentUnitTest.java

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,20 @@
66
import org.xml.sax.InputSource;
77
import org.xml.sax.SAXParseException;
88

9-
import javax.xml.parsers.DocumentBuilder;
10-
import javax.xml.parsers.DocumentBuilderFactory;
11-
import javax.xml.parsers.ParserConfigurationException;
9+
import java.io.BufferedReader;
10+
import java.io.FileReader;
11+
import java.io.IOException;
1212
import java.io.ByteArrayInputStream;
1313
import java.io.InputStream;
1414
import java.io.StringReader;
15+
16+
import javax.xml.parsers.DocumentBuilder;
17+
import javax.xml.parsers.DocumentBuilderFactory;
18+
import javax.xml.parsers.ParserConfigurationException;
1519
import java.nio.charset.StandardCharsets;
1620

1721
import static org.junit.jupiter.api.Assertions.*;
22+
import static org.xmlunit.assertj.XmlAssert.assertThat;
1823

1924
public class XmlDocumentUnitTest {
2025

@@ -57,6 +62,42 @@ public void givenDocument_whenInsertNode_thenNodeAdded() throws Exception {
5762
assertEquals("child", existingDocument.getDocumentElement().getChildNodes().item(0).getNodeName());
5863
}
5964

65+
@Test
66+
public void givenXmlFile_whenConvertToOneLineString_thenSuccess() throws IOException {
67+
68+
String filePath = "posts.xml";
69+
ClassLoader classLoader = getClass().getClassLoader();
70+
FileReader fileReader = new FileReader(classLoader
71+
.getResource(filePath)
72+
.getFile());
73+
StringBuilder xmlContentBuilder = new StringBuilder();
74+
try (BufferedReader reader = new BufferedReader(fileReader)) {
75+
String line;
76+
while ((line = reader.readLine()) != null) {
77+
xmlContentBuilder.append(line);
78+
}
79+
}
80+
String xmlString = xmlContentBuilder.toString();
81+
// Remove tabs
82+
xmlString = xmlString.replaceAll("\\t", "");
83+
84+
// Replace multiple spaces with a single space
85+
xmlString = xmlString.replaceAll(" +", " ");
86+
87+
// Remove spaces before/after tags (e.g., "> <" becomes "><")
88+
// This is important to ensure truly minimal whitespace
89+
xmlString = xmlString.replaceAll(">\\s+<", "><");
90+
91+
// Trim leading/trailing whitespace from the entire string
92+
String oneLineXml = xmlString.trim();
93+
94+
String expectedXml = """
95+
<?xml version="1.0" encoding="UTF-8"?><posts><post postId="1"><title>Parsing XML as a String in Java</title><author>John Doe</author></post></posts>
96+
""";
97+
98+
assertThat(oneLineXml).and(expectedXml).areIdentical();
99+
}
100+
60101
@Test
61102
public void givenInvalidXmlString_whenConvertToDocument_thenThrowException() throws ParserConfigurationException {
62103
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<posts>
3+
<post postId="1">
4+
<title>Parsing XML as a String in Java</title>
5+
<author>John Doe</author>
6+
</post>
7+
</posts>

0 commit comments

Comments
 (0)