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

最新更新