wood burning stoves 2.0*
The moose likes I/O and Streams and the fly likes Converting Json to XML Generated invalid XML Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "Converting Json to XML Generated invalid XML" Watch "Converting Json to XML Generated invalid XML" New topic
Author

Converting Json to XML Generated invalid XML

Yohan Weerasinghe
Ranch Hand

Joined: Oct 07, 2010
Posts: 499

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?


Are you better than me? Then please show me my mistakes..
Roger Sterling
Ranch Hand

Joined: Apr 06, 2012
Posts: 426

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

Joined: Oct 07, 2010
Posts: 499

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

Joined: Apr 06, 2012
Posts: 426

You can normalize the tags to exclude or replace whitespace with an underscore.
Yohan Weerasinghe
Ranch Hand

Joined: Oct 07, 2010
Posts: 499

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

Joined: Apr 06, 2012
Posts: 426

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
Marshal

Joined: Mar 22, 2005
Posts: 42360
    
  64
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.


Ping & DNS - my free Android networking tools app
Yohan Weerasinghe
Ranch Hand

Joined: Oct 07, 2010
Posts: 499

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
Marshal

Joined: Mar 22, 2005
Posts: 42360
    
  64
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Converting Json to XML Generated invalid XML