2013-07-23 3 views
0

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 

:

그리고 여기 내 출력됩니다. 어떤 아이디어?

+0

iTunes 파일의 스 니펫을 올리십시오. J Dilla 앨범에 해당 파트를 포함시켜야합니다. 또한 실제 디버거에서 실행하고 단계별로 인쇄하여 왜 두 번 인쇄되는지 정확하게 볼 수 있습니다. – Huy

+0

Huytard가 말했듯이 디버거를 사용하십시오. 무슨 일이 일어나고 있는지를 찾는 가장 빠른 방법입니다. –

+0

@Huytard 내 게시물에 연결했는데, 아마도 내 C++과 출력 사이에 놓친 것일까 요? 나는 QtCreator를 사용하지만 여전히 디버거를 사용하는 방법을 알 수 없다. 당신이 좋은 튜토리얼을 안다면, 나는 그것을 가져 간다. 이것이 C++의 첫 걸음입니다. 배울 점이 많습니다! – ryancey

답변

0

내 디버거에서 코드를 실행 한 후에는 아이들을 너무 많이 반복하는 것으로 보입니다. 의미, 당신은 재귀 <DICT>, 내부 <DICT>, <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(); 
    } 
    } 
} 
+0

그것은 마치 전체 XML 라이브러리 에서조차 매력처럼 작동합니다. 이전 질문에 답하기 위해 Qt 5.0 64 비트 OSX 6.8 (Snow Leopard)를 사용 중입니다. 링크 주셔서 감사합니다, 나는 그것을 체크 아웃합니다. – ryancey

+0

그럼 GDB를 찾아서 QtCreator에 연결하는 것이 아주 쉽습니다. http://stackoverflow.com/questions/4720591/how-do-i-make-qt-creators-debugger-show-the-contents-of-c-vectors-in-os-x- 스크린 샷과 모두! – Huy