1

I'm trying to parse this XML file to get name. For example, "2b Managing the Non-Profit Organization".

I've tried to use DOM and xpath but I failed.

I'd like to loop through this xml and extract each song name. Here is my xpath example.

 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);

        }

Thanks

<?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
  • This is not XML. XML always has parent node which is missing Commented Dec 4, 2015 at 16:06

1 Answer 1

1

Try this XPath expression instead:

/plist/dict/dict/dict/key[text()='Name']/following::string[1]

This will select the first string following a key with text Name

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();
 }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.