qt dom读取xml

          今天我写一个聊天软件的个人的基本设置,我本来认为用sax来解析的,但是我后来发现那个xml只有几十k,更本没有必要用sax来解析。

          

The QDomDocument class represents an XML document.

The QDomDocument class represents the entire XML document. Conceptually, it is the root of the document tree, and provides the primary access to the document's data.

Since elements, text nodes, comments, processing instructions, etc., cannot exist outside the context of a document, the document class also contains the factory functions needed to create these objects. The node objects created have an ownerDocument() function which associates them with the document within whose context they were created. The DOM classes that will be used most often are QDomNode, QDomDocument, QDomElement and QDomText.

The parsed XML is represented internally by a tree of objects that can be accessed using the various QDom classes. All QDom classes onlyreference objects in the internal tree. The internal objects in the DOM tree will get deleted once the last QDom object referencing them and the QDomDocument itself are deleted.

Creation of elements, text nodes, etc. is done using the various factory functions provided in this class. Using the default constructors of the QDom classes will only result in empty objects that cannot be manipulated or inserted into the Document. 

The QDomDocument class has several functions for creating document data, for example, createElement(), createTextNode(),createComment(), createCDATASection(), createProcessingInstruction(), createAttribute() and createEntityReference(). Some of these functions have versions that support namespaces, i.e. createElementNS() and createAttributeNS(). The createDocumentFragment() function is used to hold parts of the document; this is useful for manipulating for complex documents.

The entire content of the document is set with setContent(). This function parses the string it is passed as an XML document and creates the DOM tree that represents the document. The root element is available using documentElement(). The textual representation of the document can be obtained using toString().

     这是官方的文档,QDomDocment这个类代表了xml的文档。

     那个QDomDocument这个类带来整个xml的文档,概念的讲,它是文档树的跟,提供了主要访问文档的数据的途径。

    因为元素,节点,注释不能存在与外部的文档内容,那个文档类也存在创造别的类的函数,那个节点类能。那个dom类经常使用 QDomNode, QDomDocument, QDomElement and QDomText.类。

 is possible to insert a node from another document into the document using importNode().

    解析xml代表整个整个树对象,所有的QDom类只参考在内部树。一旦QDom参考他们的对象和 QDomDocument 删除时,QDom树就被删除!

    创造了元素,节点等等,用各种各样的函数去创造,用默认的QDom构造函数,只会产生一个空的对象,不能操纵和插入Document. 中。

   QDomDocument类有好几个函数去创造文档数据,。。。。。。一些类。

    整个文档内容设置用setContent(). 那个函数解析字符,通过xml的文档解析那个字符,创造dom树代表那个文旦。通过用documentElement(). 函数得到树根元素。用toString().可以得到文本内容。

    当然还要讲解一下,QDomNode,一个节点类。

The QDomNode class is the base class for all the nodes in a DOM tree.

Many functions in the DOM return a QDomNode.

You can find out the type of a node using isAttr(), isCDATASection(), isDocumentFragment(), isDocument(), isDocumentType(), isElement(),isEntityReference(), isText(), isEntity(), isNotation(), isProcessingInstruction(), isCharacterData() and isComment().

A QDomNode can be converted into one of its subclasses using toAttr(), toCDATASection(), toDocumentFragment(), toDocument(),toDocumentType(), toElement(), toEntityReference(), toText(), toEntity(), toNotation(), toProcessingInstruction(), toCharacterData() ortoComment(). You can convert a node to a null node with clear().

Copies of the QDomNode class share their data using explicit sharing. This means that modifying one node will change all copies. This is especially useful in combination with functions which return a QDomNode, e.g. firstChild(). You can make an independent (deep) copy of the node with cloneNode().

A QDomNode can be null, much like a null pointer. Creating a copy of a null node results in another null node. It is not possible to modify a null node, but it is possible to assign another, possibly non-null node to it. In this case, the copy of the null node will remain null. You can check if a QDomNode is null by calling isNull(). The empty constructor of a QDomNode (or any of the derived classes) creates a null node.

Nodes are inserted with insertBefore(), insertAfter() or appendChild(). You can replace one node with another using replaceChild() and remove a node with removeChild().

To traverse nodes use firstChild() to get a node's first child (if any), and nextSibling() to traverse. QDomNode also provides lastChild(),previousSibling() and parentNode(). To find the first child node with a particular node name use namedItem().

To find out if a node has children use hasChildNodes() and to get a list of all of a node's children use childNodes().

The node's name and value (the meaning of which varies depending on its type) is returned by nodeName() and nodeValue() respectively. The node's type is returned by nodeType(). The node's value can be set with setNodeValue().

QDomNode类在QDom树中所有节点的基类,你可以找到node的类型,用。。。。。。一些类

   复制QDomNode 类分享他们的数据用明确的分享,

一个QDomNode为空的,就好像一个空的指针。创造一个空的节点导致另外一个空的节点。它不可能修改空的指针,但是可能分配另外一个,

  节点插入用insertBefore(), insertAfter() or appendChild(),

  去遍历节点用firstChild()去得到第一个节点,然后用 nextSibling() 去遍历,

   在这里我说一下,元素与节点的区别,节点可分为element,atrribute,processinginstruction,comment,text,cdataaction,namespace等等把,也就说节点与元素是不同的概念!

  Adjacent QDomText nodes can be merged into a single node with normalize().

  临近的QDomText 节点可以合并到一个简单的节点

  QDomElement nodes have attributes which can be retrieved with attributes().

  元素节点有可以有属性从attributes()中得到。

  QDomElement and QDomAttr nodes can have namespaces which can be retrieved with namespaceURI(). Their local name is retrieved

 withlocalName(), and their prefix with prefix(). The prefix can be set with setPrefix().

  

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值