xml and txt format
时间:2009-07-22 来源:fewlife
by tangke <[email protected]> 2009-06-28
#include <QtXml/QDomProcessingInstruction>
#include <QtXml/QDomElement>
#include <QtXml/QDomDocument>
#include <QtCore/QCoreApplication>
#include <QtCore/QFile>
#include <QtCore/QObject>
#include <QtCore/QTextStream>
#include <QtCore/QtDebug>
void read()
{
QDomDocument doc;
QFile file("out.txt");
QString errorStr;
int errorLine;
int errorCol;
//setContent是将指定的内容指定给QDomDocument解析,第一参数可以是QByteArray或者是文件名
等
if(!doc.setContent(&file,true,&errorStr,&errorLine,&errorCol))
{
//如果出错,则会进入这里。errorStr得到的是出错说明
//errorLine和errorCol则是出错的行和列
return;
}
QDomNodeList list = doc.childNodes();
for(int i = 0;i < list.count();i ++){
QDomNode node=list.at(i);//好的风格当然是把定义写在外面:(
qDebug()<<node.hasChildNodes();
qDebug()<<"node name is "<<node.nodeName();
qDebug()<<"node type is "<<node.nodeType();
}
}
void save()
{
QDomDocument doc;
QDomProcessingInstruction instruction;
instruction =
doc.createProcessingInstruction("xml","version=\"1.0\" encoding=\"UTF-8\"");
doc.appendChild(instruction);
QDomElement root = doc.createElement("person");
doc.appendChild(root);
QDomElement kgl_input = doc.createElement("io");
root.appendChild(kgl_input);
QDomAttr name = doc.createAttribute("name");
QDomAttr type = doc.createAttribute("type");
name.setValue(QObject::tr("zhangsan"));
type.setValue(QObject::tr("90"));
kgl_input.setAttributeNode(name);
kgl_input.setAttributeNode(type);
QFile file("out.txt");
if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
return ;
QTextStream out(&file);
out.setCodec("UTF-8");
doc.save(out,4,QDomNode::EncodingFromTextStream);
}
int main(int argc, char **argv)
{
QCoreApplication a(argc, argv);
save();
read();
return a.exec();
}
#include <QtXml/QDomProcessingInstruction>
#include <QtXml/QDomElement>
#include <QtXml/QDomDocument>
#include <QtCore/QCoreApplication>
#include <QtCore/QFile>
#include <QtCore/QObject>
#include <QtCore/QTextStream>
#include <QtCore/QtDebug>
void read()
{
QDomDocument doc;
QFile file("out.txt");
QString errorStr;
int errorLine;
int errorCol;
//setContent是将指定的内容指定给QDomDocument解析,第一参数可以是QByteArray或者是文件名
等
if(!doc.setContent(&file,true,&errorStr,&errorLine,&errorCol))
{
//如果出错,则会进入这里。errorStr得到的是出错说明
//errorLine和errorCol则是出错的行和列
return;
}
QDomNodeList list = doc.childNodes();
for(int i = 0;i < list.count();i ++){
QDomNode node=list.at(i);//好的风格当然是把定义写在外面:(
qDebug()<<node.hasChildNodes();
qDebug()<<"node name is "<<node.nodeName();
qDebug()<<"node type is "<<node.nodeType();
}
}
void save()
{
QDomDocument doc;
QDomProcessingInstruction instruction;
instruction =
doc.createProcessingInstruction("xml","version=\"1.0\" encoding=\"UTF-8\"");
doc.appendChild(instruction);
QDomElement root = doc.createElement("person");
doc.appendChild(root);
QDomElement kgl_input = doc.createElement("io");
root.appendChild(kgl_input);
QDomAttr name = doc.createAttribute("name");
QDomAttr type = doc.createAttribute("type");
name.setValue(QObject::tr("zhangsan"));
type.setValue(QObject::tr("90"));
kgl_input.setAttributeNode(name);
kgl_input.setAttributeNode(type);
QFile file("out.txt");
if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
return ;
QTextStream out(&file);
out.setCodec("UTF-8");
doc.save(out,4,QDomNode::EncodingFromTextStream);
}
int main(int argc, char **argv)
{
QCoreApplication a(argc, argv);
save();
read();
return a.exec();
}
相关阅读 更多 +