2017-03-31 47 views
0

我有下面的XML:與NSXMLDocument Objective-C的使用XPath

<?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太複雜了。

任何人都可以告訴我我在做什麼錯誤的查詢?

回答

1

「我想使用此代碼來獲取所有<song>節點(是正確的術語?)包含某個稱號。」

@用於引用XML屬性。由於title元素,對於你的情況正確的XPath是:

//root/song[child::title='1-2-3'] 

,自child是XPath的默認軸,可以暗示:

//root/song[title='1-2-3']