1

私はxmlファイルを持っています:

<?xml version="1.0" encoding="ISO-8859-1"?>
<food>
    <cuisine type="Chinese">
        <restaurant name = "Panda Express">
            <location id= "0"></location>
            <phone id = "1"></phone>
            <city id="2"></phone>
        </restaurant>
        <restaurant name = "Mr. Chau's">

        </restaurant>
    </cuisine>
    <cuisine type="Indian">
        <restaurant name = "Shan">

        </restaurant>
    </cuisine>
</food>

そして私は料理ノードの数を数えようとしていますこれは私が持っているコードです、私はそれがほとんど正しいことを知っています、しかし私がノードリストの長さを印刷しようとするとそれは0であると言います

//starts talking to the xml document
        if(window.XMLHttpRequest){
            xmlhttp = new XMLHttpRequest();
        }else{
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }

        xmlhttp.open("GET","data.xml", false);
        xmlhttp.send();
        xmlData = xmlhttp.responseXML;


        //fills the first comboBox
        var sel = document.getElementById('CuisineList');
        cuisineList = xmlData.getElementsByTagName('cuisine');
        document.getElementById("test").innerHTML = cuisineList.length;
4

1 に答える 1

0

考えられる答えは 2 つあります。

A) XML にエラーが</phone>あります。必要な場所があります</city>。したがって、それを修正する必要があります。:-) (しかし、それは実際のデータではなく問題であると推測してい0ます. )

B) 応答ヘッダーを確認してください。サーバーが正しい MIME タイプの XML を返していない可能性があります。そうでない場合:

  1. 最善の答えは、XML ファイルを正しく提供するようにサーバーの構成を修正することです。

  2. 何らかの理由でそれができない場合は、 を使用して、応答を XML としてoverrideMimeType強制的に処理することができます。XMLHttpRequest

    xmlhttp.open("GET","data.xml", false);
    xmlhttp.overrideMimeType("text/xml"); // <=== The new bit
    xmlhttp.send();
    xmlData = xmlhttp.responseXML;
    

それらを修正すると、動作します:ライブ作業コピー| ソース

于 2012-07-07T07:36:48.720 に答える