- #include<QDomDocument>
- #include<QDomElement>
- #include<QFile>
- #include<QTextStream>
- #include<iostream>
- using namespace std;
- void writeDom(){
- QFile file("dom.xml");
- if(not file.open(QIODevice::ReadWrite))
- return;
- QDomDocument doc;
- QDomElement startElem = doc.createElement("Start");
- doc.appendChild(startElem);
- startElem.setAttribute("animinal","cat");
- QDomElement bookElem = doc.createElement("book");
- bookElem.appendChild(doc.createTextNode("book1"));
- startElem.appendChild(bookElem);
- QTextStream out(&file);
- doc.save(out,3);
- file.close();
- }
- void readDom(){
- QFile file("dom.xml");
- if(not file.open(QIODevice::ReadWrite))
- return;
- QDomDocument doc;
- doc.setContent(&file);
- QDomElement startElem = doc.documentElement();
- cout << "animainal:";
- cout << startElem.attribute("animinal").toStdString() << endl;
- cout << "book:";
- cout << startElem.firstChildElement("book").text().toStdString() << endl;
- file.close();
- }
- int main()
- {
- writeDom();
- readDom();
- }
- <?xml version="1.0" encoding="UTF-8"?> animinal吗?
- <Start animinal="cat">
- <book>book1</book>
- </Start>
startElem.attribute("animinal").toStdString() 代码中的animinal是
xml中的?