简体   繁体   English

JavaScript XML解析

[英]JavaScript XML parse

the xml XML

<publication_date media_type="print">
        <month>1</month>
        <year>2011</year>
</publication_date>

I have used this code to get title 我已使用此代码获取标题

var year = $xml.find('publication_date year').text();

but sometimes the xml contains two different types like this: 但有时xml包含两种不同的类型,如下所示:

<publication_date media_type="print">
        <month>05</month>
        <day>22</day>
        <year>2012</year>
</publication_date>
<publication_date media_type="online">
        <month>04</month>
        <day>26</day>
        <year>2012</year>
</publication_date>

I want to select the year witch comes with online media-type and if there is no online select print type. 我想选择女巫附带online media-type的年份,如果没有online media-type ,请选择print类型。

很简单,使用属性选择器:

$xml.find("publication_date[media_type=online] year").text()

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

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