目录
xml生成:
xml解析:
xml生成:
void Qxml::setTml()
{
QDomDocument doc;//xml文档树的创建
//xml文档树的指令版本必有的
QDomProcessingInstruction pi=doc.createProcessingInstruction("xml","version=\"1.0\" encoding=\"utf-8\"");
doc.appendChild(pi);//xml文档树的添加
QDomElement root=doc.createElement("root");//创建根元素 一般有且只有一个
doc.appendChild(root);//xml文档树的添加
///<root> 大致的格式基本如此
/// <ff00>ff0</ff00>
/// <ff11>ff1</ff11>
///</root> 添加规则 *注意父元素添的是元素 *子元素一般添的是文本 依赖文档树doc对象生成
QDomElement baotou=doc.createElement("报头");
root.appendChild(baotou);
QDomElement baowen=doc.createElement("报文");
root.appendChild(baowen);
//报头添加
{
QDomElement a1=doc.createElement("报头内容");
QDomElement a1_1=doc.createElement("子内容1");
a1_1.appendChild(doc.createTextNode("火力覆盖"));
QDomElement a1_2=doc.createElement("子内容2");
a1_2.appendChild(doc.createTextNode("直接打击"));
a1.appendChild(a1_1);
a1.appendChild(a1_2);
QDomElement a2=doc.createElement("报头内容");
a2.appendChild(doc.createTextNode("报头内容222"));
QDomElement a3=doc.createElement("报头内容");
a3.appendChild(doc.createTextNode("报头内容333"));
baotou.appendChild(a1);
baotou.appendChild(a2);
baotou.appendChild(a3);
}
//报文
{
QDomElement a1=doc.createElement("报文内容");
QDomElement a1_1=doc.createElement("子内容1");
a1_1.appendChild(doc.createTextNode("火力覆盖"));
QDomElement a1_2=doc.createElement("子内容2");
a1_2.appendChild(doc.createTextNode("直接打击"));
a1.appendChild(a1_1);
a1.appendChild(a1_2);
QDomElement a2=doc.createElement("报文内容");
a2.appendChild(doc.createTextNode("报文内容222"));
QDomElement a3=doc.createElement("报文内容");
a3.appendChild(doc.createTextNode("报文内容333"));
baowen.appendChild(a1);
baowen.appendChild(a2);
baowen.appendChild(a3);
}
//写入文件
QFile fi("baowen.xml");
if(!fi.open(QIODevice::WriteOnly|QIODevice::Text))
{return;}
else {
fi.write(doc.toByteArray());
fi.close();
qDebug()<<"写入成功";
}
}
xml解析:
void Qxml::getxml()
{
QFile f("ff.xml");//
f.open(QIODevice::ReadOnly|QIODevice::Text);
QByteArray bytes=f.readAll();
f.close();
QDomDocument doc;//xml文档树
doc.setContent(bytes);//必写如果写第二个参数可以为false,实际用的话一个参数就好了
QDomElement root=doc.documentElement();//必写返回xml文档的根元素(xml一般有且只有一个根元素)
///核心:拿到元素 获取元素里面的节点 进行判断是(文本节点直接输出)是(元素节点再次进行解析)
///<ff11>ff1</ff11> 这就是一个元素(element) 他的节点(node)为1 节点类型是文本节点
/// <ffff> ffff 就是一个元素 节点为1 节点类型是 元素节点
/// <ff00>ff0</ff00> ff00 是一个元素 节点为1 节点类型是 文本节点
/// <ff11>ff1</ff11> ff11 是一个元素 节点为1 节点类型是 文本节点
/// </ffff> //解了好久悟性太差了哎
QDomNodeList ns=root.childNodes();//获取根元素下所有同一级别 子节点 的个数
qDebug()<<QString("root元素个数%1").arg(ns.size());
QDomNode node=root.firstChild();//获取第一个子节点的所有节点
if(node.isElement())
//for(int i=0;i<ns.size();i++)//for一次处全部节点解开
{ //如果节点是元素 就把当前节点转换成元素 继续解里面的子节点
QDomElement el=node.toElement();
//QDomElement el=ns.at(i).toElement();//for一次处全部节点解开
QDomNodeList list=el.childNodes();
qDebug()<<QString("list元素个数%1 标签名%2").arg(list.size())+el.tagName();
for (int i = 0; i < list.size(); ++i)
{
QDomNode n=list.at(i);
//判断是文本节点肯定只有一组文本 直接用当前标签对象获取 标签名和文本名
if(n.nodeType()==QDomNode::TextNode)
{
//QDomText tx=n.toText();
//qDebug()<<el.tagName()+"-标签-"+tx.nodeValue();//获取单个节点文本
qDebug()<<el.tagName()+" "+el.text();//获取标签内全部文本
}
//如果是元素节点就继续解里面的子节点
else if(n.nodeType()==QDomNode::ElementNode)
{
QDomElement el=n.toElement();
QDomNodeList list=el.childNodes();
for(int i=0;i<list.size();i++)
{
QDomNode n=list.at(i);
if(n.nodeType()==QDomNode::TextNode)
{
//QDomText tx=n.toText();
//qDebug()<<el.tagName()+"-标签内-"+tx.nodeValue();
qDebug()<<el.tagName()+"-标签内-"+el.text();
}
else {
QDomElement el=n.toElement();
QDomNodeList list=el.childNodes();//没用使用为下一级节点做准备
qDebug()<<el.tagName()+" "+el.text();
}
}
//qDebug()<<el.tagName()+" "+el.text();
}
}
}
}