我正在尝试解析此XML文件以获取名称。例如,“2b管理非营利组织”。
我尝试过使用DOM和xpath但是我失败了。
我想循环浏览这个xml并提取每个歌曲名称。这是我的xpath示例。
XPath xPath = XPathFactory.newInstance().newXPath();
NodeList nList = (NodeList)xPath.evaluate("/plist/dict/dict/dict/string[@rollno='artist']", root, XPathConstants.NODESET);
for (int i = 0; i < nList.getLength(); ++i) {
Element e = (Element) nList.item(i);
String value = e.getFirstChild().getNodeValue();
// System.out.print("string:" + value+"");
names_and_numbers.add( value);
System.out.print(names_and_numbers);
}
由于
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict rollno="393">
<key>Major Version</key><integer>1</integer>
<key>Minor Version</key><integer>1</integer>
<key>Application Version</key><string>8.2.1</string>
<key>Features</key><integer>5</integer>
<key>Show Content Ratings</key><true/>
<key>Music Folder</key><string>file://localhost/C:/WINNT/Profiles/A062616/My%20Documents/My%20Music/iTunes/iTunes%20Music/</string>
<key>Library Persistent ID</key><string>542CEAB7C8BAE946</string>
<key>Tracks</key>
<dict>
<key>124</key>
<dict>
<key>Track ID</key><integer>124</integer>
<key>Name</key><string>2b Managing the Non-Profit Organization</string>
<key>Artist</key><string>Peter Drucker</string>
<key>Album</key><string>Managing the Non-Profit Organization</string>
<key>Genre</key><string>AudioBook</string>
<key>Kind</key><string>MPEG audio file</string>
<key>Size</key><integer>15096591</integer>
<key>Total Time</key><integer>2680320</integer>
<key>Date Modified</key><date>2008-12-17T18:37:02Z</date>
<key>Date Added</key><date>2008-12-17T17:36:38Z</date>
<key>Bit Rate</key><integer>45</integer>
<key>Sample Rate</key><integer>22050</integer>
<key>Persistent ID</key><string>21B422A222B952AD</string>
<key>Track Type</key><string>File</string>
<key>Location</key><string>file://localhost/C:/WINNT/Profiles/A062616/My%20Documents/My%20Music/iTunes/iTunes%20Music/Peter%20Drucker/Managing%20the%20Non-Profit%20Organization/2b%20Managing%20the%20Non-Profit%20Organizat.mp3</string>
<key>File Folder Count</key><integer>4</integer>
<key>Library Folder Count</key><integer>1</integer>
</dict>
<key>126</key>
<dict>
<key>Track ID</key><integer>126</integer>
<key>Name</key><string>1a Managing the Non-Profit Organization</string>
<key>Artist</key><string>Peter Drucker</string>
<key>Album</key><string>Managing the Non-Profit Organization</string>
<key>Genre</key><string>AudioBook</string>
<key>Kind</key><string>MPEG audio file</string>
<key>Size</key><integer>16393005</integer>
<key>Total Time</key><integer>2683402</integer>
<key>Date Modified</key><date>2008-12-17T18:37:02Z</date>
<key>Date Added</key><date>2008-12-17T17:36:38Z</date>
<key>Bit Rate</key><integer>48</integer>
<key>Sample Rate</key><integer>22050</integer>
<key>Persistent ID</key><string>21B422A222B952AF</string>
<key>Track Type</key><string>File</string>
<key>Location</key><string>file://localhost/C:/WINNT/Profiles/A062616/My%20Documents/My%20Music/iTunes/iTunes%20Music/Peter%20Drucker/Managing%20the%20Non-Profit%20Organization/1a%20Managing%20the%20Non-Profit%20Organizat.mp3</string>
<key>File Folder Count</key><integer>4</integer>
<key>Library Folder Count</key><integer>1</integer>
</dict>
<key>128</key>
<dict>
<key>Track ID</key><integer>128</integer>
<key>Name</key><string>1b Managing the Non-Profit Organization</string>
<key>Artist</key><string>Peter Drucker</string>
<key>Album</key><string>Managing the Non-Profit Organization</string>
<key>Genre</key><string>AudioBook</string>
<key>Kind</key><string>MPEG audio file</string>
<key>Size</key><integer>16412357</integer>
<key>Total Time</key><integer>2672195</integer>
<key>Date Modified</key><date>2008-12-17T18:37:02Z</date>
<key>Date Added</key><date>2008-12-17T17:36:38Z</date>
<key>Bit Rate</key><integer>49</integer>
<key>Sample Rate</key><integer>22050</integer>
<key>Persistent ID</key><string>21B422A222B952B0</string>
<key>Track Type</key><string>File</string>
<key>Location</key><string>file://localhost/C:/WINNT/Profiles/A062616/My%20Documents/My%20Music/iTunes/iTunes%20Music/Peter%20Drucker/Managing%20the%20Non-Profit%20Organization/1b%20Managing%20the%20Non-Profit%20Organizat.mp3</string>
<key>File Folder Count</key><integer>4</integer>
<key>Library Folder Count</key><integer>1</integer>
</dict>
</dict>
</dict>
</plist>
答案 0 :(得分:1)
请尝试使用此XPath表达式:
/plist/dict/dict/dict/key[text()='Name']/following::string[1]
这将选择具有文本名称
的键后面的第一个字符串public static void main(String[] args) {
try {
InputSource root = new InputSource(XMLParse.class.getResourceAsStream("/data.xml"));
XPath xPath = XPathFactory.newInstance().newXPath();
NodeList nList = (NodeList)xPath.evaluate("/plist/dict/dict/dict/key[text()='Name']/following::string[1]", root, XPathConstants.NODESET);
for (int i = 0; i < nList.getLength(); ++i) {
Element e = (Element) nList.item(i);
String value = e.getFirstChild().getNodeValue();
System.out.println("string:" + value+"");
}
} catch (XPathExpressionException e) {
e.printStackTrace();
} catch (DOMException e) {
e.printStackTrace();
}
}