简体   繁体   中英

angularjs load data from xml file and store in a xml object

I am reading data from a XML file and want to edit, using angularjs. The problem is , $http call is reading data but converting it to string, whether I am expecting it to be object. My XML data is:

<?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>

Using below code in 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 ');                                         
        });

It prints data in console and typeof shows string. Please help to understand. Will I have to convert back to XML for performing node-element operations?

We have to put transformResponse in $http request as below. it parse data back to 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 ');                                         
        });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM