0

I try retrieving data from an api which is in xml format. I've source for few examples calling json file but couldn't get it done calling an api.

Here is my code. it shows nothing on the browser and It does not show any error in the console log

angular.module('myApp', [])
.controller('foodListController', function($scope, $http) {
  $http.get('mydomain.com/api/FoodList/GetAllList').then(function (response) {
      $scope.myData = response.data.dtoFood;
  });
});


<ul>
  <li ng-repeat="x in myData">
    {{ x.FoodName + ', ' + x.IsActive }}
  </li>
</ul>

    <ArrayOfdtoFood>
        <dtoFood>
            <CreatedBy i:type="d3p1:string">1</CreatedBy>
        <CreatedOn>2016-02-01T17:22:31.645Z</CreatedOn>
        <IsActive>true</IsActive>
        <IsChecked i:nil="true"/>
        <IsDelete i:nil="true"/>
        <ModifiedOn>2016-02-20T08:06:31.905Z</ModifiedOn>
        <Modifiedby i:type="d3p1:string">1</Modifiedby>
        <FoodName>Example1</FoodName>

    </dtoFood>

    <dtoFood>
    <CreatedBy i:type="d3p1:string">1</CreatedBy>                            <CreatedOn>2016-02-01T17:22:31.645Z</CreatedOn>
    <IsActive>true</IsActive>
    <IsChecked i:nil="true"/>
    <IsDelete i:nil="true"/>
    <ModifiedOn>2016-02-20T08:06:31.905Z</ModifiedOn>
    <Modifiedby i:type="d3p1:string">1</Modifiedby>
    <FoodName>Example Food 2</FoodName>
    </dtoFood>
</ArrayOfdtoFood>
8
  • You need something like x2js. Commented Mar 14, 2016 at 12:36
  • I'm not trying to convert an xml to json but want to retrieve from the xml api. Commented Mar 14, 2016 at 12:45
  • If you want to handle it you want it to be JSON since JSON is the language objects are stored in Javascript, hence you need something like x2js Commented Mar 14, 2016 at 12:46
  • okay but it does not display in either xml or json in the browser. i think i need to fix that aspect first before knowing if i need to convert it Commented Mar 14, 2016 at 12:49
  • So what is the problem? You can fetch XML just the way you do with JSON. But then you will to access data somehow (I would suggest DOM). Commented Mar 14, 2016 at 12:49

1 Answer 1

1

You could do that this way using x2js:

  var x2js = new X2JS();  
  $scope.list = x2js.xml_str2json("<data><obj><item>1</item></obj><obj><item>2</item></obj></data>");
  console.log($scope.list);

see the fiddle.

This will work, of course if the service is giving a response and is valid XML.

Are you loading data from an absolute url...?

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.