2017-11-03 15 views
0

XMLファイルからデータを読み込み、anglejsを使用して編集したいです。問題は、$http呼び出しがデータを読み取っていますが、文字列に変換しているかどうかです。オブジェクトであることを期待していますか。私のXMLデータは次のとおりです。angularjs xmlファイルからデータをロードしてxmlオブジェクトに格納します

<?xml version="1.0" encoding="UTF-8"?> 
<catalog> 
    <book id="bk101"> 
     <author>Gambardella, Matthew</author> 
     <title>XML Developer's Guide</title> 
     <genre>Computer</genre> 
     <price>44.95</price> 
     <check1 version="1"></check1> 
     <collection name="argu.ments"/> 
     <check3></check3> 
     <check4/> 
     <acollection name="arguments"/> 
     <string name="connect_timeout"></string> 
     <publish_date>2000-10-01</publish_date> 
     <description>An in-depth look at creating applications 
     with XML.</description> 
    </book> 
    <book id="bk102"> 
     <author>Ralls, Kim</author> 
     <title>Midnight Rain</title> 
     <genre>Fantasy</genre> 
     <price>5.95</price> 
     <publish_date>2000-12-16</publish_date> 
     <description>A former architect battles corporate zombies, 
     an evil sorceress, and her own childhood to become queen 
     of the world.</description> 
    </book> 
</catalog> 

angularjsにコードの下に使用する:

$http({ 
      url: "books.xml", 
      method: "GET",   
      headers: {'Content-Type': 'application/xml','charset' : 'utf-8'} 
     }).success(function (xml) { 
        console.log(xml); 
        console.log(typeof xml); 
     }).error(function() { 
        console.log('error in /writexmlfiletest ');           
     }); 

これは、コンソール内のデータを印刷し、typeofは、文字列を示します。ご理解ください。ノード要素操作を実行するためにXMLに変換する必要がありますか?

答えて

0

$ HTTPリクエストにtransformResponseを置く必要があります。データを解析してxmlに戻します。

$http({ 
      url: "books.xml", 
      method: "GET",   
      headers: {'Content-Type': 'application/xml','charset' : 'utf-8'}, 
      transformResponse : function(data) { 
       // string -> XML document object 
       return $.parseXML(data); 
      } 
     }).success(function (xml) { 
        console.log(xml); 
        console.log(typeof xml); 
     }).error(function() { 
        console.log('error in /writexmlfiletest ');           
     }); 
関連する問題