[json-lib-user] Fwd: XMLSerializer usage for xml text to json conversion
Brought to you by:
aalmiray
From: Sudharshan P. <pol...@gm...> - 2008-02-19 15:09:35
|
---------- Forwarded message ---------- From: Sudharshan Pollapally <pol...@gm...> Date: Feb 14, 2008 4:37 PM Subject: Re: [json-lib-user] XMLSerializer usage for xml text to json conversion To: Andres Almiray <aal...@ya...> Hi Andres, This is what I tried, on jdk 1.4 <pre> import org.json.JSONException; import org.json.XML; import net.sf.json.xml.XMLSerializer; public class JsonTest { public static void main(String[] args) { String xmlStr ="<index><magazine>mag</magazine><issueDate /><promotions /><featureSet><features><sequence>1</sequence><style>set1</style><featuresHeader></featuresHeader><feature><style /></feature></features><features><sequence>2</sequence><style>set2</style><featuresHeader/><feature> <style /></feature><feature> <style /></feature><feature> <style /></feature></features><features><sequence>3</sequence><style>set2</style><featuresHeader/><feature> <style /></feature><feature> <style /></feature><feature> <style /></feature></features><features><sequence>4</sequence><style>set2</style><featuresHeader/><feature> <style /></feature><feature> <style /></feature><feature> <style /></feature></features></featureSet><relatedLinks /></index>"; XMLSerializer xmlSerializer = new XMLSerializer(); xmlSerializer.setForceTopLevelObject(true); xmlSerializer.setTypeHintsCompatibility(false); xmlSerializer.setTypeHintsEnabled(false); xmlSerializer.setSkipWhitespace(true); System.out.println("java.class.path: "+System.getProperty(" java.class.path")); System.out.println("xmlSerializer.read: "+xmlSerializer.read(xmlStr)); try { System.out.println("XML.toJSONObject: "+XML.toJSONObject(xmlStr)); } catch (JSONException e) { } } } </pre> Output: java.class.path: C:\Documents and Settings\workspace\Temp\bin;I:\.maven\repository\ezmorph\jars\ezmorph- 1.0.4.jar;I:\.maven\repository\commons-collections\jars\commons- collections-3.2.jar;I:\.maven\repository\json-lib\jars\json-lib-2.2.1.jar ;I:\.maven\repository\xom\jars\xom-1.1.jar ;I:\.maven\repository\commons-beanutils\jars\commons-beanutils-1.7.jar ;I:\.maven\repository\commons-lang\jars\commons-lang-2.2.jar ;I:\.maven\repository\commons-logging\jars\commons-logging-1.1.jar Feb 14, 2008 4:24:02 PM net.sf.json.xml.XMLSerializer getType INFO: Using default type string xmlSerializer.read: {"magazine":"mag","issueDate":[],"promotions":[],"featureSet":[{"sequence":"1","style":"set1","featuresHeader":[],"feature":[[]]},{"sequence":"2","style":"set2","featuresHeader":[],"feature":[[],[[]],[[]]]},{"sequence":"3","style":"set2","featuresHeader":[],"feature":[[],[[]],[[]]]},{"sequence":"4","style":"set2","featuresHeader":[],"feature":[[],[[]],[[]]]}],"relatedLinks":[]} XML.toJSONObject: {"index":{"featureSet":{"features":[{"style":"set1","featuresHeader":"","feature":{"style":{}},"sequence":"1"},{"style":"set2","featuresHeader":{},"feature":[{"style":{}},{"style":{}},{"style":{}}],"sequence":"2"},{"style":"set2","featuresHeader":{},"feature":[{"style":{}},{"style":{}},{"style":{}}],"sequence":"3"},{"style":"set2","featuresHeader":{},"feature":[{"style":{}},{"style":{}},{"style":{}}],"sequence":"4"}]},"promotions":{},"magazine":"mag","issueDate":{},"relatedLinks":{}}} Notice that <index> and <features> nodes are missing in the string generated by xmlSerializer.read. Thanks, polapali On Thu, Feb 14, 2008 at 3:16 PM, Andres Almiray <aal...@ya...> wrote: > Please send an snippet of your XML (a failing test would be better! =-)) > Your report has little information that can be used to pinpoint the > problem, thanks! > > Cheers, > Andres > > ------------------------------------------- > http://jroller.com/aalmiray > http://www.linkedin.com/in/aalmiray > -- > What goes up, must come down. Ask any system administrator. > There are 10 types of people in the world: Those who understand binary, > and those who don't. > To understand recursion, we must first understand recursion. > > ----- Original Message ---- > From: Sudharshan Pollapally <pol...@gm...> > To: jso...@li... > Sent: Wednesday, February 13, 2008 2:31:43 PM > Subject: [json-lib-user] XMLSerializer usage for xml text to json > conversion > > Hi, > I am trying to use json-lib 2.2.1 XMLSerializer.read(xmlString) to > convert xml text to json, similar to org.json.XML.toJSONObject(xmlString). > > I see at least the following things missing in the json generated by > XMLSerializer: > 1) the root element of the input xml is not included in the result json. > 2) My input xml text which has nothing to do with json syntax, has > multiple "<features>" nodes, all these nodes seems be omitted while > conversion. I tried the options mentioned at > http://sourceforge.net/tracker/index.php?func=detail&aid=1813520&group_id=171425&atid=857931without luck. > > I am I missing something with this relatively simple usecase? Thanks for > the help. > -polapali > > > -----Inline Attachment Follows----- > > ------------------------------------------------------------------------- > This SF.net email is sponsored by: Microsoft > Defy all challenges. Microsoft(R) Visual Studio 2008. > http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/ > > > -----Inline Attachment Follows----- > > _______________________________________________ > json-lib-user mailing list > jso...@li... > https://lists.sourceforge.net/lists/listinfo/json-lib-user > > > ------------------------------ > Be a better friend, newshound, and know-it-all with Yahoo! Mobile. Try it > now.<http://us.rd.yahoo.com/evt=51733/*http://mobile.yahoo.com/;_ylt=Ahu06i62sR8HDtDypao8Wcj9tAcJ> > |