我有以下XML:
<?xml version="1.0" encoding="UTF-8" ?>
<root>
<total_count>6640</total_count>
<song id="5075">
<title><![CDATA["Babysitting Blues"]]></title>
<name><![CDATA["Babysitting Blues"]]></name>
<artist id="1547"><![CDATA[Albert Collins]]></artist>
<album id="2097"><![CDATA[Adventures In Babysitting]]></album>
<albumartist id="1546"><![CDATA[Michael Kamen]]></albumartist>
<tag id="11" count="1" ><![CDATA[Pop Rock]]></tag>
<filename><![CDATA[/Volumes/data/My Documents/My Music/iTunes Media/Music/Michael Kamen/Adventures In Babysitting/09 _Babysitting Blues_.mp3]]></filename>
<track>9</track>
<time>247</time>
<year>1988</year>
<bitrate>128000</bitrate>
<rate>44100</rate>
<mode>cbr</mode>
<mime>audio/mpeg</mime>
<url><![CDATA[http://phirestalker.strangled.net:5555/play/index.php?ssid=faec0117bac5875e1aeb8b15f1826bdf&type=song&oid=5075&uid=3&player=api&name=Albert%20Collins%20-%20-Babysitting%20Blues-.m4a]]></url>
<size>4119468</size>
<mbid>4a8b4607-0f7e-4e04-8c62-a9280bb1ff1e</mbid>
<album_mbid>e80a4221-e21c-4505-af97-27fede6100f4</album_mbid>
<artist_mbid>5ea2c7d2-cd9c-41ba-a1c2-42889e9c4584</artist_mbid>
<albumartist_mbid>adcc6c98-0bf1-4d54-84aa-2249cf5e46bf</albumartist_mbid>
<art><![CDATA[http://phirestalker.strangled.net:5555/image.php?object_id=2097&object_type=album&auth=faec0117bac5875e1aeb8b15f1826bdf]]></art>
<preciserating>0</preciserating>
<rating>0</rating>
<averagerating>0</averagerating>
<composer><![CDATA[]]></composer>
<channels></channels>
<comment><![CDATA[00000000 00000210 00000C24 0000000000A6884C 02000003 003C6398 00000000 00000000 00000000 00000000 00000000 00000000]]></comment>
<publisher><![CDATA[Intrada]]></publisher>
<language>eng</language>
<replaygain_album_gain>0.000000</replaygain_album_gain>
<replaygain_album_peak>0.000000</replaygain_album_peak>
<replaygain_track_gain>-0.520000</replaygain_track_gain>
<replaygain_track_peak>0.540040</replaygain_track_peak>
<genre><![CDATA[Pop Rock]]></genre>
</song>
<song>
...
</song>
...
</root>
我正在尝试使用此代码来获取包含特定标题的所有<song>
个节点(这是正确的术语吗?)。
Return = [[NSXMLDocument alloc] initWithContentsOfURL:documentsURL options:(NSXMLDocumentTidyXML) error:&err];
if(err)
{
NSLog(@"Error parsing xml file: %@",err);
err = nil;
}
NSArray* results = [Return nodesForXPath:@"//root/song[@title='1-2-3']" error:&err];
if(err)
{
NSLog(@"Error searching songs: %@",err);
err = nil;
}
NSLog(@"we got this far\n%@",results);
if(results)
{
[results enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
NSXMLElement* node = (NSXMLElement *)obj;
NSString *artist = [[node attributeForName:@"artist"] stringValue];
NSString *album = [[node attributeForName:@"album"] stringValue];
NSLog(@"artist:%@\talbum:%@",artist,album);
}];
}
问题是整个results
数组是空的,所以我的XPath查询根本不起作用。我尝试了很多来源,试图拼凑如何进行查询。我甚至尝试过W3C的XPath 2.0规范,但那是WAAAAAAY太复杂了。
有谁能告诉我在查询中我做错了什么?
答案 0 :(得分:1)
&#34;我正在尝试使用此代码获取包含特定标题的所有
<song>
个节点(这是正确的术语?)。&#34;
@
用于引用XML属性。由于title
是元素,因此您的案例的正确XPath将是:
//root/song[child::title='1-2-3']
并且由于child
是XPath中的默认轴,因此可以暗示:
//root/song[title='1-2-3']