XmlToJson
XmlToJson copied to clipboard
How to skip \n\t
In xml, the format is like:
<Tag>
<Child></Child>
</Tag>
but the "\n" after <Tag> and "\t" before <Child>is also read by xmltojson and set to key default content
after I get the json, I get the result with "content":"\n\t" in jsonobject
How to ignore the special value
Any updates about this? I have the same problem with a similar XML.
Thanks
Hello and sorry for the delay, I've been very busy these days.
Could you please attach an example file for the XML, because when I copy paste your example everything is fine:
{"Tag":{"Child":""}}
thank you and sorry for the issue,
Arnaud.
As the original file is secret for commercial use, so I can't attach here. But the reason is that, between the TAG and Child, there's a tab intent. After check the source. The bug is located in XmlToJson.Java(L:345) ` private JSONObject convertTagToJson(Tag tag, boolean isListElement) { JSONObject json = new JSONObject();
// Content is injected as a key/value
if (tag.getContent() != null) {
String path = tag.getPath();
String name = getContentNameReplacement(path, DEFAULT_CONTENT_NAME);
putContent(path, json, name, tag.getContent());
}
...
}
private String getContentNameReplacement(String path, String defaultValue) {
String result = mContentNameReplacements.get(path);
if (result != null) {
return result;
}
return defaultValue;
}
` the path is /Tag, and the result is null, so default value "content" is added to json
Here is the debug info of Tag object copied from debug breakpoint:
"
"
the content is \n\t, which is not visible but actually exits. "AppSetting" is the root Tag,
I'm also facing the same issue, when I try to an API, please find attached an XML that this library fails to parse, due to the /n characters before each XML element

The weird thing is that the same API is being parsed successfully from my old JAVA app, but not with the new Kotlin app I'm building right now. The only difference is that I was using the InputStream constructor for the XmlToJson in the old app, and I'm using the String constructor now.
Hi @L4grange . Sorry I did not answer your message :/
I still cannot see the problem, the generated JSON file seems ok (but is 2700 lines...):
If you have a 10 lines xml and could provide it + the expected json result + the actual json result, it could help!
Note that I use Java and not Kotlin, but I don't see any reason why it would fail more with kotlin.
Arnaud.
Maybe we can modify the code to this:
/* package */ void setContent(String content) {
// checks that there is a relevant content (not only spaces or \n)
boolean hasContent = false;
if (content != null) {
for (int i = 0; i < content.length(); ++i) {
char c = content.charAt(i);
// if ((c != ' ') && (c != '\n')) {
// hasContent = true;
// break;
// }
if (!(c == '\n' || c == ' ' || c == '\t')) {
hasContent = true;
break;
}
}
}
if (hasContent) {
mContent = content;
}
}
It may be converted to json
{
"AppSettings": {
"Type": "Host Interface",
"SupportIP": "Y",
"ReversalFile": "rev.l",
"MaxTokenLength": "32",
"EDC": {
"CREDIT": {
"ShowName": "CREDIT",
"PrintName": "Credit",
"MinCardLength": "13",
"MaxCardLength": "19",
"AmountSize": "7",
"TipSize": "7",
"TaxSize": "6",
"Transactions": {
"SALE": {
"ShowName": "SALE",
"AbbrName": "SL",
"PrintName": "Sale",
"NeedAccount": "Y"
}
}
}
},
"Batch": {
"Close": {
"Supported": "Y",
"AutoBatch": "Y",
"ClearBatch": "Y"
}
},
"Parameters": {
"TerminalID": {
"Name": "TerminalID",
"Type": "N",
"Min": "1",
"Max": "8"
},
"DeviceId": {
"Name": "DeviceId",
"Type": "S",
"Min": "1",
"Max": "50"
}
},
"TerminalID": {
"ID0": "TerminalID"
}
}
}