简体   繁体   English

找不到导入的xml文件的节点

[英]Can't find node of imported xml file

After importing an XML file I am trying to get the length of the "sheet" nodes but they are returned as 0 when there should be 3 of them. 导入XML文件后,我尝试获取“工作表”节点的长度,但是当应该有3个时,它们将返回0。 Any help with getting this to work would be appreciated. 任何帮助使它起作用的方法,将不胜感激。 Thanks. 谢谢。

 $(document).ready(function ()
        {
            $.ajax({
                type: "GET", url: "menu5.xml", dataType: "xml",
                success: function (xml2)
                {
                    var xml = xml2,
                            xmlDoc = $.parseXML(xml),
                            $xml = $(xmlDoc);

                    alert($xml.find('sheet').length);

                    $xml.find('sheet').each(function ()
                    {
                        alert("here2");
                        var sheet = $(this);
                        var menuName = $(sheet).attr("name");
                        alert(menuName);
                    });
                }
            });

Here is the XML: 这是XML:

<?xml version="1.0" encoding="UTF-8"?>
<root>
<sheet id="1" name="Submenu1">
    <row id="1_1">
        <col1>CODE1a</col1>
        <col2>Option NAME 1</col2>
    </row>
    <row id="1_2">
        <col1>CODE1b</col1>
        <col2>Option NAME 2</col2>
    </row>
</sheet>
<sheet id="2" name="Submenu2">
    <row id="2_1">
        <col1>CODE2a</col1>
        <col2>Option NAME 1</col2>
    </row>
    <row id="2_2">
        <col1>CODE2b</col1>
        <col2>Option NAME 2</col2>
    </row>
</sheet>
<sheet id="3"  name="Submenu3">
    <row id="3_1">
        <col1>CODE3a</col1>
        <col2>Option NAME 1</col2>
    </row>
    <row id="3_2">
        <col1>CODE3b</col1>
        <col2>Option NAME 2</col2>
    </row>
</sheet>
</root>

As DataType is set to xml, the xml2 variable is already parsed. 由于DataType设置为xml,因此已经解析了xml2变量。 Thus, remove the xmlDoc step. 因此,删除xmlDoc步骤。 the following code works : 以下代码有效:

$.ajax({
            type: "GET", url: "menu5.xml", dataType: "xml",
            success: function (xml2)
            {
                var xml = xml2,
                        /*xmlDoc = $.parseXML(xml),*/
                        $xml = $(xml);

                alert($xml.find('sheet').length);

                $xml.find('sheet').each(function ()
                {
                    alert("here2");
                    var sheet = $(this);
                    var menuName = $(sheet).attr("name");
                    alert(menuName);
                });
            }
        });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM