为什么在PhoneGap应用程序中没有正确加载XML文件?

时间:2011-08-16 10:36:27

标签: javascript jquery xml cordova

我正在开发基于PhoneGap的iPhone应用程序,我的文件夹结构如下所示。

www/
www/js/
www/css
www/data/

“/ data”文件夹有一个存储facutlies的XML文件,结构是......

<?xml version="1.0" encoding="UTF-8"?>
<Faculties>
   <Faculty Name="" Room="" Phone="" Fax="" Email="" Address="">
        <Department Name="">
            <Room>
            </Room>
            <Phone>
            </Phone>
            <Fax>
            </Fax>
            <Email>                                          
            </Email>
            <Address>
            </Address>
        </Department>    
    </Faculty>
    <Faculty Name="" Room="" Phone="" Fax="" Email="" Address="">
        <Department Name="">
            <Room>
            </Room>
            <Phone>
            </Phone>
            <Fax>
            </Fax>
            <Email>                                          
            </Email>
            <Address>
            </Address>
        </Department>    
   </Faculty>
   <Faculty Name="" Room="" Phone="" Fax="" Email="" Address="">
        <Department Name="">
            <Room>
            </Room>
            <Phone>
            </Phone>
            <Fax>
            </Fax>
            <Email>                                          
            </Email>
            <Address>
            </Address>
        </Department>    
    </Faculty>
</Faculties>

我正在使用jQuery和AJAX阅读此XML。它可以查询虽然它总是检索2个院系而不是3个,因为有3个不是2个。

function populateFaculties(target, after) {
    var file = "data/FacultiesAndDepartments.xml";

    $(target).find("option:gt(0)").remove().end();

    $.ajax({
        url: file,
        type: 'GET',
        dataType: 'xml',
        success: function(result) {
            var faculties = $(result).find("Faculties Faculty");

            console.log("There are " + faculties.length + " faculties in the XML.");

            if (faculties.length < 1) {
                $(target).attr("disabled", "disabled");
                after(null);
                return null;
            }

            var done = 0;

            $(faculties).each(function(index, faculty) {
                done++;
                $(target).append('<option value="' + (index + 1) + '">' + $(faculty).attr("Name") + '</option>');
            });

            var timer = setInterval(function() {
                if (faculties.length == done) {
                    timer = window.clearInterval(timer);
                    $(target).removeAttr("disabled");
                    after(faculties);
                }
            }, 500);
        }
    });
}

传递“target”,它是“select”元素,“after”是委托函数。

1 个答案:

答案 0 :(得分:0)

我自己想出了这个问题。 XML中的&符号是保留的,必须转换为“&amp; amp;”。