zoukankan      html  css  js  c++  java
  • qt 解析XML

    QDomDocument parsing Example XML file <?xmlversion="1.0"encoding="UTF-8"?>
    <persons>
    <personid="1">
    <firstname>John</firstname>
    <surname>Doe</surname>
    <email>john.doe@example.com</email>
    <website>http://en.wikipedia.org/wiki/John_Doe</website>
    </person>
    <personid="2">
    <firstname>Jane</firstname>
    <surname>Doe</surname>
    <email>jane.doe@example.com</email>
    <website>http://en.wikipedia.org/wiki/John_Doe</website>
    </person>
    <personid="3">
    <firstname>Matti</firstname>
    <surname>Meikäläinen</surname>
    <email>matti.meikalainen@example.com</email>
    <website>http://fi.wikipedia.org/wiki/Matti_Meikäläinen</website>
    </person>
    </persons> Header file class QXSRExample :public QMainWindow
    {
    Q_OBJECT
     
    public:
    QXSRExample(QWidget *parent = 0);
    ~QXSRExample();
    private slots:
    void parseXML();
    private:
    QPointer<QVBoxLayout> _layout;
    void setupUI();
    void addPersonsToUI(QList< QMap<QString,QString>>& persons);
    };Source filevoid QXSRExample::parseXML(){
    /* We'll parse the example.xml */
    QFile* file =new QFile("C://example.xml");
    /* If we can't open it, let's show an error message. */
    if(!file->open(QIODevice::ReadOnly| QIODevice::Text)){
    QMessageBox::critical(this,
    "QXSRExample::parseXML",
    "Couldn't open example.xml",
    QMessageBox::Ok);
    return;
    }
     
    QList< QMap<QString,QString>> persons;
     
    /* QDomDocument takes any QIODevice. as well as QString buffer*/
    QDomDocument doc("mydocument");
    if(!doc.setContent(file))
    {
    return;
    }
     
    //Get the root element
    QDomElement docElem = doc.documentElement();
     
    // you could check the root tag name here if it matters
    QString rootTag = docElem.tagName();// == persons
     
    // get the node's interested in, this time only caring about person's
    QDomNodeList nodeList = docElem.elementsByTagName("person");
     
    //Check each node one by one.
    for(int ii =0;ii < nodeList.count(); ii++)
    {
    QMap<QString, QString> person;
     
    // get the current one as QDomElement
    QDomElement el = nodeList.at(ii).toElement();
     
    person["id"]= el.attribute("id");// get and set the attribute ID
     
    //get all data for the element, by looping through all child elements
    QDomNode pEntries = el.firstChild();
    while(!pEntries.isNull()){
    QDomElement peData = pEntries.toElement();
    QString tagNam = peData.tagName();
     
    if(tagNam =="firstname"){/* We've found first name. */
    person["firstname"]= peData.text();
    }elseif(tagNam =="surname"){/* We've found surname. */
    person["surname"]= peData.text();
    }elseif(tagNam =="email"){/* We've found email. */
    person["email"]= peData.text();
    }elseif(tagNam =="website"){/* We've found website. */
    person["website"]= peData.text();
    }
     
    pEntries = pEntries.nextSibling();
    }
     
    persons.append(person);
    }
     
    this->addPersonsToUI(persons);
    }

  • 相关阅读:
    《飞得更高孙正义传》成为亚洲首富肯定有原因
    AutoController通用自动增删查改
    Silverlight学习笔记第一季(3)扯扯ComboBox
    Silverlight学习笔记第一季(1)DataGrid
    菜鸟混社区之如何看待社区的口水战?
    局部化页面和效率CMS实践系列总结
    图片猜成语(思维@娱乐)
    接口+泛型强大的Repository
    Silverlight学习笔记第一季(2)Listbox横向绑定数据
    poj 2954 Triangle (pick 定理 的应用 )
  • 原文地址:https://www.cnblogs.com/zzxap/p/2175689.html
Copyright © 2011-2022 走看看