mirror of
https://github.com/fankes/JSON-java-compat.git
synced 2025-09-08 19:44:29 +08:00
#748 - close XML tag explicitly for empty tags with configuration.
This commit is contained in:
@@ -877,12 +877,25 @@ public class XML {
|
||||
}
|
||||
}
|
||||
} else if ("".equals(value)) {
|
||||
sb.append(indent(indent));
|
||||
sb.append('<');
|
||||
sb.append(key);
|
||||
sb.append("/>");
|
||||
if(indentFactor > 0){
|
||||
sb.append("\n");
|
||||
if (config.isCloseEmptyTag()){
|
||||
sb.append(indent(indent));
|
||||
sb.append('<');
|
||||
sb.append(key);
|
||||
sb.append(">");
|
||||
sb.append("</");
|
||||
sb.append(key);
|
||||
sb.append(">");
|
||||
if (indentFactor > 0) {
|
||||
sb.append("\n");
|
||||
}
|
||||
}else {
|
||||
sb.append(indent(indent));
|
||||
sb.append('<');
|
||||
sb.append(key);
|
||||
sb.append("/>");
|
||||
if (indentFactor > 0) {
|
||||
sb.append("\n");
|
||||
}
|
||||
}
|
||||
|
||||
// Emit a new tag <k>
|
||||
|
@@ -43,6 +43,13 @@ public class XMLParserConfiguration extends ParserConfiguration {
|
||||
*/
|
||||
private boolean convertNilAttributeToNull;
|
||||
|
||||
/**
|
||||
* When creating an XML from JSON Object, an empty tag by default will self-close.
|
||||
* If it has to be closed explicitly, with empty content between start and end tag,
|
||||
* this flag is to be turned on.
|
||||
*/
|
||||
private boolean closeEmptyTag;
|
||||
|
||||
/**
|
||||
* This will allow type conversion for values in XML if xsi:type attribute is defined
|
||||
*/
|
||||
@@ -145,12 +152,13 @@ public class XMLParserConfiguration extends ParserConfiguration {
|
||||
*/
|
||||
private XMLParserConfiguration (final boolean keepStrings, final String cDataTagName,
|
||||
final boolean convertNilAttributeToNull, final Map<String, XMLXsiTypeConverter<?>> xsiTypeMap, final Set<String> forceList,
|
||||
final int maxNestingDepth) {
|
||||
final int maxNestingDepth, final boolean closeEmptyTag) {
|
||||
super(keepStrings, maxNestingDepth);
|
||||
this.cDataTagName = cDataTagName;
|
||||
this.convertNilAttributeToNull = convertNilAttributeToNull;
|
||||
this.xsiTypeMap = Collections.unmodifiableMap(xsiTypeMap);
|
||||
this.forceList = Collections.unmodifiableSet(forceList);
|
||||
this.closeEmptyTag = closeEmptyTag;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -169,7 +177,8 @@ public class XMLParserConfiguration extends ParserConfiguration {
|
||||
this.convertNilAttributeToNull,
|
||||
this.xsiTypeMap,
|
||||
this.forceList,
|
||||
this.maxNestingDepth
|
||||
this.maxNestingDepth,
|
||||
this.closeEmptyTag
|
||||
);
|
||||
}
|
||||
|
||||
@@ -303,4 +312,13 @@ public class XMLParserConfiguration extends ParserConfiguration {
|
||||
public XMLParserConfiguration withMaxNestingDepth(int maxNestingDepth) {
|
||||
return super.withMaxNestingDepth(maxNestingDepth);
|
||||
}
|
||||
|
||||
public XMLParserConfiguration withCloseEmptyTag(boolean closeEmptyTag){
|
||||
this.closeEmptyTag = closeEmptyTag;
|
||||
return this;
|
||||
}
|
||||
|
||||
public boolean isCloseEmptyTag() {
|
||||
return this.closeEmptyTag;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user