3

Given that I have this XML:

<?xml version="1.0" encoding="UTF-8"?>
<Root>
    <op>update</op>
    <path>someString</path>
    <value>12345</value>
</Root>

and I want this output in JSON:

[{ "op":"update", "path":"someString", "value":"12345" }]

I have tried the following code:

package jsonconvertor;
import net.sf.json.JSON;
import net.sf.json.xml.XMLSerializer;

public class JSONConvertor {

    public static void main(String[] args) {

        String input = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><Root><op>update</op><path>someString</path><value>12345</value></Root>";
        String output = "";

        XMLSerializer xml = new XMLSerializer();
        JSON jObj = xml.read( input );
        output = jObj.toString();

        System.out.println("My JSON:\n" + output);
    }
}

When I run that code I get the following response:

{"op":"update","path":"someString","value":"12345"}

However you will notice that the leading '[' and closing ']' are missing.

When I try changing the XML "input" string in the code to the following:

String input = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><Root class=\"object\"><op>update</op><path>someString</path><value>12345</value></Root>";

which makes sense since itRoot was an object to start with, so I tried changing the "input" string to "array":

String input = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><Root class=\"array\"><op>update</op><path>someString</path><value>12345</value></Root>";

however then I get the following:

["update","someString","12345"]

What am I missing? I want the output I get when class="object" however I want it enclosed in square brackets. I want the output to look like the example at the top of the post.

3
  • 1
    Try with this : String input2 = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><Root class=\"array\"><SubRoot class=\"object\"><op>update</op><path>someString</path><value>12345</value></SubRoot></Root>"; Commented Nov 2, 2018 at 6:38
  • 1
    I can't thank you enough! .. This is the solution to my problem! I will certainly accept this as the solution I was looking for if you add it again as an answer! Commented Nov 2, 2018 at 6:44
  • Yes I've added as answer you can accept it . Happy to help! :) Commented Nov 2, 2018 at 6:45

2 Answers 2

1

Try with this input String it should work :

String input = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><Root class=\"array\"><SubRoot class=\"object\"><op>update</op><path>someString</path><value>12345</value></SubRoot></Root>";
Sign up to request clarification or add additional context in comments.

Comments

0

In the given XML, you have only 1 ROOT element , so it's a single JSONObject and not a JSONArray. So, if you want a JSONArray as Output, you should provide XML Array as input to the xml.read( input ) method.

If your input looks like below, you will get your desired output.

<?xml version="1.0" encoding="UTF-8"?>
<Root>
    <SubRoot>
         <op>update</op>
         <path>someString</path>
         <value>12345</value>
    <SubRoot>
</Root> 

Output :

[{"op":"update","path":"someString","value":"12345"}]

4 Comments

Thank you, however when adding String input = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><Root><SubRoot><op>update</op><path>someString</path><value>12345</value></SubRoot></Root>"; I get the following result: {"SubRoot":["update","someString","12345"]}
OMG. But i am getting the output i pasted here which looks same as you. I think the difference is because of the dependency jar version's.Let me check again
@Deepak Gunasekaran try to add class="array" to Root.
Okay @Samabcde . Thanks :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.