Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Converting Json to XML Generated invalid XML

 
Yohan Weerasinghe
Ranch Hand
Posts: 507
Java Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all,

First, please note I made this question 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?
 
Roger Sterling
Ranch Hand
Posts: 426
Eclipse IDE Fedora Linux
  • Mark post as helpful
  • send pies
  • 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
Java Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
You can normalize the tags to exclude or replace whitespace with an underscore.
 
Yohan Weerasinghe
Ranch Hand
Posts: 507
Java Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • 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
Java Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • 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: 42968
73
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic