Removing space from an XML element using java

I have JSON as follows

String str = {'Emp name' : 'JSON','Emp id' : 1,'Salary' : 20997.00}

I want to hide this JSON in XML using java.My java code here.

JSON json= JSONSerializer.toJSON(str);
XMLSerializer xmlSerializer = new XMLSerializer();  

//To Skip the white space from XML data and not from XML Element (By default it does)
    xmlSerializer.setSkipWhitespace(true);  
    //To set type of xml element If it true, it will be type
    xmlSerializer.setTypeHintsCompatibility(true);  
    xmlSerializer.setRootName("book");  
    String xml = xmlSerializer.write( json );  
    System.out.println(xml);        

I get output if I pass xml without a space between the XML element (e.g. Emp name as Empname and Emp id as Empid). I want to remove whitespace from an XML element, not from the contents of an XML element.

+4
source share
2 answers

I decompiled and checked json-lib (2.4) and xom (1.2.5) libraies. Unfortunately, there are no such pre / post processors or handlers in relation to the key. This applies both when building JSON and when creating XML.

There seems to be no other way to manually fix the JSON keys. So please check below snippet:

public static void main(String[] args) {

        String str = "{'Emp name' : 'JSON','Emp id' : 1,'Salary' : 20997.00, " +
                "'manager' : {'first name':'hasan', 'last name' : 'kahraman'}," +
                "'co workers': [{'first name':'john', 'last name' : 'wick'}, " +
                "{'first name':'albert', 'last name' : 'smith'}]}";

        JsonConfig config = new JsonConfig();
        JSON json = JSONSerializer.toJSON(str, config);

        fixJsonKey(json);

        XMLSerializer xmlSerializer = new XMLSerializer();
        //To Skip the white space from XML data and not from XML Element (By default it does)
        xmlSerializer.setSkipWhitespace(true);
        //To set type of xml element If it true, it will be type
        xmlSerializer.setTypeHintsCompatibility(true);
        xmlSerializer.setRootName("book");

        String xml = xmlSerializer.write(json);
        System.out.println(xml);
    }

    private static void fixJsonKey(Object json) {

        if (json instanceof JSONObject) {
            JSONObject jsonObject = (JSONObject) json;
            List<String> keyList = new LinkedList<String>(jsonObject.keySet());
            for (String key : keyList) {
                if (!key.matches(".*[\\s\t\n]+.*")) {
                    Object value = jsonObject.get(key);
                    fixJsonKey(value);
                    continue;
                }

                Object value = jsonObject.remove(key);
                String newKey = key.replaceAll("[\\s\t\n]", "");

                fixJsonKey(value);

                jsonObject.accumulate(newKey, value);
            }
        } else if (json instanceof JSONArray) {
            for (Object aJsonArray : (JSONArray) json) {
                fixJsonKey(aJsonArray);
            }
        }
    }

The output is as follows:

<?xml version="1.0" encoding="UTF-8"?>
<book>
    <Empid type="number">1</Empid>
    <Empname type="string">JSON</Empname>
    <Salary type="number">20997.0</Salary>
    <coworkers class="array">
        <e class="object">
            <firstname type="string">john</firstname>
            <lastname type="string">wick</lastname>
        </e>
        <e class="object">
            <firstname type="string">albert</firstname>
            <lastname type="string">smith</lastname>
        </e>
    </coworkers>
    <manager class="object">
        <firstname type="string">hasan</firstname>
        <lastname type="string">kahraman</lastname>
    </manager>
</book>
+1

JSON / , Jackson. DTO ( ):

class Employee {
    private String name;

    @JsonProperty("Emp name")
    public String getname() { return name; }

    ...setter...
}

. " : "

+1

All Articles