Join the Stack Overflow Community
Stack Overflow is a community of 6.7 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

Is there a way to convert XML url to JSON url so that I can use it in my website ?

{"breakfast_menu": {
"food": [
  {
    "name": "Belgian Waffles",
    "price": "$5.95",
    "description": "Two of our famous Belgian Waffles with plenty of real maple syrup",
    "calories": "650"
  },
  {
    "name": "Strawberry Belgian Waffles",
    "price": "$7.95",
    "description": "Light Belgian waffles covered with strawberries and whipped cream",
    "calories": "900"
  },
  {
    "name": "Berry-Berry Belgian Waffles",
    "price": "$8.95",
    "description": "Light Belgian waffles covered with an assortment of fresh berries and whipped cream",
    "calories": "900"
  },
  {
    "name": "French Toast",
    "price": "$4.50",
    "description": "Thick slices made from our homemade sourdough bread",
    "calories": "600"
  },
  {
    "name": "Homestyle Breakfast",
    "price": "$6.95",
    "description": "Two eggs, bacon or sausage, toast, and our ever-popular hash browns",
    "calories": "950"
  }
]}}

Suppose the URL is given as http://www.w3schools.com/xml/simple.xml then how can I convert that to JSON so that I can use it in AngularJS

share|improve this question
up vote 1 down vote accepted

You may use xml2json.js to convert an XML to JSON

Be sure to read the accompanying article on the xml.com, which goes into details of interesting problems with these conversions.

Usage:

  $scope.xml = '<breakfast_menu>'+
                '<food>'+
                   '<name>Belgian Waffles</name>'+
                   '<price>$5.95</price>'+
                '</food>'+
              '</breakfast_menu>';

  var dom = parseXml($scope.xml);
  $scope.json = xml2json(dom,"  ");

use {{json}} to display the json in your view!

{ "breakfast_menu":{"food":{ "name":"Belgian Waffles", "price":"$5.95" }} }

Here's a working plunkr

share|improve this answer
    
In $scope.xml can I access url instead of data – Ramana Uday Jun 17 '15 at 6:38
    
ofcourse, you'd simply need to use $http service and get the xml – NLN Jun 17 '15 at 6:41
    
Sorry for the previous comment here is plunker Plunker – Ramana Uday Jun 17 '15 at 6:58
    
@RamanaUday: Hi..I checked your plunker..and found following issues. - First, see the console for this error No 'Access-Control-Allow-Origin' header is present on the requested resource. It is a CORS issue. Secondly..you are trying to do $scope.xml = $http.get('...'). $http.get service returns a promise and not the response data itself. So you cannot assign it to a variable straightaway. – NLN Jun 17 '15 at 7:18
    
Here's the updated plunkr. I have changed to url from w3schools.com/xml/simple.xml to cdn.rawgit.com/motyar/bcf1d2b36e8777fd77d6/raw/… to make it work. Regarding the CORS issue..see this. Hope it helps :) – NLN Jun 17 '15 at 7:27

@SFDC:Hi... This xml link('http://www.webservicex.com/globalweather.asmx/GetCitiesByCountry') is possible to convert from xml to json format

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.