Some words about Qt DOM Node, Element and Attribut

Add Instruction Node

Every valid XML must contain processing instruction. XML is widely used for HTML, SVG, XLS etc. So make sure your XML file has valid instruction of its type and encoding. The following line is a sample XML processing instruction.

<?xml version="1.0" encoding="UTF-8"?>

Skip Instruction Node While Reading XML

I don’t know how you read XML nodes and values. Most examples out there use parent and child counts. Obviously, DOM processing instruction is a node. Let’s refer the following XML file.

<?xml version="1.0" encoding="UTF-8"?>
<properties>
 <node type="user">minhaz</node>
 <node type="os">linux</node>
 <node type="version">3.2.0-37</node>
</properties>

A valid XML must start with a root element. Here properties is a root element. It has 3 child nodes. Each node has node value and attribute. So how do we parse the whole XML? If we refer the whole document as a node, then the first line, processing instruction, will be regarded as a node. So properties will be the 2nd child node, but technically, it is the first/root node. So instead of using document.childNodes().at(1) method, we could use document.namedItem(“properties”) to get list of all child nodes under properties tag. Here is a sample code.

QDomDocument document;
document.setContent(xmlString);
QDomNode root = document.namedItem("properties");
QDomNodeList nodes = root.childNodes();

for(int i=0;i<nodes.count();i++)
{
    qDebug() << "Type: " << nodes.at(i).toElement().attribute("type")
	      << " Value: " << nodes.at(i).toElement().text();
}

And this will result into the following output.

Type:  "user"  Value:  "minhaz" 
Type:  "os"  Value:  "linux" 
Type:  "version"  Value:  "3.2.0-37"

Any more confusion? Or a better method? Let me know.

Create XML and Write into File

Parsing XML is easy. The structure is already preset there. Just detect the hierarchy and parse. But what about creating your own? Perhaps from an user defined directory tree? I went into this painful issue, and then found the correct solution.

What if we try to generate an XML from the previous text output. Assume that we have two string lists.

QStringList attributeValues = QStringList() << "user" << "os" << "version";
QStringList nodeValues = QStringList() << "minhaz" << "linux" << "3.2.0-37";

We need to create an XML using these strings, and with a processing instruction. Lets get started.

First, create a DOM document. (Don’t blame me unless you added qt += xml to your project file) Then declare a QDomDocument object.

QDomDocument document;

Now create a QDomProcessingInstruction object with correct encoding and XML version. Then add it as child node of document.

QDomProcessingInstruction header = document.createProcessingInstruction("xml", "version=\"1.0\" encoding=\"UTF-8\"");
document.appendChild(header);

Now move to root node with tagname properties. Create a QDomElement object and append it to root.

QDomElement root = document.createElement("properties");
document.appendChild(root);

Then we will add child nodes under properties tag. Most people might think that the following line would add child nodes under root.

QDomNode child;
root.appendChild(child);

NO! That is wrong. You must create every node or elements using the reference of document. Just keep in mind that there might be more than one documents in codes. So you should not mess with parents and childs. The following lines will generate child nodes under properties tag.

QDomElement node = document.createElement("node");
root.appendChild(node);

Did you spot the difference? You have to use document.createElement, not root.appendChild method. As we have a list of strings, so we can use a loop to create all nodes and set values to them.

for(int i=0;i<attributeValues.size();i++)
{
    QDomElement node = document.createElement("node");
    node.setAttribute("type",attributeValues.at(i));
    root.appendChild(node);

    QDomText value = document.createTextNode(nodeValues.at(i));
    node.appendChild(value);
}

The code looks pretty clear. Create an element, set its attribute and append it to root. The 2nd block is used to set the value of that node.

To write the XML into file, open a QFile object, open it and flush the document onto it.

QFile xml("new-xml-file.xml");
xml.open(QIODevice::WriteOnly);
xml.write(document.toByteArray());
xml.close();

Here is the output.

<?xml version="1.0" encoding="UTF-8"?>
<properties>
 <node type="user">minhaz</node>
 <node type="os">linux</node>
 <node type="version">3.2.0-37</node>
</properties>

That is enough for basic XML r/w actions. If you want to learn more or go deeper, run Qt Assistant. They have really awesome and detailed documents out there.

时间: 2024-09-30 20:03:14

Some words about Qt DOM Node, Element and Attribut的相关文章

【转载】跟随 Web 标准探究DOM -- Node 与 Element 的遍历

跟随 Web 标准探究DOM -- Node 与 Element 的遍历 这个是 Joyee 2014年更新的,可能是转战github缘故,一年多没有跟新了.这篇感觉还挺全面,就转载过来,如以前文章一样,下面附上转载地址: 跟随 Web 标准探究DOM -- Node 与 Element 的遍历[Joyee] 感觉有点敷衍... 完...

Raphael.js API 之Element.remove(),Element.removeData(),paper.text(),Element.node(),Element.onDragOver

/*API-38*/ Element.remove() 删除某个元素对象,无返回值 /*API-39*/ Element.removeData([key]); 删除某个key的value值,如果没有特殊说明则删除所有的元素数据 参数列表: key 可选参数 字符串类型 key 返回值:元素对象 /*API-105*/ 在画布上添加一个字符串,如果需要换行,使用'\n' 参数列表: x number类型 x轴坐标位置 y number类型 y轴坐标 text 字符串类型 文本内容 返回值:type

centos安装 php时 出现 make: *** [ext/dom/node.lo] Error

Linux安装PHP ,make 的时候报错: make: *** [ext/dom/node.lo] Error 1 解决办法: $ curl -o php-5.2.17.patch https://mail.gnome.org/archives/xml/2012-August/txtbgxGXAvz4N.txt $ tar jxf php-5.2.17.tar.bz2  $ cd php-5.2.17 $ patch -p0 -b <../php-5.2.17.patch    patchi

org.w3c.dom.Node.getTextContent()方法编译错误-已解决

org.w3c.dom.Node.getTextContent()方法编译错误. 在项目的Java Build Path | Order and Export选项卡中,将JRE System Library选中,并Top置顶.然后再进行编译即可. 参考: https://blog.csdn.net/maoxiao1229/article/details/51694553 原文地址:https://www.cnblogs.com/wrong/p/10441783.html

js DOM Node类型

DOM(文档对象模型)是针对HTML和XML文档的一个API. DOM可以将任何HTML或XML文档描绘成一个由多层节点构成的.以特定节点为根节点的树形结构.节点分为12种不同的类型,每种类型分别表示文档中的不同信息及(或)标记.这些类型都继承自一个基类型. 以下面为例: <html> <!--文档元素,文档节点的子节点,是文档的最外层元素.每个文档只有一个文档元素.--> <head> <title>My article</title> <

JavaScript DOM(三) Element 类型

简介: 在JavaScript中除了document类型之外,Element类型就要算是Web编程中最常用的类型了.Element类型用于表现XML或者HTML元素,提供了对元素标签名,子节点及特性的访问: 特征: 1.nodeType的值为1; 2.nodeName的值为元素的标签名: 3.nodeValue的值为null; 4.parentNode可能是document或Element; 5.其子节点可能是Element,Text,Coment,ProcessingInstruction,C

[转载]Extjs中的dom,Ext.Element和Ext.Component对象的关系

原文地址:http://www.cnblogs.com/lwzz/archive/2011/01/30/1948106.html   Ext.Element对象是对dom对象的封装,目的是为了跨平台以及增加一些有用的方法.但是Ext.Element是不包含外观的,封装的dom原来是怎么样就是怎么样.开发中最好有现成的组件可以使用,否则Extjs和Jquery差别真的不大了.因此Ext在Element的基础上进一步封装,产生了Component类,这些类含有外观,也就是多加入了一些html之类的进

VueCli3.0全栈项目-资金管理系统带权限(node/element/vue)

课程简介:通过本系列课程,可以快速的掌握全栈开发流程, 包括node.js的接口搭建, vue前端项目的构建, element-ui视图的构建. 一套应有尽有的课程! 课程目录:1.Vue全栈-最终成果展示.mp42.Node接口搭建-express搭建服务器.mp43.Node接口搭建-连接MongoDB数据库.mp44.Node接口搭建-搭建路由和数据模型.mp45.Node接口搭建-搭建注册接口并存储数据.mp46.Node接口搭建-使用全球公认头像gravatar.mp47.Node接口

Qt Dom方式写xml(二)

struct PUBLIC_DATA_SEND {     bool ifrealtimedata;     bool ifdatasend;     bool confirmflag;     int sendtimes;     int sendtime;     QList<QString> waitconfirmterminal;     QList<QString> DateTimeList;     char capturetime[20]; }SEND; int pa