XML解析 - 尝试访问第二个for循环中的xml元素的XPath表达式

时间:2015-12-06 19:01:02

标签: java xml parsing xpath xml-parsing

我有一个XML文件,我试图解析并存储为对象,以便我可以保留它们。

<key>Playlists</key>
<array>
    <dict>
        <key>Name</key><string>Library</string>
        <key>Master</key><true/>
        <key>Playlist ID</key><integer>1806</integer>
        <key>Playlist Persistent ID</key><string>29E12A03204E0722</string>
        <key>Visible</key><false/>
        <key>All Items</key><true/>
        <key>Playlist Items</key>
        <array>
            <dict>
                <key>Track ID</key><integer>1234</integer>
            </dict>
            <dict>
                <key>Track ID</key><integer>1282</integer>
            </dict>
            <dict>
                <key>Track ID</key><integer>1694</integer>
            </dict>
            <dict>
                <key>Track ID</key><integer>1558</integer>
            </dict>
       </array>
    </dict>
    <dict>
        <key>Name</key><string>Music</string>
        <key>Playlist ID</key><integer>2687</integer>
        <key>Playlist Persistent ID</key><string>29E12A03204E072C</string>
        <key>Distinguished Kind</key><integer>4</integer>
        <key>Music</key><true/>
        <key>All Items</key><true/>
        <key>Playlist Items</key>
        <array>
            <dict>
                <key>Track ID</key><integer>1234</integer>
            </dict>
            <dict>
                <key>Track ID</key><integer>1282</integer>
            </dict>
            <dict>
                <key>Track ID</key><integer>1694</integer>
            </dict>
            <dict>
                <key>Track ID</key><integer>1558</integer>
            </dict>
       </array>
    </dict>

我无法访问第二个阵列。它会在每个播放列表的第一个数组中显示曲目ID。

我正在尝试显示的所有播放列表都这样做。

有人可以帮我吗?

1 个答案:

答案 0 :(得分:0)

更改

NodeList tList = (NodeList)x.evaluate("/plist/dict/array/dict/array/dict", root, XPathConstants.NODESET);

NodeList tList = (NodeList)x.evaluate("array/dict", dict, XPathConstants.NODESET);