无法使用 javascript 和 jquery 获取 XML 属性值
Can not get XML attribute value using javascript and jquery
XML:
<?xml version="1.0" encoding="utf-8"?>
<Publications LatestPubDate="2012-12-20" Version="0">
<PubYear Year="2012">
<PubMonth Month="12">
<Publication Name="Headline" PubDay="15" ThumbnailWidth="300" ThumbnailHeight="400" ThumbnailPath="FlippingBook/Dev/Frontend/OutPutFolder/Headline/2012/12/15/0/0/A/Content/1/Pg001.jpg" Version="0" Visible="true"/>
<Publication Name="Headline" PubDay="16" ThumbnailWidth="300" ThumbnailHeight="400" ThumbnailPath="FlippingBook/Dev/Frontend/OutPutFolder/Headline/2012/12/16/0/0/A/Content/1/Pg001.jpg" Version="0" Visible="true"/>
<Publication Name="Headline" PubDay="17" ThumbnailWidth="300" ThumbnailHeight="400" ThumbnailPath="FlippingBook/Dev/Frontend/OutPutFolder/Headline/2012/12/17/0/0/A/Content/1/Pg001.jpg" Version="0" Visible="true"/>
<Publication Name="Headline" PubDay="18" ThumbnailWidth="300" ThumbnailHeight="400" ThumbnailPath="FlippingBook/Dev/Frontend/OutPutFolder/Headline/2012/12/18/0/0/A/Content/1/Pg001.jpg" Version="0" Visible="true"/>
<Publication Name="Headline" PubDay="19" ThumbnailWidth="300" ThumbnailHeight="400" ThumbnailPath="FlippingBook/Dev/Frontend/OutPutFolder/Headline/2012/12/19/0/0/A/Content/1/Pg001.jpg" Version="0" Visible="true"/>
<Publication Name="Headline" PubDay="20" ThumbnailWidth="300" ThumbnailHeight="400" ThumbnailPath="FlippingBook/Dev/Frontend/OutPutFolder/Headline/2012/12/20/0/0/A/Content/1/Pg001.jpg" Version="0" Visible="true"/>
</PubMonth>
</PubYear>
</Publications>
.js:
function get_past_issues(year,month) {
$.ajax({ url: './demo/Headline/PublicationList.xml',
async: false,
success: function(xml) {
//$("#dialog").append("<div class = 'issues'>");
$(xml).find("Publications").find($("PubYear[Year='" + year + "']")).each(function() {
//alert ($(this).attr ('Year'));
$(xml).find("Publications").find($("PubMonth[Month='" + month + "']")).find("Publication").each(function() {
alert ($(this).attr ('ThumbnailPath'));
});
});
//$("#dialog").append("</div>");
}
});
例如,我提供的函数年份是 2012 年,月份是 12,但是,fliter $("PubYear[Year='" + year + "']") 似乎不适用于查找函数?如何解决问题?谢谢
使用 fliter find("PubYear[Year='" + year + "']")
function get_past_issues(year,month) {
$.ajax({ url: './demo/Headline/PublicationList.xml',
async: false,
success: function(xml) {
//$("#dialog").append("<div class = 'issues'>");
$(xml).find("Publications").find("PubYear[Year='" + year + "']").each(function() {
//alert ($(this).attr ('Year'));
$(xml).find("Publications").find("PubMonth[Month='" + month + "']").find("Publication").each(function() {
alert ($(this).attr ('ThumbnailPath'));
});
});
//$("#dialog").append("</div>");
}
});
}
在这里我以这种方式使用它:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
<script>
function get_past_issues(year, month){
$.ajax({
url: 'list.xml',
type:"get",
async: false,
success: function(xml) {
$(xml).find("PubYear[Year='" + year + "']").find("PubMonth[Month='" + month + "']").each(function(){
console.log($(this).find('Publication').attr('ThumbnailPath'));
// Output is:FlippingBook/Dev/Frontend/OutPutFolder/Headline/2012/12/15/0/0/A/Content/1/Pg001.jpg
});
},
error:function(){
alert('err');
}
});
}
$(function(){
get_past_issues(2012, 12);
});
</script>
相关文章:
- 在控制器中按ID获取XML ViewElement(MultiInput)
- 如何获取XML值,然后使用javascript将它们输出到htm文件中
- 使用 AJAX 获取 XML,然后将其解析为 JSON(无需服务器)
- 如何在JQUERY中从HTML文档中获取XML对象
- 获取xml节点文本
- XML到JSON代理服务器未获取XML提要
- 无法获取XML中的节点值,如何获取
- 在 javascript 中获取 xml 标记名称
- 在javascript中获取xml dom子元素
- 如何在WCF服务中获取Xml作为字符串并使用JavaScript发送
- 无法使用 jquery 获取 xml 属性的值
- 无法使用 jquery 获取 xml 元素文本值
- Angularjs - 使用 $http 获取 xml 响应而不是 json
- 如何使用 JS AJAX 获取 XML 标记的名称
- 获取 XML 节点并将其存储在变量中
- 使用 JavaScript 获取 XML 数据
- 如何获取 xml 控件属性
- 如何在IE8中获取XML节点的文本值
- 如何使用 Jquery 获取 XML 文件,并在每次加载时以随机顺序显示元素一次
- 如何在 JavaScript 中获取 XML 命名空间