Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/arduino/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
解析XML文件以查找title属性_Xml_Jquery_Jquery Xml - Fatal编程技术网

解析XML文件以查找title属性

解析XML文件以查找title属性,xml,jquery,jquery-xml,Xml,Jquery,Jquery Xml,我是一个天真的jQuery程序员,所以请有人帮我解决这个问题。第一个示例显示了我正在做的事情及其工作原理。但我的困境是,列表是通过解析XML创建的。如果是这样,我该如何解析xml,找到“title”属性,然后将相应的url加载到Div中。提前感谢 jQuery代码 $('.treeLinks').click(function() { var sourceURL = $(this).attr('title'); $('#content').load(sourceURL);

我是一个天真的jQuery程序员,所以请有人帮我解决这个问题。第一个示例显示了我正在做的事情及其工作原理。但我的困境是,列表是通过解析XML创建的。如果是这样,我该如何解析xml,找到“title”属性,然后将相应的url加载到Div中。提前感谢

jQuery代码

 $('.treeLinks').click(function() {
    var sourceURL = $(this).attr('title');
    $('#content').load(sourceURL);
    });
对应的HTML代码

<ul>
<li><a href="#" title="contentArea1.html" class="treeLinks">Link 1</a></li>
<li><a href="#" title="contentArea2.html" class="treeLinks">Link 2</a></li>
</ul>
获取title属性需要解析的XML代码

<?xml version="1.0" encoding="UTF-8"?>
<root>
<item id="pxml_1">
  <content><name class="treeLinks"><![CDATA[Root node 1]]></name></content>
 <item id="pxml_2">
 <content><name class="treeLinks"><![CDATA[Child node 1a]]></name>
 <item id="pxml_23">
 <content><name><![CDATA[Child node 1a]]></name></content>
 </item>
 </content>
 </item>
 <item id="pxml_3">
 <content><name><![CDATA[Child node 2b]]></name></content>
 </item>
 <item id="pxml_4">
 <content><name><![CDATA[Child node 3c]]></name></content>
 </item>
</item>
</root>

以与html相同的方式解析XML(其实没有什么特别之处);这在Firefox中有效(不确定IE为什么不喜欢它)。另存为.html文件以获取示例

<html>
<head>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
    <script type="text/javascript">
        //Document Ready: Everything inside this function fires after the page is loaded
        $(document).ready(function () {
            var test = '<?xml version="1.0" encoding="UTF-8"?><root><item id="pxml_1"><content><name class="treeLinks"><![CDATA[Root node 1]]></name></content> <item id="pxml_2"> <content><name class="treeLinks"><![CDATA[Child node 1a]]></name> <item id="pxml_23"> <content><name><![CDATA[Child node 1a]]></name></content> </item> </content> </item> <item id="pxml_3"> <content><name><![CDATA[Child node 2b]]></name></content> </item> <item id="pxml_4"> <content><name><![CDATA[Child node 3c]]></name></content> </item></item></root>';

            //Need to wrap you xml with a root node
            test = "<wrapper>" + test + "</wrapper>";

            $(test).find('.treeLinks').each(function(){
                alert($(this).html());
            });
        });
    </script>
</head>
<body>
</body>
</html>

//文档准备就绪:加载页面后,此函数中的所有内容都会激发
$(文档).ready(函数(){
var检验='';
//需要用根节点包装xml
test=“”+test+”;
$(test).find('.treeLinks').each(function()){
警报($(this.html());
});
});
你剩下的问题不是很清楚。我无法理解您的xml是如何创建上述链接的。如果可以,请澄清XML和链接之间的关系


希望这能让你开始

谢谢…-),它工作了,但由于我使用的是外部文件,这对我没有帮助。我将href传递给xml文件中的title属性,该文件将包含链接“ContentArea1.html”。我已经完成了这个查找标题的示例,但是如何在单击事件时触发它。。下面的注释$.ajax({url:“sample.xml”,数据类型:“xml”,success:function(data){$(data).find(“item”).each(function(){alert($(this.attr)();});})@苏伦,我还是不清楚。听起来好像您正在获取一个XML文件(通过ajax)并从该XML文件生成一个链接列表。然后,当用户点击一个链接时,“内容”区域将加载该链接提供的任何内容。。。也就是说,我在XML中根本看不到任何链接(或您所引用的任何标题属性)。坦白地说,我甚至不确定它的目的是基于你上面的例子(它包含了什么/它对过程有什么贡献?)。如果你能再澄清一点,那会很有帮助的。