• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Converting Json to XML Generated invalid XML

 
Ranch Hand
Posts: 507
Netbeans IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello all,

First, please note I made this question http://stackoverflow.com/questions/21827492/converting-json-to-xml-generated-invalid-xml";" target="_blank" rel="nofollow">here as well, but no satisfying answer so I came here.

I am trying to convert a JSON file into XML. My Json file is a Big Json Array. Below code shows how I am trying to make this happen.


This is how the Json files look like




This code generated invalid XML. When tested in XML Validators, they show the errors. What is wrong here? Please help.


**UPDATE**

I managed to generate the following output



But XML Validator in here says:



I have been struggling with this for days , still no good and no one is able to help too. Any idea please?
 
Ranch Hand
Posts: 426
Eclipse IDE Fedora Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not that this is illegal, but I prefer not to have whitespace in your tag : < type> </ type>.

You might also import your XML into XML Notepad (free download from Microsoft) which could help you figure out what changes you need to make to your code to get the right output.
 
Yohan Weerasinghe
Ranch Hand
Posts: 507
Netbeans IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Roger Sterling wrote:Not that this is illegal, but I prefer not to have whitespace in your tag : < type> </ type>.

You might also import your XML into XML Notepad (free download from Microsoft) which could help you figure out what changes you need to make to your code to get the right output.



Thanks for the reply. I have used built in ways of doing the job. Do you know any other proven way of making this happen?
 
Roger Sterling
Ranch Hand
Posts: 426
Eclipse IDE Fedora Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can normalize the tags to exclude or replace whitespace with an underscore.
 
Yohan Weerasinghe
Ranch Hand
Posts: 507
Netbeans IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Roger Sterling wrote:You can normalize the tags to exclude or replace whitespace with an underscore.


The problem here is there are too many root level elements in the XML. Not whitespaces.
 
Roger Sterling
Ranch Hand
Posts: 426
Eclipse IDE Fedora Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Yohan Weerasinghe wrote:

Roger Sterling wrote:You can normalize the tags to exclude or replace whitespace with an underscore.


The problem here is there are too many root level elements in the XML. Not whitespaces.



Then add a parent root XML. You can have many children in the parent.
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Yohan Weerasinghe wrote:The problem here is there are too many root level elements in the XML.


I would phrase it slightly differently: There aren't too many root elements, there is no root element. JSON has no concept of a root element, so the XML class can't provide one. As the javadocs of the XML.toString method says, it creates an XML string, not an XML document. You have to provide the rest.
 
Yohan Weerasinghe
Ranch Hand
Posts: 507
Netbeans IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ulf Dittmer wrote:

Yohan Weerasinghe wrote:The problem here is there are too many root level elements in the XML.


I would phrase it slightly differently: There aren't too many root elements, there is no root element. JSON has no concept of a root element, so the XML class can't provide one. As the javadocs of the XML.toString method says, it creates an XML string, not an XML document. You have to provide the rest.



:O :O :O How can I do that? There is also another issue. I have only one root element in Json, that is an array But this XML conversation has generated multiple arrays!!
 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Yohan Weerasinghe wrote:How can I do that?


Do you know XML? If you look at what gets created, what is still missing? If you are unsure, then I think you should read up on what well-formatted XML is.

I have only one root element in Json, that is an array But this XML conversation has generated multiple arrays!!


As I said before, there is no concept of a root element in JSON. JSON is different than XML - there is no one-to-one mapping between the two. (And no, there are not multiple arrays - there is a list of elements, each one of which is surrounded by a tag named "array".) I've never used this class before, but it seems to be the most basic of JSON-to-XML converters. If you want more, you'll have to find a more capable converter, or write one yourself.
 
reply
    Bookmark Topic Watch Topic
  • New Topic