2011-04-20 3 views
0

Mac OS X에서 Qt 4.7을 사용하고 있으며 XML 파일 경로가 포함 된 QString을 가지고 있습니다. 그 파일을 DOM 트리로 가져 와서 데이터를 클래스에 멤버 변수로 저장하고 싶습니다. 이 작업을 수행하는 가장 좋은 방법은 무엇입니까?Qt에서 XML을 DOM 트리로 가져 오기

QtXml 설명서를 보았지만 QXml* 클래스를 QDom* 클래스로 변환하는 명확한 방법을 찾을 수 없습니다.

답변

2

DOM을 탐색하기 위해 QXml * 클래스를 신경 쓰지 않아도된다고 생각합니다.

QDomDocument 클래스에는 열린 QFile을 사용할 수있는 setContent() 메서드가 있습니다.

There's a code sample QDomDocument 설명서의 "자세히"절을 참조하십시오.

QDomDocument doc("mydocument"); 
QFile file("mydocument.xml"); 
if (!file.open(QIODevice::ReadOnly)) 
    return; 
if (!doc.setContent(&file)) { 
    file.close(); 
    return; 
} 
file.close(); 

// print out the element names of all elements that are direct children 
// of the outermost element. 
QDomElement docElem = doc.documentElement(); 

QDomNode n = docElem.firstChild(); 
while(!n.isNull()) { 
    QDomElement e = n.toElement(); // try to convert the node to an element. 
    if(!e.isNull()) { 
     cout << qPrintable(e.tagName()) << endl; // the node really is an element. 
    } 
    n = n.nextSibling(); 
} 
관련 문제