json - Remove white space from XML element using java -


i have json follows

string str = {'emp name' : 'json','emp id' : 1,'salary' : 20997.00} 

i want covert json xml using java.my java code here.

json json= jsonserializer.tojson(str); xmlserializer xmlserializer = new xmlserializer();    //to skip white space xml data , not xml element (by default does)     xmlserializer.setskipwhitespace(true);       //to set type of xml element if true, type     xmlserializer.settypehintscompatibility(true);       xmlserializer.setrootname("book");       string xml = xmlserializer.write( json );       system.out.println(xml);         

i getting output if pass xml without space between xml element (i.e emp name empname , emp id empid). want remove whitespace xml element , not xml element content.

i decompiled , checked json-lib(2.4) , xom (1.2.5) libraies. unfortunately there no such pre/post processors or handlers regarding key. applies both when constructing json or building xml well.

seems there no other way manually fix keys of json. 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 white space xml data , not xml element (by default does)         xmlserializer.setskipwhitespace(true);         //to set type of xml element if true, 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());             (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) {             (object ajsonarray : (jsonarray) json) {                 fixjsonkey(ajsonarray);             }         }     } 

output below:

<?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> 

Comments

Popular posts from this blog

Magento/PHP - Get phones on all members in a customer group -

php - Bypass Geo Redirect for specific directories -

php - .htaccess mod_rewrite for dynamic url which has domain names -