虽然看过也用过XML方面的,但对QT如何处理XML还比较陌生,工作中也用,但主要是调用同事写好的接口
转载的目标,是消除QT如何处理xml(或应用)神秘感
int main(int argc, char **argv)
{
Q_UNUSED(argc);
Q_UNUSED(argv);
QFile file("text.xml");
if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
return 0;
QDomDocument doc;
QDomElement ui = doc.createElement("ui");
ui.setAttribute("name", "UI EDITOR");
QDomElement ribbon = doc.createElement("ribbon");
ribbon.setAttribute("name", "ribbon");
ui.appendChild(ribbon);
QDomElement tab = doc.createElement("tab");
tab.setAttribute("title", "simulation");
ribbon.appendChild(tab);
QDomElement panel = doc.createElement("panel");
tab.appendChild(panel);
for (int i = 0; i < 10; ++i) {
QDomElement button = doc.createElement("button");
button.setAttribute("tooltip", QString("scFileOpen('/home/vajindar/Project/auto%1.tin')").arg(i + 1));
button.setAttribute("id", i + 1);
panel.appendChild(button);
}
doc.appendChild(ui);
QTextStream out(&file);
out << doc.toString();
return 0;
}
/**********************write xml******************/
#include
#include
#include
#include
#include
#include
struct Contact
{
QString name;
QString phone;
QString eMail;
};
QDomElement ContactToNode( QDomDocument &d, const Contact &c )
{
QDomElement cn = d.createElement( "contact" );
cn.setAttribute( "name", c.name );
cn.setAttribute( "phone", c.phone );
cn.setAttribute( "email", c.eMail );
return cn;
}
int main( int argc, char **argv )
{
QApplication a( argc, argv );
QDomDocument doc( "AdBookML" );
QDomElement root = doc.createElement( "adbook" );
doc.appendChild( root );
Contact c;
c.name = "Kal";
c.eMail = "";
c.phone = "+46(0)31 123 4567";
root.appendChild( ContactToNode( doc, c ) );
c.name = "Ada";
c.eMail = "";
c.phone = "+46(0)31 765 1234";
root.appendChild( ContactToNode( doc, c ) );
QFile file( "test01.xml" );
if( !file.open( QFile::WriteOnly | QFile::Text ) )
return -1;
QTextStream ts( &file );
ts << doc.toString();
file.close();
return 0;
}
/********************** read xml ***************/
struct Contact
{
QString name;
QString phone;
QString eMail;
};
int main4()
{
QDomDocument doc( "AdBookML" );
QFile file( "test01.xml" );
if( !file.open( QFile::ReadOnly | QFile::Text ) )
return -1;
if( !doc.setContent( &file ) )
{
file.close();
return -2;
}
file.close();
QDomElement root = doc.documentElement();
if( root.tagName() != "adbook" )
return -3;
QDomNode n = root.firstChild();
while( !n.isNull() )
{
QDomElement e = n.toElement();
if( !e.isNull() )
{
if( e.tagName() == "contact" )
{
Contact c;
c.name = e.attribute( "name", "" );
c.phone = e.attribute( "phone", "" );
c.eMail = e.attribute( "email", "" );
qDebug() << "Contact = " << c.name << "\n" << c.phone << "\n" << c.eMail;
//QMessageBox::information( 0, "Contact", c.name + "\n" + c.phone + "\n" + c.eMail );
}
}
n = n.nextSibling();
}
return 0;
}
/********************** sax xml ***************/
class AdBookParser : public QXmlDefaultHandler
{
public:
bool startDocument()
{
inAdBook = false;
return true;
}
bool endElement( const QString&, const QString&, const QString &name )
{
if( name == "adbook" )
inAdBook = false;
return true;
}
bool startElement( const QString&, const QString&, const QString &name, const QXmlAttributes &attrs )
{
if( inAdBook && name == "contact" )
{
QString name, phone, email;
for( int i=0; i
{
if( attrs.localName( i ) == "name" )
name = attrs.value( i );
else if( attrs.localName( i ) == "phone" )
phone = attrs.value( i );
else if( attrs.localName( i ) == "email" )
email = attrs.value( i );
}
qDebug() << "Contact = " << name << "\n" << phone << "\n" << email;
//QMessageBox::information( 0, "Contact", name + "\n" + phone + "\n" + email );
}
else if( name == "adbook" )
inAdBook = true;
return true;
}
private:
bool inAdBook;
};
int main_sax( int argc, char **argv )
{
QApplication a( argc, argv );
AdBookParser handler;
QFile file( "test01.xml" );
QXmlInputSource source( &file );
QXmlSimpleReader reader;
reader.setContentHandler( &handler );
reader.parse( source );
return 0;
}
/********************* read xml attribute*********************************/
child11 boy
child12 boy
child21 boy
child22 boy
#include
#include
#include
#include
void addElements(QList& list, const QDomElement& root)
{
// add children to list
QDomNode node = root.firstChild();
for (; !node.isNull(); node = node.nextSibling())
{
if (!node.isElement()) continue;
list.append(node.toElement());
if (node.isText())
{
QString title = node.toText().data();
//qDebug() << "root" << title;
}
}
// go deeper
node = root.firstChild();
for (; !node.isNull(); node = node.nextSibling())
{
if (!node.isElement()) continue;
addElements(list, node.toElement());
if (node.isText())
{
QString title = node.toText().data();
//qDebug() << "node" << title;
}
}
QDomElement child = root.firstChildElement();
while (!child.isNull())
{
qDebug() << "tag = " << child.tagName();
if (child.hasAttribute("item") )
{
qDebug() << "Att = " << child.attribute("item");
}
if (child.hasAttribute("items") )
{
qDebug() << "Atts = " << child.attribute("items");
}
if (child.hasAttribute("item") )
{
if ( !child.isText() )
{
qDebug() << "Text = " << child.text() << "\n";
}
QString title = child.firstChildElement("child21").text();
if (!title.isEmpty())
{
qDebug() << "dddddd = " << title << "\n";
}
}
child = child.nextSiblingElement();
}
}
int main(int argc, char** argv)
{
if (argc != 2)
qFatal("call with xml file as the argument");
QFile file(argv[1]);
if (!file.exists())
qFatal("file \"%s\" does not exist", argv[1]);
if (!file.open(QFile::ReadOnly | QFile::Text))
qFatal("file \"%s\" cannot be read", argv[1]);
QString errorStr;
int errorLine;
int errorColumn;
QDomDocument doc;
//if (!domDocument.setContent(device, true, &errorStr, &errorLine,&errorColumn)) {
if (!doc.setContent(&file, true, &errorStr, &errorLine,&errorColumn))
qFatal("file \"%s\" cannot be parsed", argv[1]);
QList list;
addElements(list, doc.documentElement());
qDebug("file has %d elements", list.count());
QList::const_iterator it = list.begin();
QList::const_iterator endIt = list.end();
for (; it != endIt; ++it)
{
qDebug("%s", (*it).nodeName().toUtf8().data());
}
return 0;
}