xml - Tricky xpath query -


i'm new xpath , xml parsing in general. read documentations couldn't find solution problem. .xml looks (fyi: it's playlist part of "itunes music library.xml"):

    <dict>       <array>         <dict>           <key>name</key><string>playlist 1</string>           <key>playlist id</key><integer>187826</integer>           <key>playlist persistent id</key><string>04250e0617138909</string>           <key>all items</key><true/>           <key>playlist items</key>           <array>             <dict>               <key>track id</key><integer>11111</integer>             </dict>             <dict>               <key>track id</key><integer>22222</integer>             </dict>           </array         </dict>         <dict>           <key>name</key><string>playlist 2</string>           <key>playlist id</key><integer>187826</integer>           <key>playlist persistent id</key><string>04250e0617138909</string>           <key>all items</key><true/>           <key>playlist items</key>           <array>             <dict>               <key>track id</key><integer>23456</integer>             </dict>             <dict>               <key>track id</key><integer>34567</integer>             </dict>           </array         </dict>       </dict> 

my input string containing name of playlist. here want output: songids related specific playlist. unfortunately name not attribute of example array or first dict. here's i've got (java-code):

ap.selectxpath("//dict/array/dict/string[1][text()=\"" + playlistname + "\"]"); 

that gives me playlistname of course. how proceed on? came mind: parse file until parser reaches desired name:

ap.selectxpath("//dict/array/dict[" + count++ +"]/string[1]"); 

with "int count". start again position appropriate query. again, doesn't seem elegant or efficient.

long story short: there xpath query fulfill requirements?

the way dict element works in plist format alternating pairs of elements, each value being first following sibling element of key. given key can access value as

key[. = 'entry name']/following-sibling::*[1] 

so particular example you're interested in, dict element "playlist 1" is

/dict/array/dict[key[. = 'name']/following-sibling::*[1] = 'playlist 1'] 

its track listing array is

/dict/array/dict[key[. = 'name']/following-sibling::*[1] = 'playlist 1']   /key[. = 'playlist items']/following-sibling::*[1] 

and set of integer elements giving track ids be

/dict/array/dict[key[. = 'name']/following-sibling::*[1] = 'playlist 1']   /key[. = 'playlist items']/following-sibling::*[1]   /dict/key[. = 'track id']/following-sibling::*[1] 

be careful if you're getting playlist names user input , might contain single or double quote characters. don't library you're using, if supports defining variables should use mechanism inject values rather relying on string concatenation build expressions.


Comments

Popular posts from this blog

c++ - Creating new partition disk winapi -

Android Prevent Bluetooth Pairing Dialog -

php - joomla get content in onBeforeCompileHead function -