C++ (Qt)MyXMLObject::MyXMLObject(QString xmlName,QObject *parent): QObject(parent){ QDomDocument domDoc; QFile file(xmlName); if(file.open(QIODevice::ReadOnly)) { qDebug()<<"File open"; if(domDoc.setContent(&file)) { QDomElement domElement= domDoc.documentElement(); traverseNode(domElement); } file.close(); } else { qDebug()<<"File no open"; }} MyXMLObject::~MyXMLObject(){ } void MyXMLObject::traverseNode(const QDomNode& node){ QDomNode domNode = node.firstChild(); while(!domNode.isNull()) { if(domNode.isElement()) { QDomElement domElement = domNode.toElement(); if(!domElement.isNull()) { if(domElement.tagName() == "contact") { qDebug() << "Attr: " << domElement.attribute("number", ""); } else { qDebug() << "TagName: " << domElement.tagName() << "\tText: " << domElement.text(); } } } traverseNode(domNode); domNode = domNode.nextSibling(); }}
<?xml version="1.0" encoding="Windows-1251"?><doc name="Demaker"> <contact number="1234567890"/></doc>
void MyXMLObject::traverseNode(const QDomElement& node){ QDomElement domElement = node.firstChildElement(); while(!domElement.isNull()) { if(domElement.tagName() == "contact") { qDebug() << "Attr: " << domElement.attribute("number", ""); } else { qDebug() << "TagName: " << domElement.tagName() << "\tText: " << domElement.text(); } traverseNode(domElement); domElement = domElement.nextSiblingElement(); }}
C++ (Qt)MyXMLObject::MyXMLObject(QString xmlName,QObject *parent): QObject(parent){ QDomDocument domDoc; QFile file(xmlName); if(file.open(QIODevice::ReadOnly)) { qDebug()<<"File open"; if(domDoc.setContent(&file)) { QDomElement domElement= domDoc.documentElement(); QDomNode node = domElement.firstChild(); while (!node.isNull()) { if(node.isElement()) { QDomElement element = node.toElement(); qDebug()<<tr("Element:")<<element.tagName(); qDebug()<<tr("Attribut:")<<element.attribute("name","--"); } if(node.isText()) { QDomText text = node.toText(); qDebug()<<text.data(); } node = node.nextSibling(); } //traverseNode(domElement); } file.close(); } else { qDebug()<<"File no open"; }}
File open "Element:" "o:DocumentProperties" "Attribut:" "--" "Element:" "w:fonts" "Attribut:" "--" "Element:" "w:styles" "Attribut:" "--" "Element:" "w:docPr" "Attribut:" "--" "Element:" "w:body" "Attribut:" "--"
<?xml version="1.0" encoding="windows-1251"?><doc name="Evgen"> <contact number="12345676890"/></doc>
C++ (Qt)...QDomDocument domDoc;QFile file(xmlName); qDebug()<<file.fileName(); //имя файла!!!
"D:/Qt/Qt5.2.1/Tools/QtCreator/bin/Graphics/addressbook.xml"
<?xml version="1.0"?><!--MY Address Book--><addressbook><contact number = "1"> <name>Piggy</name> <phone>+49 612345663</phone> <email>piggy@mega.de</email> </contact></addressbook>
C++ (Qt)MyXMLObject::MyXMLObject(QString xmlName,QObject *parent): QObject(parent){ QDomDocument domDoc; QFile file(xmlName); qDebug()<<file.fileName(); if(file.open(QIODevice::ReadOnly)) { qDebug()<<"File open"; if(domDoc.setContent(&file)) { QDomElement domElement = domDoc.documentElement(); traverseNode(domElement); } file.close(); } else { qDebug()<<"File no open"; }} MyXMLObject::~MyXMLObject(){ } void MyXMLObject::traverseNode(const QDomNode& node){ QDomNode domNode = node.firstChild(); while(!domNode.isNull()) { if(domNode.isElement()) { QDomElement domElement = domNode.toElement(); if(!domElement.isNull()) { if(domElement.tagName() == "contact") { qDebug() << "Attr: " << domElement.attribute("number", ""); } else { qDebug() << "TagName: " << domElement.tagName() << "\tText: " << domElement.text(); } } } traverseNode(domNode); domNode = domNode.nextSibling(); }}
"D:/Qt/Qt5.2.1/Tools/QtCreator/bin/Graphics/addressbook.xml" File open TagName: "o:DocumentProperties" Text: "<EVGENIEVGENI212014-05-20T10:26:00Z2014-05-20T10:26:00Z128163SNIIP1119011.5604" TagName: "o:Title" Text: "<" TagName: "o:Author" Text: "EVGENI" TagName: "o:LastAuthor" Text: "EVGENI" TagName: "o:Revision" Text: "2" TagName: "o:TotalTime" Text: "1" TagName: "o:Created" Text: "2014-05-20T10:26:00Z" TagName: "o:LastSaved" Text: "2014-05-20T10:26:00Z" TagName: "o:Pages" Text: "1" TagName: "o:Words" Text: "28" TagName: "o:Characters" Text: "163" TagName: "o:Company" Text: "SNIIP" TagName: "o:Lines" Text: "1" TagName: "o:Paragraphs" Text: "1" TagName: "o:CharactersWithSpaces" Text: "190" TagName: "o:Version" Text: "11.5604" TagName: "w:fonts" Text: "" TagName: "w:defaultFonts" Text: "" TagName: "w:styles" Text: "" TagName: "w:versionOfBuiltInStylenames" Text: "" TagName: "w:latentStyles" Text: "" TagName: "w:style" Text: "" TagName: "w:name" Text: "" TagName: "wx:uiName" Text: "" TagName: "w:rPr" Text: "" TagName: "wx:font" Text: "" TagName: "w:sz" Text: "" TagName: "w:sz-cs" Text: "" TagName: "w:lang" Text: "" TagName: "w:style" Text: "" TagName: "w:name" Text: "" TagName: "wx:uiName" Text: "" TagName: "w:semiHidden" Text: "" TagName: "w:style" Text: "" TagName: "w:name" Text: "" TagName: "wx:uiName" Text: "" TagName: "w:semiHidden" Text: "" TagName: "w:rPr" Text: "" TagName: "wx:font" Text: "" TagName: "w:tblPr" Text: "" TagName: "w:tblInd" Text: "" TagName: "w:tblCellMar" Text: "" TagName: "w:top" Text: "" TagName: "w:left" Text: "" TagName: "w:bottom" Text: "" TagName: "w:right" Text: "" TagName: "w:style" Text: "" TagName: "w:name" Text: "" TagName: "wx:uiName" Text: "" TagName: "w:semiHidden" Text: "" TagName: "w:docPr" Text: "" TagName: "w:view" Text: "" TagName: "w:zoom" Text: "" TagName: "w:attachedTemplate" Text: "" TagName: "w:defaultTabStop" Text: "" TagName: "w:punctuationKerning" Text: "" TagName: "w:characterSpacingControl" Text: "" TagName: "w:optimizeForBrowser" Text: "" TagName: "w:validateAgainstSchema" Text: "" TagName: "w:saveInvalidXML" Text: "" TagName: "w:ignoreMixedContent" Text: "" TagName: "w:alwaysShowPlaceholderText" Text: "" TagName: "w:compat" Text: "" TagName: "w:breakWrappedTables" Text: "" TagName: "w:snapToGridInCell" Text: "" TagName: "w:wrapTextWithPunct" Text: "" TagName: "w:useAsianBreakRules" Text: "" TagName: "w:dontGrowAutofit" Text: "" TagName: "w:body" Text: "<?xml version="1.0"?><!--MY Address Book--><addressbook><contact number = "1"><name>Piggy</name><phone>+49 612345663</phone><email>piggy@mega.de</email></contact></addressbook>" TagName: "wx:sect" Text: "<?xml version="1.0"?><!--MY Address Book--><addressbook><contact number = "1"><name>Piggy</name><phone>+49 612345663</phone><email>piggy@mega.de</email></contact></addressbook>" TagName: "w:p" Text: "<?xml version="1.0"?>" TagName: "w:r" Text: "<?xml version="1.0"?>" TagName: "w:t" Text: "<?xml version="1.0"?>" TagName: "w:p" Text: "<!--MY Address Book-->" TagName: "w:r" Text: "<!--MY Address Book-->" TagName: "w:t" Text: "<!--MY Address Book-->" TagName: "w:p" Text: "<addressbook>" TagName: "w:r" Text: "<addressbook>" TagName: "w:t" Text: "<addressbook>" TagName: "w:p" Text: "<contact number = "1">" TagName: "w:r" Text: "<contact number = "1">" TagName: "w:t" Text: "<contact number = "1">" TagName: "w:p" Text: "<name>Piggy</name>" TagName: "w:r" Text: "<name>Piggy</name>" TagName: "w:tab" Text: "" TagName: "w:t" Text: "<name>Piggy</name>" TagName: "w:p" Text: "<phone>+49 612345663</phone>" TagName: "w:r" Text: "<phone>+49 612345663</phone>" TagName: "w:tab" Text: "" TagName: "w:t" Text: "<phone>+49 612345663</phone>" TagName: "w:p" Text: "<email>piggy@mega.de</email>" TagName: "w:r" Text: "<email>piggy@mega.de</email>" TagName: "w:tab" Text: "" TagName: "w:t" Text: "<email>piggy@mega.de</email>" TagName: "w:r" Text: "" TagName: "w:tab" Text: "" TagName: "w:r" Text: "" TagName: "w:tab" Text: "" TagName: "w:p" Text: "</contact>" TagName: "w:r" Text: "</contact>" TagName: "w:t" Text: "</contact>" TagName: "w:p" Text: "</addressbook>" TagName: "w:r" Text: "</addressbook>" TagName: "w:t" Text: "</addressbook>" TagName: "w:sectPr" Text: "" TagName: "w:pgSz" Text: "" TagName: "w:pgMar" Text: "" TagName: "w:cols" Text: "" TagName: "w:docGrid" Text: ""
MyXMLObject::MyXMLObject(QString xmlName,QObject *parent): QObject(parent){ QDomDocument domDoc; QFile file(xmlName); qDebug()<<file.fileName(); if(file.open(QIODevice::ReadOnly)) { qDebug()<<"File open"; if(domDoc.setContent(&file)) { QDomElement domElement = domDoc.documentElement(); traverseNode(domElement.firstChild()); // <<<<<<<<<<<<<<<<<<<<< } file.close(); } else { qDebug()<<"File no open"; }}
"D:/Qt/Qt5.2.1/Tools/QtCreator/bin/Graphics/addressbook.xml" File open TagName: "o:Title" Text: "<" TagName: "o:Author" Text: "EVGENI" TagName: "o:LastAuthor" Text: "EVGENI" TagName: "o:Revision" Text: "2" TagName: "o:TotalTime" Text: "1" TagName: "o:Created" Text: "2014-05-20T10:26:00Z" TagName: "o:LastSaved" Text: "2014-05-20T10:26:00Z" TagName: "o:Pages" Text: "1" TagName: "o:Words" Text: "28" TagName: "o:Characters" Text: "163" TagName: "o:Company" Text: "SNIIP" TagName: "o:Lines" Text: "1" TagName: "o:Paragraphs" Text: "1" TagName: "o:CharactersWithSpaces" Text: "190" TagName: "o:Version" Text: "11.5604"
<?xml version="1.0"?><!--MY Address Book--><addressbook><contact number = "1"> <name>Piggy</name> <phone>+49 612345663</phone> <email>piggy@mega.de</email></contact></addressbook>
class MyXMLObject : public QObject{public: MyXMLObject(QString xmlName,QObject *parent = 0) : QObject(parent) { QDomDocument domDoc; QFile file(xmlName); qDebug()<<file.fileName(); if(file.open(QIODevice::ReadOnly)) { qDebug()<<"File open"; if(domDoc.setContent(&file)) { QDomElement domElement = domDoc.documentElement(); traverseNode(domElement); } file.close(); } else { qDebug()<<"File no open"; } } ~MyXMLObject() { } void traverseNode(const QDomNode& node) { QDomNode domNode = node.firstChild(); while(!domNode.isNull()) { if(domNode.isElement()) { QDomElement domElement = domNode.toElement(); if(!domElement.isNull()) { if(domElement.tagName() == "contact") { qDebug() << "Attr: " << domElement.attribute("number", ""); } else { qDebug() << "TagName: " << domElement.tagName() << "\tText: " << domElement.text(); } } } traverseNode(domNode); domNode = domNode.nextSibling(); } }};...MyXMLObject xxx("d:/addr.xml");
"d:/addr.xml" File open Attr: "1" TagName: "name" Text: "Piggy" TagName: "phone" Text: "+49 612345663" TagName: "email" Text: "piggy@mega.de"