Use the qdomdocument class in QT to process XML files

Source: Internet
Author: User

XML, fully known as the extensible markup language, is a very convenient data exchange tool. Currently, the output format of the blog subscription system is the RSS format that meets the XML specification, and the SVG vector image also uses the XML format.

After obtaining an XML file, we need to perform Syntactic Analysis to extract the information provided by the publisher. Qt provides good support for XML, including Dom implementation and sax implementation.

Dom
Yes Document Object
The short name of model is to load the entire document into the memory as an object for processing. Then, developers can access every node in the object, and each node corresponds to one of the XML files.
Tags. The advantage of this method is that the operation is very simple, but the disadvantage is that the entire file needs to be put into the memory, not suitable for processing large files. In this way, the Implementation class in QT is qdomdocument.
This article will explain. RSS files represented by blogs are not very large. This method is suitable for processing.

Sax
Simple API
XML abbreviation, which is implemented by reading documents to the memory in stages. When a tag or other stage is encountered, the developer can call the previously designed callback function for processing. The disadvantage of this method is that development is required.
Write the callback function to process different tags,CodeComplex. The advantage is that it can process large XML files. In this way, the Implementation class in QT is called qxmlstreamreader.

The following section describes the code.

(1) If you need to use qdomdocument, add "QT + = xml" to your project. Pro file, otherwise it will not be compiled.

2)

Qdomdocument Doc;

Qfile file ("RSS. xml ");

Qstring errorstr;

Int errorline;

Int errorcol;

// Setcontent specifies the specified content for qdomdocument resolution. The first parameter can be qbytearray or file name.

If (! Doc. setcontent (& file, true, & errorstr, & errorline, & errorcol ))

{

// If an error occurs, it will enter here. Error description obtained by errorstr

// Errorline and errorcol indicate the rows and columns with errors.

}

If there is no error in the above steps, congratulations, you have obtained a complete qdomdocument object Doc, you only need to access the subnode or subnode of the doc to get all the marked content in the XML.

3)

You can use Doc. childnodes () to obtain the qdomnodelist list of all subnodes of Doc. For example

Qdomnodelist list = Doc. childnodes ();

For (INT I = 0; I <list. Count (); I ++ ){

Qdomnode node = List. at (I); // a good style is of course to write the definition out :(

// Qdebug () <"node name is" <node. nodename ();

// Qdebug () <"node type is" <. nodetype ();

}

Connect
You can know the name and type of each node through the preceding method. The node name is the name of the mark. It is easy to understand the XML file. The qdomnode class is a parent
Class, qdomdocument is a subclass of qdomnode, because most qdomnode types are qdomdocument. You can use
The todocument () function converts the qdomnode type to qdomdocument.

Qstring text = node. toelement (). Text ();

The preceding statement can extract the text in a node, that is, the text part in the mark. The encoding obtained by using this function is already in unicode format and does not need to be converted.

 

I have read it. Visit my website www.xiabanl.com for my support. I'm off duty.Community

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.