2011-05-02 15 views
1

XMLファイルからデータをインポートする方法については混乱します。 次のようにXMLファイルには、構造化されています。QtでXMLデータをインポートする方法

<Workflow> 
    <ItemList1> 
     <Item1>1</Item1> 
     <otherItem>1</otherItem> 
     <anotherItem>1</anotherItem> 
      ........................ 
    </ItemList1> 
    <TaskLists> 
     <NumberOfTasks>2</NumberOfTasks> 
     <Task_1> 
      <description>"description"</description> 
      <position>"x, y"</position> 
      <name>"name"</name> 
      <tagListNumberOfItems>2</tagListNumberOfItems> 
      <tagList> 
       <subTag>"text"</subTag> 
       <other_subTag>"text"</other_subTag> 
      </tagList> 
     </Task_1> 
     <Task_2> 
      <description>"description"</description> 
      <position>"x,y"</position> 
      <name>"name"</name> 
      <tagListNumberOfItems>4</tagListNumberOfItems> 
      <tagList> 
       <different_subTag>"text"</different_subTag> 
       <other_different_subTag>"text"</other_different_subTag> 
       <a_3rd_subTag>"text"</a_3rd_subTag> 
       <a_4th_subTag>"text"</a_4th_subTag> 
      </tagList> 
     </Task_2> 
    </TaskLists> 
</Workflow> 

にはどうすればいいのデータということにインポートする必要がありますか? ありがとう!

答えて

2

QtXmlモジュールを見てください。それはあなたが読み取りまたは修正することができるツリーとしてXMLファイルをロードすることができます http://doc.qt.io/qt-5/qdomdocument.html

私はこのクラスでは何が必要でしょうと思います。

2

最も簡単なのはBookmark Exampleです。それは、docからQXmlStreamReader

を使用しています:

QXmlStreamReader xml; 
    ... 
    while (!xml.atEnd()) { 
     xml.readNext(); 
     ... // do processing 
    } 
    if (xml.hasError()) { 
     ... // do error handling 
    } 

例から:

bool XbelReader::read(QIODevice *device) 
{ 
    xml.setDevice(device); 

    if (xml.readNextStartElement()) { 
     if (xml.name() == "xbel" && xml.attributes().value("version") == "1.0") 
      readXBEL(); 
     else 
      xml.raiseError(QObject::tr("The file is not an XBEL version 1.0 file.")); 
    } 

    return !xml.error(); 
} 


void XbelReader::readXBEL() 
{ 
    Q_ASSERT(xml.isStartElement() && xml.name() == "xbel"); 

    while (xml.readNextStartElement()) { 
     if (xml.name() == "folder") 
      readFolder(0); 
     else if (xml.name() == "bookmark") 
      readBookmark(0); 
     else if (xml.name() == "separator") 
      readSeparator(0); 
     else 
      xml.skipCurrentElement(); 
    } 
} 

あなたはDOM、SAXやXmlStreamを使用することができます。見てくださいHere for a couple of examples

したがって、xmlを読み込んで、XMLファイルの内容によって/ オブジェクト/ランタイムを作成します。

0

私はあなたがXMLファイルを読むためのどこかのサンプルコードを取得できないので、この質問をしていることは知っています。次のコードは、これを行うのに役立ちます。

QString fileName = "yourfile.xml"; 
QFile file(fileName); 
if(file.exists()) { 

    QDomDocument doc("XMLFile"); 
    if(!file.open(QIODevice::ReadOnly)) 
     return false; 
    if(!doc.setContent(&file)) 
    { 
     file.close(); 
     return false; 
    } 
    file.close(); 
    QDomElement root = doc.documentElement(); 
    QDomNode n = root.firstChild(); 
    while(!n.isNull()) 
    { 
     QDomElement e = n.toElement(); 
     if(!e.isNull()) 
     { 
      qDebug() << e.tagName(); //this gives you the name of the tag 
      qDebug() << e.namedItem("ChildTag").toElement().text(); //this gives you the node value of a tag. 

     } 
     n = n.nextSibling(); 
    } 
} else { 
    return false; 
} 
return false; 
} 
関連する問題