I need to convert a string XML to Json, and need to make sure that the values are not changed.
I have tried various methods I found but still the data conversion either changes the values or keys.
I started with the this:
String xml = " <product><a001>045293373</a001><a002>03</a002><productidentifier><b221>02</b221><b244>0545791324</b244></productidentifier><productidentifier><b221>03</b221><b244>9780545791328</b244></productidentifier><productidentifier><b221>13</b221><b244>2016014983</b244></productidentifier><productidentifier><b221>14</b221><b244>09780545791328</b244></productidentifier><productidentifier><b221>15</b221><b244>9780545791328</b244></productidentifier><descriptivedetail><x314>00</x314><b012>BB</b012><b333>B304</b333><b333>B401</b333><b333>B501</b333><measure><x315>01</x315><c094>10.60</c094><c095>in</c095></measure><measure><x315>02</x315><c094>8.80</c094><c095>in</c095></measure><measure><x315>03</x315><c094>1.40</c094><c095>in</c095></measure><measure><x315>08</x315><c094>3.5000</c094><c095>lb</c095></measure><x316>US</x316><productclassification><b274>01</b274><b275>4901.99.0070</b275></productclassification><collection><x329>20</x329><x330>INGRAM</x330><titledetail><b202>01</b202><titleelement><x409>02</x409><b203>Harry Potter</b203></titleelement><titleelement><x409>01</x409><x410>2</x410></titleelement></titledetail></collection><titledetail><b202>01</b202><titleelement><x409>01</x409><x501/><b031>Harry Potter and the Chamber of Secrets: The Illustrated Edition (Harry Potter, Book 2)</b031><b029>Volume 2</b029></titleelement></titledetail><contributor><b034>1</b034><b035>A01</b035><b039>J K</b039><b040>Rowling</b040><contributordate><x417>50</x417><j260>05</j260><b306>1965</b306></contributordate><b044><![CDATA[<p>J.K. ROWLING is the author of the enduringly popular, era-defining Harry Potter seven-book series, which have sold over 600 million copies in 85 languages, been listened to as audiobooks for over one billion hours and made into eight smash hit movies. To accompany the series, she wrote three short companion volumes for charity, including <i>Fantastic Beasts and Where to Find Them</i>, which went on to inspire a new series of films featuring Magizoologist Newt Scamander. Harry's story as a grown-up was continued in a stage play, <i>Harry Potter and the Cursed Child</i>, which J.K. Rowling wrote with playwright Jack Thorne and director John Tiffany.</p></p><p></p><p>In 2020, she returned to publishing for younger children with the fairy tale <i>The Ickabog</i>, the royalties for which she donated to her charitable trust, Volant, to help charities working to alleviate the social effects of the Covid 19 pandemic. Her latest children's novel, <i>The Christmas Pig</i>, was published in 2021.</p></p><p></p><p>J.K. Rowling has received many awards and honours for her writing, including for her detective series written under the name Robert Galbraith. She supports a wide number of humanitarian causes through Volant, and is the founder of the international children's care reform charity Lumos. J.K. Rowling lives in Scotland with her family.</p></p>]]></b044><contributorplace><x418>04</x418><b251>GB</b251></contributorplace></contributor><contributor><b034>2</b034><b035>A12</b035><b039>Jim</b039><b040>Kay</b040><b044><![CDATA[Jim Kay won the Kate Greenaway Medal in 2012 for his illustrations in <i>A Monster Calls</i> by Patrick Ness. Jim studied illustration at the University of Westminster and since graduating has worked in the archives of Tate Britain and the Royal Botanic Gardens at Kew. Jim has produced concept work for television and contributed to a group exhibition at the Victoria and Albert Museum in London. He now lives and works in Northamptonshire, England, with his wife.]]></b044></contributor><n386/></descriptivedetail><collateraldetail><textcontent><x426>02</x426><x427>00</x427><d104>Originally published in a slightly different form in the United Kingdom by Bloomsbury in 1998.</d104></textcontent><textcontent><x426>03</x426><x427>00</x427><d104 textformat = "02"><![CDATA[Award-winning artist Jim Kay illustrates year two of Harry Potter's adventures at Hogwarts, in a stunning, gift-ready format.<p></p>The Dursleys were so mean and hideous that summer that all Harry Potter wanted was to get back to the Hogwarts School for Witchcraft and Wizardry. But just as he's packing his bags, Harry receives a warning from a strange, impish creature named Dobby who says that if Harry Potter returns to Hogwarts, disaster will strike.And strike it does. For in Harry's second year at Hogwarts, fresh torments and horrors arise, including an outrageously stuck-up new professor, Gilderoy Lockhart, a spirit named Moaning Myrtle who haunts the girls' bathroom, and the unwanted attentions of Ron Weasley's younger sister, Ginny.But each of these seem minor annoyances when the real trouble begins, and someone -- or something -- starts turning Hogwarts students to stone. Could it be Draco Malfoy, a more poisonous rival than ever? Could it possibly be Hagrid, whose mysterious past is finally told? Or could it be the one everyone at Hogwarts most suspects... Harry Potter himself?]]></d104></textcontent></collateraldetail></product>";
JSONObject jsonObj =XML.toJSONObject(xml);
System.out.println(jsonObj);
but that let go of the 0’s that were present in the front.
After that I found this solution:
String xml = " <product><a001>045293373</a001><a002>03</a002><productidentifier><b221>02</b221><b244>0545791324</b244></productidentifier><productidentifier><b221>03</b221><b244>9780545791328</b244></productidentifier><productidentifier><b221>13</b221><b244>2016014983</b244></productidentifier><productidentifier><b221>14</b221><b244>09780545791328</b244></productidentifier><productidentifier><b221>15</b221><b244>9780545791328</b244></productidentifier><descriptivedetail><x314>00</x314><b012>BB</b012><b333>B304</b333><b333>B401</b333><b333>B501</b333><measure><x315>01</x315><c094>10.60</c094><c095>in</c095></measure><measure><x315>02</x315><c094>8.80</c094><c095>in</c095></measure><measure><x315>03</x315><c094>1.40</c094><c095>in</c095></measure><measure><x315>08</x315><c094>3.5000</c094><c095>lb</c095></measure><x316>US</x316><productclassification><b274>01</b274><b275>4901.99.0070</b275></productclassification><collection><x329>20</x329><x330>INGRAM</x330><titledetail><b202>01</b202><titleelement><x409>02</x409><b203>Harry Potter</b203></titleelement><titleelement><x409>01</x409><x410>2</x410></titleelement></titledetail></collection><titledetail><b202>01</b202><titleelement><x409>01</x409><x501/><b031>Harry Potter and the Chamber of Secrets: The Illustrated Edition (Harry Potter, Book 2)</b031><b029>Volume 2</b029></titleelement></titledetail><contributor><b034>1</b034><b035>A01</b035><b039>J K</b039><b040>Rowling</b040><contributordate><x417>50</x417><j260>05</j260><b306>1965</b306></contributordate><b044><![CDATA[<p>J.K. ROWLING is the author of the enduringly popular, era-defining Harry Potter seven-book series, which have sold over 600 million copies in 85 languages, been listened to as audiobooks for over one billion hours and made into eight smash hit movies. To accompany the series, she wrote three short companion volumes for charity, including <i>Fantastic Beasts and Where to Find Them</i>, which went on to inspire a new series of films featuring Magizoologist Newt Scamander. Harry's story as a grown-up was continued in a stage play, <i>Harry Potter and the Cursed Child</i>, which J.K. Rowling wrote with playwright Jack Thorne and director John Tiffany.</p></p><p></p><p>In 2020, she returned to publishing for younger children with the fairy tale <i>The Ickabog</i>, the royalties for which she donated to her charitable trust, Volant, to help charities working to alleviate the social effects of the Covid 19 pandemic. Her latest children's novel, <i>The Christmas Pig</i>, was published in 2021.</p></p><p></p><p>J.K. Rowling has received many awards and honours for her writing, including for her detective series written under the name Robert Galbraith. She supports a wide number of humanitarian causes through Volant, and is the founder of the international children's care reform charity Lumos. J.K. Rowling lives in Scotland with her family.</p></p>]]></b044><contributorplace><x418>04</x418><b251>GB</b251></contributorplace></contributor><contributor><b034>2</b034><b035>A12</b035><b039>Jim</b039><b040>Kay</b040><b044><![CDATA[Jim Kay won the Kate Greenaway Medal in 2012 for his illustrations in <i>A Monster Calls</i> by Patrick Ness. Jim studied illustration at the University of Westminster and since graduating has worked in the archives of Tate Britain and the Royal Botanic Gardens at Kew. Jim has produced concept work for television and contributed to a group exhibition at the Victoria and Albert Museum in London. He now lives and works in Northamptonshire, England, with his wife.]]></b044></contributor><n386/></descriptivedetail><collateraldetail><textcontent><x426>02</x426><x427>00</x427><d104>Originally published in a slightly different form in the United Kingdom by Bloomsbury in 1998.</d104></textcontent><textcontent><x426>03</x426><x427>00</x427><d104 textformat = "02"><![CDATA[Award-winning artist Jim Kay illustrates year two of Harry Potter's adventures at Hogwarts, in a stunning, gift-ready format.<p></p>The Dursleys were so mean and hideous that summer that all Harry Potter wanted was to get back to the Hogwarts School for Witchcraft and Wizardry. But just as he's packing his bags, Harry receives a warning from a strange, impish creature named Dobby who says that if Harry Potter returns to Hogwarts, disaster will strike.And strike it does. For in Harry's second year at Hogwarts, fresh torments and horrors arise, including an outrageously stuck-up new professor, Gilderoy Lockhart, a spirit named Moaning Myrtle who haunts the girls' bathroom, and the unwanted attentions of Ron Weasley's younger sister, Ginny.But each of these seem minor annoyances when the real trouble begins, and someone -- or something -- starts turning Hogwarts students to stone. Could it be Draco Malfoy, a more poisonous rival than ever? Could it possibly be Hagrid, whose mysterious past is finally told? Or could it be the one everyone at Hogwarts most suspects... Harry Potter himself?]]></d104></textcontent></collateraldetail></product>";
Reader inputString = new StringReader(xml);
BufferedReader reader = new BufferedReader(inputString);
while ((line = reader.readLine()) != null)
{
str+=line;
}
JSONObject jsondata = XML.toJSONObject(str);
System.out.println(jsondata);
In this the mapping was correct but is is making the name of datatype pop in the JSON so that was also not correct. I did try to remove that but of no use.
I also used the XMLSerialiser but ig in JAVA 1.8 that is not supported and got this error
Exception in thread "main" java.lang.NoClassDefFoundError: nu/xom/Serializer
at org.example.Main.main(Main.java:35)
Caused by: java.lang.ClassNotFoundException: nu.xom.Serializer
at java.net.URLClassLoader.findClass(URLClassLoader.java:387)
at java.lang.ClassLoader.loadClass(ClassLoader.java:419)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:352)
at java.lang.ClassLoader.loadClass(ClassLoader.java:352)
... 1 more
Output I want:
{
"product": {
"a001": "045293373",
"a002": "03",
"productidentifier": [
{
"b221": "02",
"b244": "0545791324"
},
{
"b221": "03",
"b244": "9780545791328"
},
{
"b221": "13",
"b244": "2016014983"
},
{
"b221": "14",
"b244": "09780545791328"
},
{
"b221": "15",
"b244": "9780545791328"
}
],
"descriptivedetail": {
"x314": "00",
"b012": "BB",
"b333": [
"B304",
"B401",
"B501"
],
"measure": [
{
"x315": "01",
"c094": "10.60",
"c095": "in"
},
{
"x315": "02",
"c094": "8.80",
"c095": "in"
},
{
"x315": "03",
"c094": "1.40",
"c095": "in"
},
{
"x315": "08",
"c094": "3.5000",
"c095": "lb"
}
],
"x316": "US",
"productclassification": {
"b274": "01",
"b275": "4901.99.0070"
},
"collection": {
"x329": "20",
"x330": "INGRAM",
"titledetail": {
"b202": "01",
"titleelement": [
{
"x409": "02",
"b203": "Harry Potter"
},
{
"x409": "01",
"x410": "2"
}
]
}
},
"titledetail": {
"b202": "01",
"titleelement": {
"x409": "01",
"x501": "",
"b031": "Harry Potter and the Chamber of Secrets: The Illustrated Edition (Harry Potter, Book 2)",
"b029": "Volume 2"
}
},
"contributor": [
{
"b034": "1",
"b035": "A01",
"b039": "J K",
"b040": "Rowling",
"contributordate": {
"x417": "50",
"j260": "05",
"b306": "1965"
},
"b044": "<p>J.K. ROWLING is the author of the enduringly popular, era-defining Harry Potter seven-book series, which have sold over 600 million copies in 85 languages, been listened to as audiobooks for over one billion hours and made into eight smash hit movies. To accompany the series, she wrote three short companion volumes for charity, including <i>Fantastic Beasts and Where to Find Them</i>, which went on to inspire a new series of films featuring Magizoologist Newt Scamander. Harry's story as a grown-up was continued in a stage play, <i>Harry Potter and the Cursed Child</i>, which J.K. Rowling wrote with playwright Jack Thorne and director John Tiffany.</p></p><p></p><p>In 2020, she returned to publishing for younger children with the fairy tale <i>The Ickabog</i>, the royalties for which she donated to her charitable trust, Volant, to help charities working to alleviate the social effects of the Covid 19 pandemic. Her latest children's novel, <i>The Christmas Pig</i>, was published in 2021.</p></p><p></p><p>J.K. Rowling has received many awards and honours for her writing, including for her detective series written under the name Robert Galbraith. She supports a wide number of humanitarian causes through Volant, and is the founder of the international children's care reform charity Lumos. J.K. Rowling lives in Scotland with her family.</p></p>",
"contributorplace": {
"x418": "04",
"b251": "GB"
}
},
{
"b034": "2",
"b035": "A12",
"b039": "Jim",
"b040": "Kay",
"b044": "Jim Kay won the Kate Greenaway Medal in 2012 for his illustrations in <i>A Monster Calls</i> by Patrick Ness. Jim studied illustration at the University of Westminster and since graduating has worked in the archives of Tate Britain and the Royal Botanic Gardens at Kew. Jim has produced concept work for television and contributed to a group exhibition at the Victoria and Albert Museum in London. He now lives and works in Northamptonshire, England, with his wife."
}
],
"n386": ""
},
"collateraldetail": {
"textcontent": [
{
"x426": "02",
"x427": "00",
"d104": "Originally published in a slightly different form in the United Kingdom by Bloomsbury in 1998."
},
{
"x426": "03",
"x427": "00",
"d104": {
"_textformat": "02",
"__cdata": "Award-winning artist Jim Kay illustrates year two of Harry Potter's adventures at Hogwarts, in a stunning, gift-ready format.<p></p>The Dursleys were so mean and hideous that summer that all Harry Potter wanted was to get back to the Hogwarts School for Witchcraft and Wizardry. But just as he's packing his bags, Harry receives a warning from a strange, impish creature named Dobby who says that if Harry Potter returns to Hogwarts, disaster will strike.And strike it does. For in Harry's second year at Hogwarts, fresh torments and horrors arise, including an outrageously stuck-up new professor, Gilderoy Lockhart, a spirit named Moaning Myrtle who haunts the girls' bathroom, and the unwanted attentions of Ron Weasley's younger sister, Ginny.But each of these seem minor annoyances when the real trouble begins, and someone -- or something -- starts turning Hogwarts students to stone. Could it be Draco Malfoy, a more poisonous rival than ever? Could it possibly be Hagrid, whose mysterious past is finally told? Or could it be the one everyone at Hogwarts most suspects... Harry Potter himself?"
}
}
]
}
}
}
Can somebody please suggest other method or give any new advice what should I try next?
Thanks in advance
2
Answers
If you are open to try out third party libs you may try Underscore, which has has a static method
U.xmlToJson(xml)
and some other overloads.Maven dependency:
Example using Underscore
You may need to tweak it to specify how for example CDATA elements should be displayed
In all my experiments, I have never found an XML-to-JSON converter (or vice versa) that gives you exactly what you want, except in toy examples. In real life, you nearly always have to preprocess the input or postprocess the output. Personally, I prefer to do the whole job by hand. It can usually be done in a few lines of XSLT 3.0 code, and that way you get exactly the output you want.