我有一个大的xml文件,我想要解析并存储为对象。目前我只是试图解析所有信息,然后我会尝试将其存储在对象中。到目前为止,我只能解析xml文件中的字符串,但我希望能够解析整数,我不知道如何在同一个循环中执行此操作,因为我将它们存储为对象。
以下是我的代码:
class ListX[A](list: List[A]) {
def zipMap[B, C](that: List[B])(op: (A, B) => C): List[C] =
list.zip(that).map({
case (a, b) => op(a, b)
})
}
println(new ListX[Int](List(2, 2, 2)).zipMap(List(1, 1, 1))(_ + _))
我的xml文件:
FileInputStream file = new FileInputStream(new File("itunes2.xml"));
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse(file);
Element root = document.getDocumentElement();
System.out.println(root.getNodeName());
System.out.println("============================");
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("Name: " + value);
}
My Song.java:
<plist version="1.0">
<dict>
<key>Major Version</key><integer>1</integer>
<key>Minor Version</key><integer>1</integer>
<key>Application Version</key><string>10.1</string>
<key>Features</key><integer>5</integer>
<key>Show Content Ratings</key><true/>
<key>Music Folder</key><string>file://localhost//Thomts/Holly/data/My%20Music/iTunes/iTunes%20Music/</string>
<key>Library Persistent ID</key><string>29E12A03204E0721</string>
<key>Tracks</key>
<dict>
<key>324</key>
<dict>
<key>Track ID</key><integer>324</integer>
<key>Name</key><string>Talk Dirty To Me</string>
<key>Artist</key><string>Poison</string>
<key>Composer</key><string>Poison</string>
<key>Album</key><string>The Best Of Poison: 20 Years Of Rock</string>
<key>Genre</key><string>Metal</string>
<key>Kind</key><string>AAC audio file</string>
<key>Size</key><integer>3698203</integer>
<key>Total Time</key><integer>224866</integer>
<key>Disc Number</key><integer>1</integer>
<key>Disc Count</key><integer>1</integer>
<key>Track Number</key><integer>1</integer>
<key>Track Count</key><integer>18</integer>
<key>Year</key><integer>1986</integer>
<key>Date Modified</key><date>2008-08-07T15:16:57Z</date>
<key>Date Added</key><date>2008-08-07T15:17:17Z</date>
<key>Bit Rate</key><integer>128</integer>
<key>Sample Rate</key><integer>44100</integer>
<key>Play Count</key><integer>6</integer>
<key>Play Date</key><integer>3378486192</integer>
<key>Play Date UTC</key><date>2011-01-22T03:23:12Z</date>
<key>Compilation</key><true/>
<key>Sort Album</key><string>Best Of Poison: 20 Years Of Rock</string>
<key>Persistent ID</key><string>29E12A03204E0873</string>
<key>Track Type</key><string>File</string>
<key>Location</key><string>file://localhost//Thomts/Holly/data/My%20Music/iTunes/iTunes%20Music/Compilations/The%20Best%20Of%20Poison_%2020%20Years%20Of%20Rock/01%20Talk%20Dirty%20To%20Me.m4a</string>
<key>File Folder Count</key><integer>4</integer>
<key>Library Folder Count</key><integer>1</integer>
</dict>
<key>326</key>
<dict>
<key>Track ID</key><integer>326</integer>
<key>Name</key><string>I Want Action [Single Version]</string>
<key>Artist</key><string>Poison</string>
<key>Composer</key><string>Poison</string>
<key>Album</key><string>The Best Of Poison: 20 Years Of Rock</string>
<key>Genre</key><string>Metal</string>
<key>Kind</key><string>AAC audio file</string>
<key>Size</key><integer>3070259</integer>
<key>Total Time</key><integer>185893</integer>
<key>Disc Number</key><integer>1</integer>
<key>Disc Count</key><integer>1</integer>
<key>Track Number</key><integer>2</integer>
<key>Track Count</key><integer>18</integer>
<key>Year</key><integer>1986</integer>
<key>Date Modified</key><date>2008-08-07T15:17:12Z</date>
<key>Date Added</key><date>2008-08-07T15:17:32Z</date>
<key>Bit Rate</key><integer>128</integer>
<key>Sample Rate</key><integer>44100</integer>
<key>Play Count</key><integer>3</integer>
<key>Play Date</key><integer>3367665479</integer>
<key>Play Date UTC</key><date>2010-09-18T21:37:59Z</date>
<key>Compilation</key><true/>
<key>Sort Album</key><string>Best Of Poison: 20 Years Of Rock</string>
<key>Persistent ID</key><string>29E12A03204E087A</string>
<key>Track Type</key><string>File</string>
<key>Location</key><string>file://localhost//Thomts/Holly/data/My%20Music/iTunes/iTunes%20Music/Compilations/The%20Best%20Of%20Poison_%2020%20Years%20Of%20Rock/02%20I%20Want%20Action%20%5BSingle%20Version%5D.m4a</string>
<key>File Folder Count</key><integer>4</integer>
<key>Library Folder Count</key><integer>1</integer>
</dict>
</dict>
</dict>
我正在使用的表达式(/ plist / dict / dict / dict / key [text()='Name'] / following :: string [1])有效,但我还想使用另一个整数的表达式,然后将这些全部存储在同一循环中的对象中。我试过但还没有成功。有人可以帮我这个吗?
答案 0 :(得分:1)
我们的想法是选择包含填充每个Song
对象所需的所有信息的元素,即最内层的<dict>
元素。遍历<dict>
,并在每次迭代中使用相对XPath表达式来获取详细信息,例如:
.....
.....
NodeList nList = (NodeList)xPath.evaluate("/plist/dict/dict/dict", root, XPathConstants.NODESET);
for (int i = 0; i < nList.getLength(); ++i) {
Element dict = (Element)nList.item(i);
//get current TRACK ID and print
Double trackId = (Double)xPath.evaluate("key[.='Track ID']/following-sibling::integer[1]", dict, XPathConstants.NUMBER);
System.out.println("Track ID: " + trackId.toString());
//get current NAME and print
String name = (String)xPath.evaluate("key[.='Name']/following-sibling::string[1]", dict, XPathConstants.STRING);
System.out.println("Name: " + name);
//get further information and process as needed
.....
.....
}