简体   繁体   English

jquery:合并 XML 文档

[英]jquery: merge XML documents

is it possible to merge multiple xmlDocuments into one single file (var newResult) by using jquery (or pure javascript)?是否可以使用 jquery(或纯 JavaScript)将多个 xmlDocuments 合并到一个文件(var newResult )中?

unfortunately i've to combine different hosted xml-documents into one single file (don't ask me why...).不幸的是,我必须将不同的托管 xml 文档合并到一个文件中(不要问我为什么......)。 i've played around with different techniques, but i can't find a solution for this (maybe jquery isn't ready for xml-handling??).我玩过不同的技术,但我找不到解决方案(也许 jquery 还没有准备好进行 xml 处理??)。 thank you:)谢谢你:)

see my piece of code:看我的一段代码:

var files = ['john.xml','doe.xml']
var newResult, temp;

function loadMyXMLFiles(){

    // > let's load file by file using jquery's ajax-call:

    $.each(files, function(index,value) { 

      $.ajax({

          url: value,
          cache: true,
          success: function(data){

            // > check if temp is empty, append or fill

            (temp) ? temp += (data) : temp = data;


            // > on complete:

            if (index == files.length-1) {

                xmlResult = temp; temp = null;
            }
          }

      });
  });
}

loadMyXMLFiles()

在此处输入图像描述

As I can see, you are trying to concatenate them.如我所见,您正在尝试将它们连接起来。 If this is what you need, your code wasn't that bad,如果这是您需要的,那么您的代码还不错,

var files = ['john.xml','doe.xml']
var xmlResult, temp = ""; //Make temp an empty string

loadMyXMLFiles(){
    $.each(files, function(index,value) { 
      $.ajax({
          url: value,
          cache: true,
          async: false, //This is crucial!
          success: function(data){
            temp += data;
            if (index == files.length-1) {
                xmlResult = temp; temp = null;
            }
          }
      });
  });
}

You have to use async: false so your files arrive in order.您必须使用async: false以便您的文件按顺序到达。 But caution, this will hang the browser until the processing is finished.但请注意,这将挂起浏览器,直到处理完成。 If it's vital for it not to happen, just tell me.如果它不发生至关重要,请告诉我。

Hope this helps.希望这可以帮助。 Cheers干杯

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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