0

I am calling an external API which returns me XML response but I want to convert it to json so that the frontend can use it.

I tried to set the request header to application/json but seems like the API returns only XML.

Vinaya Nayak
  • 1,083
  • 3
  • 16
  • 29
  • There's this thing called 'AJAX' which stands for 'Asynchronous Javascript and XML' which allows a front end to use XML APIs... – Pete Kirkham Sep 28 '18 at 10:15
  • @PeteKirkham, AJAX still requires frontend to understand XML if it wants to use it. Pretty sure there's no magic code that would convert xml to json in anything related to AJAX on javascript site. – M. Prokhorov Sep 28 '18 at 10:16
  • If your API supports only XML, then you will need to convert it yourself. First thing to do is to set the rules of how the conversion is supposed to happen (XML includes *a lot* of metadata, and JSON doesn't - you have to decide which of the metadata you are dropping, and which you're retaining in JSON output). As it stands though, the question is way too broad - this conversion is case per case, and you aren't showing us the cases you work with. Consider adding more details to your question if you need better answers. – M. Prokhorov Sep 28 '18 at 10:20
  • @M.Prokhorov my point being that the assumption being made in the OP that 1/ converting the XML to JSON will make it any easier for the front end to work with it or 2/ the front end has to work with JSON are both false. – Pete Kirkham Sep 28 '18 at 10:34
  • @PeteKirkham (2) is indeed false, however, (1) is true if frontend was specifically written to only work with json responses. We have no way of knowing directly whether it was or wasn't written that way, but we have to take that as true considering OP thinks that converting to JSON will make the response usable (implication being that XML isn't usable). – M. Prokhorov Sep 28 '18 at 10:38

3 Answers3

0

Similar kind of question is answered here - convert-xml-to-json
Try

import org.json.XML;

and then use

JSONObject jsonObject = XML.toJSONObject("<XMLStringValue>");
Haripriya
  • 822
  • 1
  • 14
  • 27
0

I've open sourced a library called unXml, that lets you create a parser that consumes xml, and produces Jackson json ObjectNodes or ArrayNodes.

It's available here on Maven Central.

Example:

Input xml:

<root>
  <id>1</id>
  <title>mytitle</title>
</root>

Creating the parser in Java:

import com.nerdforge.unxml.Parsing;
import com.nerdforge.unxml.factory.ParsingFactory;
...

public class MyController {
    public ObjectNode getJsonFromXml(String inputXmlString) {
        Parsing parsing = ParsingFactory.getInstance().create();
        Document document = parsing.xml().document(inputXmlString);

        Parser<ObjectNode> parser = parsing.obj("root")
            .attribute("id", "id", parsing.number())
            .attribute("title")
            .build();

        ObjectNode node = parser.apply(document);
        return node;
    }
}

Gives the following json-result:

{
  "id":1,
  "title":"mytitle"
}
tomaj
  • 1,570
  • 1
  • 18
  • 32
-1

Try this IT worked for me,

  header('Content-Type: application/xml');

  $response=simplexml_load_string($response);
Ajinkya Bodade
  • 491
  • 1
  • 5
  • 11