gpt4 book ai didi

java - java如何解析XML文件

转载 作者:行者123 更新时间:2023-11-30 07:44:42 29 4
gpt4 key购买 nike

我正在尝试解析此 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>

最佳答案

试试这个 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();
}
}

关于java - java如何解析XML文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34092201/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com