如何解析java中的XML文件

如何解析java中的XML文件,java,xml,parsing,xpath,Java,Xml,Parsing,Xpath,我正试图解析这个XML文件以获取名称。例如,“管理非营利组织” 我尝试过使用DOM和xpath,但失败了 我想循环使用这个xml并提取每首歌曲的名称。下面是我的xpath示例 XPath xPath = XPathFactory.newInstance().newXPath(); NodeList nList = (NodeList)xPath.evaluate("/plist/dict/dict/dict/string[@rollno='artist']", root, XP

我正试图解析这个XML文件以获取名称。例如,“管理非营利组织”

我尝试过使用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);

        }
XPath=XPathFactory.newInstance().newXPath();
NodeList nList=(NodeList)xPath.evaluate(“/plist/dict/dict/dict/string[@rollno='artist']”,根,XPathConstants.NODESET);
对于(int i=0;i
谢谢

<?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>

主要版本1
小版本1
应用程序版本8.2.1
特点5
显示内容分级
音乐Folderfile://localhost/C:/WINNT/Profiles/A062616/My%20Documents/My%20Music/iTunes/iTunes%20Music/
库持久性ID542CEAB7C8BAE946
轨道
124
轨道ID124
管理非营利组织
艺术家德鲁克
管理非营利组织
GenreAudioBook
一种mpeg音频文件
尺寸15096591
总时间2680320
修改日期2008-12-17T18:37:02Z
添加日期2008-12-17T17:36:38Z
比特率45
抽样率22050
持久性ID21B42A222B952AD
轨道类型文件
Locationfile://localhost/C:/WINNT/Profiles/A062616/My%20Documents/My%20Music/iTunes/iTunes%20Music/Peter%20Drucker/Managing%20the%20Non-利润%20组织/2b%20管理%20非盈利%20组织。mp3
文件夹计数4
库文件夹计数1
126
轨道ID126
名称1管理非营利组织
艺术家德鲁克
管理非营利组织
GenreAudioBook
一种mpeg音频文件
尺寸16393005
总时间2683402
修改日期2008-12-17T18:37:02Z
添加日期2008-12-17T17:36:38Z
比特率48
抽样率22050
持久性ID21B42A222B952AF
轨道类型文件
Locationfile://localhost/C:/WINNT/Profiles/A062616/My%20Documents/My%20Music/iTunes/iTunes%20Music/Peter%20Drucker/Managing%20the%20Non-利润%20组织/1a%20管理%20非盈利%20组织。mp3
文件夹计数4
库文件夹计数1
128
磁道ID128
管理非营利组织
艺术家德鲁克
管理非营利组织
GenreAudioBook
一种mpeg音频文件
尺寸16412357
总时间2672195
修改日期2008-12-17T18:37:02Z
添加日期2008-12-17T17:36:38Z
比特率49
抽样率22050
持久性ID21B42A222B952B0
轨道类型文件
Locationfile://localhost/C:/WINNT/Profiles/A062616/My%20Documents/My%20Music/iTunes/iTunes%20Music/Peter%20Drucker/Managing%20the%20Non-利润%20组织/1b%20管理%20非盈利%20组织。mp3
文件夹计数4
库文件夹计数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();
 }
}
publicstaticvoidmain(字符串[]args){
试一试{
InputSource root=newInputSource(XMLParse.class.getResourceAsStream(“/data.xml”);
XPath=XPathFactory.newInstance().newXPath();
NodeList nList=(NodeList)xPath.evaluate(“/plist/dict/dict/dict/key[text()='Name']/following::string[1]”,根,XPathConstants.NODESET);
对于(int i=0;i
这不是XML。XML始终具有缺少的父节点