1

XML ファイルからデータをインポートする方法がわかりません。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>

そのデータをどのようにインポートすればよいですか? ありがとう!

4

3 に答える 3

2

QtXml モジュールを見てください。

このクラスは必要なことを行うと思います: http://doc.qt.io/qt-5/qdomdocument.html

XML ファイルを、読み取りまたは変更可能なツリーとしてロードできます。

于 2011-05-02T14:44:26.853 に答える
2

Bookmark Exampleを見るのが最も簡単です。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 を使用できます。いくつかの例については、こちらをご覧ください。

したがって、xml を読み取り、XML ファイルが提供するものに応じて、オブジェクト/ランタイムを作成/設定します。

于 2011-05-02T17:02:28.670 に答える
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;
}
于 2011-05-02T17:18:35.677 に答える