#748 - close XML tag explicitly for empty tags with configuration.

This commit is contained in:
rudrajyoti biswas
2023-10-25 23:23:00 +05:30
parent 1a2108efa2
commit c6ec2f0e4c
4 changed files with 70 additions and 8 deletions

View File

@@ -557,6 +557,17 @@ public class XMLConfigurationTest {
assertEquals(actualXML, resultXML);
}
@Test
public void shouldHandleEmptyNodeValue()
{
JSONObject inputJSON = new JSONObject();
inputJSON.put("Emptyness", "");
String expectedXmlWithoutExplicitEndTag = "<Emptyness/>";
String expectedXmlWithExplicitEndTag = "<Emptyness></Emptyness>";
assertEquals(expectedXmlWithoutExplicitEndTag, XML.toString(inputJSON, null, new XMLParserConfiguration().withCloseEmptyTag(false)));
assertEquals(expectedXmlWithExplicitEndTag, XML.toString(inputJSON, null, new XMLParserConfiguration().withCloseEmptyTag(true)));
}
/**
* Investigate exactly how the "content" keyword works
*/

View File

@@ -1177,6 +1177,26 @@ public class XMLTest {
}
@Test
public void shouldCreateExplicitEndTagWithEmptyValueWhenConfigured(){
String jsonString = "{outer:{innerOne:\"\", innerTwo:\"two\"}}";
JSONObject jsonObject = new JSONObject(jsonString);
String expectedXmlString = "<encloser><outer><innerOne></innerOne><innerTwo>two</innerTwo></outer></encloser>";
String xmlForm = XML.toString(jsonObject,"encloser", new XMLParserConfiguration().withCloseEmptyTag(true));
assertEquals(expectedXmlString, xmlForm);
}
@Test
public void shouldNotCreateExplicitEndTagWithEmptyValueWhenNotConfigured(){
String jsonString = "{outer:{innerOne:\"\", innerTwo:\"two\"}}";
JSONObject jsonObject = new JSONObject(jsonString);
String expectedXmlString = "<encloser><outer><innerOne/><innerTwo>two</innerTwo></outer></encloser>";
String xmlForm = XML.toString(jsonObject,"encloser", new XMLParserConfiguration().withCloseEmptyTag(false));
assertEquals(expectedXmlString, xmlForm);
}
@Test
public void testIndentSimpleJsonObject(){
String str = "{ \"employee\": { \n" +