StupidBeauty
Read times:1016Posted at:Sat Mar 9 02:21:13 2013
- no title specified

Qt4.8文档翻译:QDomElement类参考,QDomElement Class Reference

详细说明

QDomElement类表示的是一棵文档对象模型(DOM)树中的一个元素。

元素拥有 一个标签名( tagName ())和0或多个与之关联的属性。 可使用 setTagName ()来改变标签名。

元素 的属性是由 QDomAttr 对象来表示的, 可使用 attribute ()和 attributeNode ()函数 来查询各个属性。 妳可使用 setAttribute ()和 setAttributeNode ()函数来设置各个属性。属性 可使用 removeAttribute ()删除。 这些函数都有对应的 命名空间 (namespace-aware) 版本 ,也就是说 setAttributeNS () setAttributeNodeNS () removeAttributeNS ()

如果 妳想访问某个节点的文本内容的话,使用 text (),例如:

QDomElement e = //...

//...

QString s = e. text ()

text ()函数 会递归 地操作以便找到文本内容(因为 并不是所有元素都包含文本内容的 )。如果 妳想找到某个节点 的所有子代节点中所包含的文本内容的话, 就遍历那 子代内容,寻找 QDomText 节点,例如

QString text;

QDomElement element = doc.documentElement();

for( QDomNode n = element.firstChild(); !n.isNull(); n = n.nextSibling())

{

QDomText t = n.toText();

if (!t.isNull())

text += t.data();

}

注意 我们尝试着 将每个节点都转换成文本节点 ,并且使用 text ()函数 而不是在节点上直接使用 firstChild (). toText ().data() 或n. toText ().data()函数,因为对应 的节点不一定是一个文本元素。

妳可使用 elementsByTagName () elementsByTagNameNS ()函数来获取某个元素 中拥有特定标签 名的所有子代元素组成 的列表。

要想浏览某个文档对象模型文档中的元素,则使用 firstChildElement () lastChildElement () nextSiblingElement () previousSiblingElement ()。例如, 要想 在一个名为"database"的 根元素中遍历所有名为"entry" 的子代元素的话,可这样写:

QDomDocument doc = // ...

QDomElement root = doc. firstChildElement ("database");

QDomElement elt = root. firstChildElement ("entry");

for (; !elt.isNull(); elt = elt.nextSiblingElement("entry")) {

// ...

}

要了解更多关于文档对象模型的信息的话,参考 1 2 级核心部分 要想高屋建瓴 了解 QT 中的文档对象模型实现 的话,参考 QDomDocument 文档

唐宁

Your opinions
Your name:Email:Website url:Opinion content:
- no title specified

HxLauncher: Launch Android applications by voice commands

 
Recent comments
2017年4月~2019年4月垃圾短信排行榜Posted at:Thu Sep 26 04:51:48 2024
Qt5.7文档翻译:QWebEngineCookieStore类,QWebEngineCookieStore ClassPosted at:Fri Aug 11 06:50:35 2023盲盒kill -9 18289 Grebe.20230517.211749.552.mp4