XML 라이브러리 (iTunes 디렉토리의 iTunes Music Library.xml)를 구문 분석하여 iTunes 앨범 목록을 가져 오려고합니다.QtXML DOM 구문 분석/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;
}
아이튠즈 'XML 정말 STANDART XML되지는 앨범의 이름은 각 항목에 대한 <key>Album</key>
옆에있는 노드에 저장된다. Here is what it looks like. intentionnaly 디버깅 목적으로 일부 노드의 이름을 변경했습니다 (출력 결과에 도달했는지 확인하기 위해). 루프가 첫 번째 노드를 파싱 왜 내가 볼 수없는
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
:
그리고 여기 내 출력됩니다. 어떤 아이디어?
iTunes 파일의 스 니펫을 올리십시오. J Dilla 앨범에 해당 파트를 포함시켜야합니다. 또한 실제 디버거에서 실행하고 단계별로 인쇄하여 왜 두 번 인쇄되는지 정확하게 볼 수 있습니다. – Huy
Huytard가 말했듯이 디버거를 사용하십시오. 무슨 일이 일어나고 있는지를 찾는 가장 빠른 방법입니다. –
@Huytard 내 게시물에 연결했는데, 아마도 내 C++과 출력 사이에 놓친 것일까 요? 나는 QtCreator를 사용하지만 여전히 디버거를 사용하는 방법을 알 수 없다. 당신이 좋은 튜토리얼을 안다면, 나는 그것을 가져 간다. 이것이 C++의 첫 걸음입니다. 배울 점이 많습니다! – ryancey