javascript - 使用 var 将 xml 标签数据保存到 xml 文件中

标签 javascript jquery

假设我的 xml 文件

sample.xml

<?xml version="1.0" encoding="utf-8"?>
 <api>
    <category>
       <id>1</id>
       <title>title-1</title>
       <mode />
    </category> 
    <category>
       <id>2</id>
       <title>title-2</title>
       <mode />
    </category>
    <category> 
       <id>3</id>
       <title>title-3</title>
       <!--store mode tag in xml-->
         <mode>
            <title>title-mod-1</title>        
            <id>mod-id</id>
            <description>my own description</description>
         </mode>

       <!--end of mode tag-->
    </category>
 </api>

我的Ajax调用方法

 $.ajax({
   type: "GET",
   url: "sample.xml",
   contentType: "text/xml",
   dataType: "xml",
   data: "",
   crossDomain:true,
   success: function(xml) { 

     $(xml).find('category').each(function(){

        var id=$(this).find('id').text();                                
        var title=$(this).find('title').text();
                    alert(id + ""+title);                   //good

                    var my_mode= $(this).find('mode').text();  
                    alert(my_mode); 
                     /* using this alert output some like that
                     title-mod-1
                     mod-id
                     my own description
                     */     

                    /* but i require my output something like that
                   </mode>
                     <title>title-mod-1</title>        
                     <id>mod-id</id>
                     <description>my own description</description>
                   </mode>

                   above output is store in xml file(modified.xml)  in one variable and 
                   able to use this xml file 
                      */
                   /*for those <mode /> have not any thing then nothing will be save*/                          
        });
              });

最佳答案

试试这个。

$.ajax({
   type: "GET",
   url: "sample.xml",
   contentType: "text/xml",
   dataType: "xml",
   data: "",
   crossDomain:true,
   success: function(xml) { 

     $(xml).find('category').each(function(){

        var id=$(this).find('id').text();                                
        var title=$(this).find('title').text();
                    alert(id + ""+title);                   //good

                    var my_mode= $(this).find('mode').html(); //try this
                  // var my_mode= $(this).find('mode').parent().html(); //or try this
                    alert(my_mode); 
                     /* using this alert output some like that
                     title-mod-1
                     mod-id
                     my own description
                     */     

                    /* but i require my output something like that
                   </mode>
                     <title>title-mod-1</title>        
                     <id>mod-id</id>
                     <description>my own description</description>
                   </mode>

                   above output is store in xml file(modified.xml)  in one variable and 
                   able to use this xml file 
                      */
                   /*for those <mode /> have not any thing then nothing will be save*/                          
        });
              });

Demo link

关于javascript - 使用 var 将 xml 标签数据保存到 xml 文件中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18509024/

相关文章:

javascript - 使用 perl 登录 https 站点

javascript - image.src 到大图像在加载时卡住

javascript - 使用 JavaScript、jQuery 或 CSS 更改下拉菜单选项的颜色

javascript - 在 View @model 列表中使用 javascript 变量

javascript - 当我从开头删除元素时,为什么 m.render() 不更新 View ?

javascript - ajax,在ajax成功时缩短每个div的高度

javascript - 临时文档中的 getElementById 不起作用

javascript - (Javascript- HTML Parser) 没有服务器语言可以用javascript解析html吗?

javascript - 为什么 Bootstrap Collapse 扩展后不断删除 "collapse"类?

javascript - 使数据表总计行不受排序/过滤的影响