赞
踩
1.写xml文件
- void Sign::writeXml2( )
- {
- QString filename ="D:/ZCodeTest/Signs/Sign_0/Signature.xml";
- QFile file(filename);
-
- if(!file.open(QIODevice::WriteOnly|QIODevice::Text))
- {
- qDebug()<<QObject::tr("打开失败");
- return;
- }
-
- //写入xml头部
- QDomDocument doc;
- QDomProcessingInstruction instruction; //添加处理命令
- instruction=doc.createProcessingInstruction("xml","version=\"1.0\" encoding=\"UTF-8\"");
- doc.appendChild(instruction);//doc开头加入instruction
-
- //创建root根节点
- QDomElement root=doc.createElement("ofd:Signature");
- root.setAttribute("xmlns:ofd","http://www.ofdspec.org/2016"); //创建属性 其中键值对的值可以是各种类型
- doc.appendChild(root);//将root节点添加到doc
-
- //在root根节点下面创建子节点child
- QDomElement child=doc.createElement("ofd:SignedInfo"); //创建子元素
-
- //在child子节点下面创建叶子节点leaf1
- QDomElement leaf1 = doc.createElement("ofd:Provider");
- leaf1.setAttribute("ProviderName", "xxx");
- leaf1.setAttribute("Company", "xxxx");
- leaf1.setAttribute("Version","2.0");
- child.appendChild(leaf1); //先将leaf1节点添加到child
- root.appendChild(child); //再将child节点添加到根节点root
-
- //在child子节点下面创建与leaf1节点并列的leaf2节点
- QDomElement leaf2=doc.createElement("ofd:SignatureMethod"); //创建子元素
- QDomText text1; //设置括号标签中间的值
- text1=doc.createTextNode("1.2.840.113549.1.1.11");
- leaf2.appendChild(text1);
- child.appendChild(leaf2);
-
- //在child子节点下面创建与leaf1节点并列的leaf3节点
- QDomElement leaf3=doc.createElement("ofd:SignatureDateTime"); //创建子元素
- QDomText text2; //设置括号标签中间的值
- text2=doc.createTextNode("2020-04-05-10:26:38.586");
- child.appendChild(leaf3);
- leaf3.appendChild(text2);
-
- //在child子节点下面创建与leaf1节点并列的leaf4节点
- QDomElement leaf4 = doc.createElement("ofd:References");
- leaf4.setAttribute("CheckMethod", "2.16.840.1.101.3.4.2.1");
- child.appendChild(leaf4);
-
- //在leaf4叶子节点下面创建common节点
- QDomElement common = doc.createElement("ofd:References");
- common.setAttribute("FileRef", "/Doc_0/Signs/Sign_0/Seal.esl");
- QDomElement title3=doc.createElement("ofd:CheckValue"); //创建子元素
- QDomText text3; //设置括号标签中间的值
- text3=doc.createTextNode("rbhVQNUcRGVoVvIgTdcobIJZfh88R6fiI/KQHwB6mEA=");
- common.appendChild(title3);
- title3.appendChild(text3);
- leaf4.appendChild(common);
-
- //添加第二个子节点及其子元素,部分变量只需重新赋值
- common = doc.createElement("ofd:References");
- common.setAttribute("FileRef", "/Doc_0/Pages/Page_0/Content.xml");
- title3=doc.createElement("ofd:CheckValue"); //创建子元素
- text3=doc.createTextNode("avmJc1lYs0c/zLPcEtZmB3RQDzZpniHOL0trBV/uRBw=");
- common.appendChild(title3);
- title3.appendChild(text3);
- leaf4.appendChild(common);
-
- //添加第三个子节点及其子元素,部分变量只需重新赋值
- common = doc.createElement("ofd:References");
- common.setAttribute("FileRef", "/Doc_0/Document.xml");
- title3=doc.createElement("ofd:CheckValue"); //创建子元素
- text3=doc.createTextNode("SNv9vLp5bRwTd9auSEQ2+VsdIoV/C0csCdg3OFdCnKQ=");
- common.appendChild(title3);
- title3.appendChild(text3);
- leaf4.appendChild(common);
-
- //添加第四个子节点及其子元素,部分变量只需重新赋值
- common = doc.createElement("ofd:References");
- common.setAttribute("FileRef", "/Doc_0/PublicRes.xml");
- title3=doc.createElement("ofd:CheckValue"); //创建子元素
- text3=doc.createTextNode("v4lm6B1YSqt83ercEoKu4P/Yz2PXG548bap2b2I6sho=");
- common.appendChild(title3);
- title3.appendChild(text3);
- leaf4.appendChild(common);
-
- //在child子节点下面创建与leaf1节点并列的leaf5节点
- QDomElement leaf5 = doc.createElement("ofd:StampAnnot");
- leaf5.setAttribute("PageRef", "1");
- leaf5.setAttribute("ID", "1");
- leaf5.setAttribute("Boundary","86.545 67.2744 42 42");
- child.appendChild(leaf5);
-
- //在child子节点下面创建与leaf1节点并列的leaf6节点
- QDomElement leaf6 = doc.createElement("ofd:Seal");
- QDomElement title6=doc.createElement("ofd:BaseLoc"); //创建子元素
- QDomText text6=doc.createTextNode("Seal.esl");
- leaf6.appendChild(title6);
- title6.appendChild(text6);
- child.appendChild(leaf6);
-
- //在root根节点下面创建与子节点child并列的child2节点
- QDomElement child2=doc.createElement("ofd:SignedValue"); //创建子元素
- QDomText text7=doc.createTextNode("SignedValue.dat");
- root.appendChild(child2);
- child2.appendChild(text7);
-
- //输出到文件
- QTextStream out_stream(&file);
- doc.save(out_stream,4); //缩进4格
- file.close();
-
- }
2.读xml文件
- void Sign::readXml()
- {
- QString filename ="C:/Users/ntko/Desktop/Signature.xml";
-
- QFile file(filename);
-
- if(file.exists())
- {
- if(!file.open(QFile::ReadOnly))
- {
- qDebug()<<QObject::tr("打开失败");
- return ;
- }
- file.close();
- }
- else
- {
- qDebug()<<QObject::tr("该文件不存在");
- return ;
- }
-
- QDomDocument doc;
- if (!doc.setContent(&file))
- {
- file.close();
- qDebug()<<"文件解析失败";
- return;
- }
-
- QDomElement root=doc.documentElement(); //返回根节点
- qDebug()<<root.nodeName();
-
- QDomNode node=root.firstChild(); //获得第一个子节点
- qDebug()<<node.nodeName();
-
- if(!node.isNull())
- {
- if(node.isElement())
- {
- QDomElement e=node.toElement(); //转换为元素,注意元素和节点是两个数据结构,其实差不多
- QDomNodeList list=e.childNodes();
- QDomNode n=list.at(0);
- if(node.isElement())
- qDebug()<<n.nodeName()<<":"<<n.toElement().attribute("ProviderName")<<":"<<n.toElement().attribute("Company")<<":"<<n.toElement().attribute("Version");
-
- QString str=n.toElement().attribute("ProviderName");
- QByteArray ba = str.toLatin1();
- puchName=(unsigned char *)ba.data();
-
- int len= strlen((char*)puchName);
- piNameLen=&len;
- qDebug()<<len;
- qDebug()<<piNameLen;
-
- QDomNode n1=list.at(1);
- if(node.isElement())
- qDebug()<<n1.nodeName()<<":"<<n1.toElement().text();
-
- QDomNode n2=list.at(2);
- if(node.isElement())
- qDebug()<<n2.nodeName()<<":"<<n2.toElement().text();
-
- QDomNode n3=list.at(3);
- if(node.isElement())
- qDebug()<<n3.nodeName()<<":"<<n3.toElement().attribute("CheckMethod");
- //0,1,2,3 四个子节点
- //再把ofd:Reference中的子节点遍历出来
-
- QDomElement t=n3.toElement();
- QDomNodeList x=t.childNodes();
- for(int i=0;i<x.count();i++) //遍历子元素,count和size都可以用,可用于标签数计数
- {
- QDomNode n10=x.at(i);
- if(node.isElement())
- qDebug()<<n10.nodeName()<<n10.toElement().attribute("FileRef")<<":"<<n10.toElement().text();
- }
- //4,5 两个子节点
- QDomNode n4=list.at(4);
- if(node.isElement())
- qDebug()<<n4.nodeName()<<":"<<n4.toElement().attribute("PageRef")<<":"<<n4.toElement().attribute("ID")<<":"<<n4.toElement().attribute("Boundary");
-
- QDomNode n5=list.at(5);
- if(node.isElement())
- qDebug()<<n5.nodeName()<<":"<<n5.toElement().text();
- }
- }
- QDomNode node2=root.lastChild();//获得最后一个子节点
- qDebug()<<node2.nodeName()<<":"<<node2.toElement().text();
-
-
- }
xml格式文件:
- <?xml version="1.0" encoding="UTF-8"?>
- <ofd:Signature xmlns:ofd="http://www.ofdspec.org/2016">
- <ofd:SignedInfo>
- <ofd:Provider ProviderName="xxx" Version="2.0" Company="xxxx"/>
- <ofd:SignatureMethod>1.2.840.113549.1.1.11</ofd:SignatureMethod>
- <ofd:SignatureDateTime>2020-04-05-10:26:38.586</ofd:SignatureDateTime>
- <ofd:References CheckMethod="2.16.840.1.101.3.4.2.1">
- <ofd:References FileRef="/Doc_0/Signs/Sign_0/Seal.esl">
- <ofd:CheckValue>rbhVQNUcRGVoVvIgTdcobIJZfh88R6fiI/KQHwB6mEA=</ofd:CheckValue>
- </ofd:References>
- <ofd:References FileRef="/Doc_0/Pages/Page_0/Content.xml">
- <ofd:CheckValue>avmJc1lYs0c/zLPcEtZmB3RQDzZpniHOL0trBV/uRBw=</ofd:CheckValue>
- </ofd:References>
- <ofd:References FileRef="/Doc_0/Document.xml">
- <ofd:CheckValue>SNv9vLp5bRwTd9auSEQ2+VsdIoV/C0csCdg3OFdCnKQ=</ofd:CheckValue>
- </ofd:References>
- <ofd:References FileRef="/Doc_0/PublicRes.xml">
- <ofd:CheckValue>v4lm6B1YSqt83ercEoKu4P/Yz2PXG548bap2b2I6sho=</ofd:CheckValue>
- </ofd:References>
- </ofd:References>
- <ofd:StampAnnot ID="1" Boundary="86.545 67.2744 42 42" PageRef="1"/>
- <ofd:Seal>
- <ofd:BaseLoc>Seal.esl</ofd:BaseLoc>
- </ofd:Seal>
- </ofd:SignedInfo>
- <ofd:SignedValue>SignedValue.dat</ofd:SignedValue>
- </ofd:Signature>
参考文档:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。