jqueryでxmlを解析する際に問題があります。私は自分でいくつかの調査を行いましたが、私の問題に対する答えが見つかりませんでした。問題は、parseXml 関数が id 結果の div に結果を表示せず、div 情報に「成功」が表示されないことです。qanda.xml を qandaa.xml のような存在しないファイル名に変更すると、情報の div に「XML ファイルが見つかりません」と表示されるため、ファイルは読み込まれていると思いますが、parseXml 関数に問題があります。
XML (qanda.xml)
<?xml version="1.0" encoding="UTF-8"?>
<QandA>
<question>how much?</question>
<answer>100</answer>
</QandA>
<QandA>
<question>how much?</question>
<answer>110</answer>
</QandA>
<QandA>
<question>how much?</question>
<answer>120</answer>
</QandA>
<QandA>
<question>how much?</question>
<answer>130</answer>
</QandA>
<QandA>
<question>how much?</question>
<answer>140</answer>
</QandA>
htmlページ
<!DOCTYPE HTML>
<html>
<head>
<LINK REL=StyleSheet HREF="layout.css" TYPE="text/css" MEDIA=screen>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript">
$(document).ready( function() {
var information = $("#info");
var result = $("#result");
$.ajax({
type: "GET",
url: "qanda.xml",
datatype: "xml",
success: parseXml,
error: function(xhr, status, error) {
if(xhr.status==404) {
information.text("XML file not found");
}
}
});
function parseXml(xml) {
$(xml).find('QandA').each(function(){
result.append($(this).find('question').text());
result.append($(this).find('answer').text());
});
information.text("Success");
}
});
</script>
</head>
<body>
<div id="info"></div>
<div id="result"></div>
</body>
</html>