XmlToJson icon indicating copy to clipboard operation
XmlToJson copied to clipboard

How to skip \n\t

Open anoreg opened this issue 6 years ago • 8 comments

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

anoreg avatar Sep 02 '19 02:09 anoreg

Any updates about this? I have the same problem with a similar XML.

Thanks

moisesvs avatar Sep 13 '19 07:09 moisesvs

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.

smart-fun avatar Sep 15 '19 20:09 smart-fun

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: " image " the content is \n\t, which is not visible but actually exits. "AppSetting" is the root Tag,

anoreg avatar Oct 14 '19 06:10 anoreg

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 Screenshot 2019-10-15 16 06 50

api.xml.zip

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.

lpbas avatar Oct 15 '19 13:10 lpbas

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...):

api.json.zip

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.

smart-fun avatar Feb 26 '21 20:02 smart-fun

@smart-fun here is a sample xml that cause the bug

sys_opedgerh.zip

anoreg avatar Apr 08 '21 10:04 anoreg

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;
        }
    }

anoreg avatar Apr 13 '21 07:04 anoreg

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"
    }
  }
}

javadev avatar Jun 16 '21 19:06 javadev