「first.xml」を解析する必要があり、この解析関数内で、「first.xml」のすべての要素に対して「second.xml」を解析する必要があります。「first.xml」の解析が完了するまで、jQuery は「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 ファイルの複数の解析を許可していないのでしょうか? もしそうなら、最初のファイルの解析を中断し、2 番目のファイルの解析を終了し、最初のファイルの解析を再開するオプションはありますか?
奇妙な詳細: JetBrains Webstorm を使用してコードをデバッグすると、ステップごとの出力は正しくなります。通常実行した場合のみ、「first.xml」の後に「second.xml」の出力が追加されます。
ご協力いただきありがとうございます。