我需要解析一个“first.xml”,在这个解析函数中,我必须为“first.xml”中的每个元素解析一个“second.xml”。看起来 jQuery 在解析“first.xml”之前不会解析“second.xml”。
我的 HTML 文件有一个 div,其中附加了内容:
<div id="Content"></div>
整个代码嵌入在 $(document).ready(function({});
function parseXML(xmlFilePath, callback){
    $.ajax({
        type: "GET",
        url: xmlFilePath,
        dataType: "xml",
        success: function(xml) {
            callback(xml);
        }
    });
}
parseXML("first.xml", function(returnedXML){
    $(returnedXML).find("ElementA").each(function(counter) {
        name_of_Attribute_of_Element_A = $(this).attr("name");
        $("#Content").append(counter  + ". " + name_of_Attribute_of_Element_A);
        $("#Content").append("------ before parsing second.xml ------");
        parseXML("second.xml", function(returnedXML){
            $(returnedXML).find("ElementB").each(function() {
                name_of_Attribute_of_Element_B = $(this).attr("name");
                $("#Content").append(name_of_Attribute_of_Element_B);
            });
        });
    });
});
我的输出看起来像这样:
1. name_of_Attribute_of_Element_A
------ before parsing second.xml ------
2. name_of_Attribute_of_Element_A
----- before parsing second.xml ------
3. name_of_Attribute_of_Element_A
----- before parsing second.xml ------
name_of_Attribute_of_Element_B
name_of_Attribute_of_Element_B
name_of_Attribute_of_Element_B
代替:
1. name_of_Attribute_of_Element_A
------ before parsing second.xml ------
name_of_Attribute_of_Element_B
2. name_of_Attribute_of_Element_A
----- before parsing second.xml ------
name_of_Attribute_of_Element_B
3. name_of_Attribute_of_Element_A
----- before parsing second.xml ------    
name_of_Attribute_of_Element_B
我还尝试连接我的输出,最后只附加一次。这意味着而不是
$("#Content").append(result);
我用过
myHTMLOutput = myHTMLOutput + result;
在我使用的文件的末尾
$("#Content").append(myHTMLOutput);
一次,但结果相同。
是不是 jQquery 不允许使用 Ajax 多次解析 XML 文件?如果是这样,是否可以选择中断第一个文件的解析,完成第二个文件的解析并恢复第一个文件的解析?
奇怪的细节:如果我使用 JetBrains Webstorm 调试我的代码,则逐步输出是正确的。只有当我正常运行它时,才会在“first.xml”之后附加“second.xml”的输出。
提前感谢您提供的任何帮助。