QtXml:QDomText、QDomProcessingInstruction、QDomDocumentFragment、QDomAttr

这几个类比较简单,仅展示值得一说的函数或用法。

QDomText

1、QDomText splitText(int offset)

将此 DOM 文本对象拆分为两个 QDomText 对象。 这个对象保留它的第一个偏移字符,第二个(新创建的)对象被插入到这个对象之后的文档树中,并带有剩余的字符。

该函数返回新创建的对象。

 

    QDomElement docElem = doc.documentElement();
    qDebug()<<docElem.text();//helloWorld
    QDomText textNode = docElem.firstChild().toText();
    QDomText splitNode = textNode.splitText(5);
    docElem.insertBefore(splitNode,textNode);

QDomProcessingInstruction

xml的处理指令。例:

    QDomDocument doc;
    QDomProcessingInstruction stateInstruct =  doc.createProcessingInstruction("xml", "version=\"1.0\" encoding=\"UTF-8\"");
    QDomProcessingInstruction dealInstruct =  doc.createProcessingInstruction("xml-stylesheet", "type=\"text/css\"  href=\"style.css\"");
    doc.appendChild(stateInstruct);
    doc.appendChild(dealInstruct);

    QDomElement root = doc.createElement("MyML");
    doc.appendChild(root);

    QDomElement tag = doc.createElement("Greeting");
    root.appendChild(tag);

    QDomText t = doc.createTextNode("Hello World");
    tag.appendChild(t);

    QFile file("demo.xml");
    file.open(QIODevice::WriteOnly | QIODevice::Truncate | QIODevice::Text);
    QTextStream out(&file);
    out.setCodec("UTF-8");
    doc.save(out, 4, QDomNode::EncodingFromTextStream);
    file.close();

QDomDocumentFragment

QDomDocumentFragment用作存储节点组,可以将多个节点添加到QDomDocumentFragment对象,然后将QDomDocumentFragment对象添加到文档。

QDomElement和QDomDocumentFragment都可以作为节点的容器,但QDomElement要设置标签。节点添加到QDomElement和QDomDocumentFragment的比较:

    QDomElement node = doc.createElement("xxxx");

    QDomElement tag = doc.createElement("Greeting");
    node .appendChild(tag);

    QDomText t = doc.createTextNode("Hello World");
    tag.appendChild(t);

    tag = doc.createElement("Greeting2");
    node .appendChild(tag);

    t = doc.createTextNode("Hello World2");
    tag.appendChild(t);

    root.appendChild(node);

    QDomDocumentFragment fragmentNode = doc.createDocumentFragment();

    QDomElement tag = doc.createElement("Greeting");
    fragmentNode.appendChild(tag);

    QDomText t = doc.createTextNode("Hello World");
    tag.appendChild(t);

    tag = doc.createElement("Greeting2");
    fragmentNode.appendChild(tag);

    t = doc.createTextNode("Hello World2");
    tag.appendChild(t);

    root.appendChild(fragmentNode);

 

QDomAttr

一、描述

QDomAttr是属性元素节点。

以下 XML 片段生成一个没有子元素但有两个属性的元素:

 <link href="http://qt-project.org" color="red" />

 可以使用如下代码访问元素的属性:

 QDomElement e = //...
 //...
 QDomAttr a = e.attributeNode("href");
 cout << a.value() << Qt::endl;                // "http://qt-project.org"
 a.setValue("http://qt-project.org/doc");      // change the node's attribute
 QDomAttr a2 = e.attributeNode("href");
 cout << a2.value() << Qt::endl;               // "http://qt-project.org/doc"

二、成员函数

1、QDomElement ownerElement()

返回此属性附加到的元素节点。

2、bool specified()

是否已使用 setValue() 设置了该属性。

  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值