QtXML DOM parsing / iTunes Library
QtXML DOM parsing / iTunes Library
我正试图通过解析XML库(iTunes目录中的iTunes Music library.XML)来获得iTunes专辑的列表。
#include <iostream>
#include <QtCore>
#include <QFile>
#include <QtXml>
using namespace std;
void parse(QDomNode n) {
while(!n.isNull()) {
// If the node has children
if(n.hasChildNodes() && !n.isNull()) {
// We get the children
QDomNodeList nChildren = n.childNodes();
// We print the current tag name
//std::cout << "[~] Current tag : <" << qPrintable(n.toElement().tagName()) << ">" << std::endl;
// And for each sub-tag of the current tag
for(int i = 0; i < nChildren.count(); i++) {
// We get the children node
QDomNode nChild = nChildren.at(i);
// And the tag value (we're looking for *Album* here)
QString tagValue = nChild.toElement().text();
// If the tag isn't null and contain *Album*
if(!nChild.isNull() && tagValue == "Album") {
// The album name is in the next tag
QDomElement albumNode = nChild.nextSiblingElement();
std::cout << "[-] Album found -> " << qPrintable(albumNode.text()) << std::endl;
}
// And we parse the children node
parse(nChild);
}
}
n = n.nextSibling();
}
}
int main() {
QDomDocument doc("Lib");
QFile file("/Users/wizardman/QtRFIDMusic/Lib.min.xml");
if(!file.open(QIODevice::ReadOnly))
return 1;
if(!doc.setContent(&file)) {
file.close();
return 1;
}
file.close();
// Root element
QDomElement docElem = doc.documentElement();
// <plist> -> <dict>
QDomNode n = docElem.firstChild().firstChild();
cout << endl << "Album list" << endl;
cout << "------------------------------------" << endl;
parse(n);
return 0;
}
iTunes的XML并不是真正的标准XML,专辑的名称存储在每个条目的<key>Album</key>
旁边的节点中。这是它的样子。出于调试目的,我有意重命名了一些节点(看看我是否能在输出中找到它们)。
这是我的输出:
Album list
------------------------------------
[-] Album found -> J Dilla - Legacy Vol.1
[-] Album found -> J Dilla - Legacy Vol.2
[-] Album found -> J Dilla - Legacy Vol.1
[-] Album found -> J Dilla - Legacy Vol.2
[-] Album found -> J Dilla - Legacy Vol.2
[-] Album found -> J Dilla - Legacy Vol.2
我不明白循环为什么要重新准备第一个节点。有什么想法吗?
在我的调试器下运行代码后。。。看来你在孩子们身上迭代的次数太多了。也就是说,你在<dict>,内部<dict>lt;dict_FOCUS>并且<dict_FOCUS2>。
对我来说,使用QDomNode::firstChildElement(QString)在节点之间迭代(无递归)更容易;我不能保证这是防弹的。。但这只是一个开始!)
// Root element
QDomElement docElem = doc.documentElement();
// <plist> -> <dict>
QDomNode n = docElem.firstChildElement().firstChildElement("dict");
qDebug() << "Album list";
qDebug() << "------------------------------------";
QDomNodeList list = n.childNodes();
int count = list.count();
for(int i = 0; i < count; ++i)
{
QDomElement node = list.at(i).toElement();
if(node.tagName().startsWith("dict_FOCUS"))
{
node = node.firstChildElement();
while(!node.isNull())
{
if(node.text() == "Album" && node.tagName() == "key")
{
node = node.nextSiblingElement();
if(!node.isNull() && node.tagName() == "string")
{
qDebug() << "[-] Album found -> " << qPrintable(node.text());
}
}
node = node.nextSiblingElement();
}
}
}
相关文章:
- Boost Graph Library,修复节点大小
- SFML library: http request
- 解决"ld: library not found for -ltensorflow_framework.2.3.0"
- Linking library C++
- 在VS2019项目中集成ImageMagick:x64-windows-static library
- OpenSSL fips in C++ wrapper Library 如何?错误:指纹不匹配
- C++ class vs a library
- C++ library with Tensorflow on Android
- CMake:尝试在 Jenkins 构建机器上运行时出现"Linked Library"错误 (0xc0000135)
- 将 c++ cmake-library 构建发布到本地存储库时出现 Gradle 错误
- 为什么'allocate in one library and free in the other'是错误的
- 链接 library.lib 使用 setup.py.
- 目标提升::<library>已经有导入的位置 + 链接错误
- Visual Studio 2017 C++,如何定义一个"环境变量"'Additional Library Directory'?
- C++ Boost SML Library
- Windows 套接字和身份验证构建在包含 Poco-Library 时失败
- Qt小部件应用程序"Library not registered"错误
- Xcode/IOS: linking a CMake library
- 如何修复'The procedure entry point SDL_RWclose could not be located in the dynamic link library'
- QtXML DOM parsing / iTunes Library