简体   繁体   English

无法使用javascript和jquery获取XML属性值

[英]Can not get XML attribute value using javascript and jquery

XML: 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: 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>");    
        }
    });

For instance I have provided the function year is 2012 and month is 12, however, the fliter $("PubYear[Year='" + year + "']") seems do not work with the find function? 例如我提供的函数年份是2012年,月份是12,但是,fliter $(“PubYear [Year ='”+ year +“']”)似乎不能与find函数一起使用? How to fix the problem ? 如何解决问题? thanks 谢谢

Use fliter find("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>");    
        }
    });
}

Here i used it this way: 在这里我用这种方式:

<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>

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

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