Using DOM in QT to parse XML (Summary)

Source: Internet
Author: User
Using Dom to parse XML in QT (Summary)

A few days ago, due to a change in the underlying topology, the topology file was changed. The parsing operations on the topology file I was responsible for would naturally have to be re-initiated. I used the last weekend to complete this task. By the way, I also used this time to summarize the learned knowledge.

We know that there are only two ways to read and write XML files in QT: Dom and sax. The two methods are described as follows:

/*************************************** I am the legendary line of separation *********************************** ***************************/

The standard for reading and operating XML files is the Document Object Model Dom.
Dom defines a set of interfaces for the parsed versions of the XML document. The parser reads the entire document and constructs a tree with resident memory. Then, your code can use the DOM interface to operate on this tree structure. You can traverse the tree to understand what the original document contains. You can delete several sections of the tree, arrange the tree again, and add new branches.
Unfortunately, the DOM method involves reading the entire file and storing the file in a tree structure, which may be inefficient, slow, and resource-consuming:
Dom constructs a tree where the entire document resides in the memory. If the file size is large, a large amount of memory is required.
Dom creation indicates the object of each item in the original document, including elements, text, attributes, and spaces. If you only need to pay attention to a small part of the original document, it is extremely waste to create objects that will never be used.
The Dom parser must read the entire document before your code gets control. This can cause significant latency for very large documents.
These are only problems caused by the design of the Document Object Model. Apart from these problems, dom api is a very useful method for parsing XML documents.

An alternative technology is sax. Compared with the Document Object Model Dom, Sax is a faster and lighter way to read and operate XML data. Sax allows you to process a document while reading it, so that you do not have to wait until the entire document is stored. It does not involve the overhead and concept jumps required by Dom.

The sax API is an event-based API that is suitable for processing data streams, that is, processing data sequentially as data flows.
You will be notified when a certain event occurs when the sax API parses your document. When you reply to the message, the data you do not save will be discarded.

/*************************************** I'm a gorgeous splitting line *********************************** ***********************************/

We can see that if the XML file to be read is not very large, it is very convenient to use the DOM reading method. Because I am also using the DOM tree reading method, this article mainly introduces dom-based reading.

Based on common operations, I simply divide XML operations into the following types:

1. First, perform operations on the XML file to open the file.

This is actually an operation on the file. It can be directly defined as a constructor and completed during object initialization.

Topodatareader: topodatareader (const wstring & filepath): _ filepath (filepath), _ qdomdoc ("mydocument"), _ qfile (qstring: fromstdwstring (filepath ))
{
If (! _ Qfile. Open (qiodevice: readonly ))
{
Return;
}

If (! _ Qdomdoc. setcontent (& _ qfile ))
{
_ Qfile. Close ();
Return;
}
}

 

2. Read the node content in the XML file

Assume that the XML file format is as follows:

(1)

<Switchs>

<Switch snmpip = 211.87.235.136 newwork = front>

</Switch>

</Switchs>

(2)

<Ip> 211.87.235.136 </IP>

In the first case, use the following method:

Qdomelement docelem = _qdomdoc.doc umentelement ();

Qdomnode nodeswitch = docelem. elementsbytagname ("Switch"); // The label name is red.

Qdomelement elemnodeswitch = nodeswitch. toelement ();

String snmpip = qprintable (elemnodeswitch. Attribute ("snmpip"); // The same is true for network.

In the second case, use the following method:

You can directly call the text () API.

String IP = qprintable (elementnodeip. Text ());

However, assume that there are multiple identical nodes in the file, as shown below:

<A>

<B> </B>

<B> </B>

</A>

In this way, elementsbytagname ("B") returns a childnodes () instead of a separate node.

We can use the at () method for specific positioning.

In addition, we can use this method to obtain the node value. Assume that the XML file is as follows:

-+ <Switch snmpip ="192.168.120.htm"Network ="Front"><Name>Front-End primary Switch</Name><Description/>-<Iplist><Ip>192.168.120.htm</IP></Iplist><Rwcommunity>Public 120</Rwcommunity><Workmode>True</Workmode><Workstatus>True</Workstatus><Enablealarm>True</Enablealarm><Snmpcount>0</Snmpcount><Memoryutilizationratio>50.0</Memoryutilizationratio><Cpuutilizationratio>50.0</Cpuutilizationratio><Port>161</Port><Snmpstatus>True</Snmpstatus><Privatename>CZ-5_FA</Privatename><Switchindex>Topleft</Switchindex></Switch>

We can first obtain the switch node and then obtain its childnodes (), so we can use the at () method to obtain each of the following nodes (Note: Starting from 0)

For example, the IP node above is nodeswitch. childnodes (). At (3 ).

To do this well, you don't have to worry about repetition. As long as you confirm that the switch node is correct, the bottom part will certainly be OK.

3. Return the number of subnodes under a node.

This is simple.

Qdomelement docelem = _qdomdoc.doc umentelement ();
Qdomnode nodetagname = docelem. elementsbytagname (tagname). At (0); // If there are multiple tagnames, select the first one.
Int num = nodetagname. childnodes (). Size ();

Okay, that's all you need to remember. I will try again later.

 

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.