如何使用Angular向下钻取XML文件

时间:2015-01-04 22:37:24

标签: xml angularjs

我需要显示medline的健康主题。我正在使用xml2json来翻译它,但我无法弄清楚如何显示我需要的东西。我收到一条未定义的消息,但我不确定为什么?我创建了一个plunker,我附上了我需要显示的照片。基本上它只是标题和链接(如果可用)。

plunker

Error: topics is not defined

 app.factory('medlineFactory',function($http){
      var factory = [];

      factory.getHealthTopics = function(){
        return $http.get("healthTopics.xml");
      }

        return factory;
    });

    app.controller('healthTopics',function($scope,medlineFactory){

      $scope.healthTopics = [];
      loadHealthTopics();

      function loadHealthTopics(){
        medlineFactory.getHealthTopics().success(function(data){
            allTopics  = x2js.xml_str2json(data);
            console.log(allTopics.health-topics.health-topic);
            $scope.healthTopics =allTopics.health-topics.health-topic;
        });
        }
    });

display

1 个答案:

答案 0 :(得分:1)

我建议你在这里使用 x2js 更多信息https://code.google.com/p/x2js/

请在此处查看工作演示:http://plnkr.co/edit/pob3c28vkC3qR1hXfCv0?p=preview

var app = angular.module('plunker', []);

app.controller('MainCtrl', function($scope, $http) {
  $scope.xml = '';
  $scope.dom = {};


  $http.get('sample.xml').then(function(response) {

    $scope.xml = response.data
    var x2js = new X2JS();
    $scope.dom = x2js.xml_str2json($scope.xml);
    $scope.topics = $scope.dom['health-topics']


  })


});