Latest

Archive

Community news

C++

Communities and Content

Databases

Editorials

Emacs

General

HTML

Java

Notices

PHP

XML

Apache

C++

Database

General

HTML

Java

Javascript

Linux

Object oriented programming

Open source

Perl

PHP

Python

Ruby

SOAP

XML

Suggest a link

Advertise on zez

Contribute

Contact us

About zez


Parsing XML with QT's DOM classes



The Document Object Model (DOM) by the World Wide Web Consortium specifies a simple way of interacting with various document formats, including XML. Trolltech's Qt implements some very handy DOM classes. In this tutorial I'll demonstrate the basic principles of DOM, and give some pointers on how to use the Qt DOM classes.

DOM introduction


The Document Object Model (DOM) by the World Wide Web Consortium is an object-oriented approach to document handling. It breaks up the document into a tree of nodes, each node with a set of attributes. We start with a simple xml document:

<xml>
  <smith>
    <john/>
    <susan/>
  </smith>
  <throckmorton>
    <baldrick/>
    <evangeline/>
    <waldemar/>
  </throckmorton>
</xml>



A simple DOM tree

When this document is parsed using a DOM-compliant parser, you get a DOM tree as shown to the right. Each circle represents a node, and the arrows shows the relations between them. There are different types of nodes, the most common are element nodes. (All the nodes in this first example are element nodes.) More about the different types of nodes later.

The node named "xml" is the first node of the document and is called the document element. The nodes "smith" and "throckmorton" are children of "xml". "xml" is the parent of "smith" and "throckmorton". And, logically, "smith" and "throckmorton" are siblings. Using this concept, we can navigate the document as follows:

#include <qdom.h>

QDomDocument doc( "myDocument" );
doc.setContent( &myFile );                        // myFile is a QFile

QDomElement docElement = doc.documentElement();   // docElement now refers to the node "xml"
QDomNode node;

node = docElement.firstChild();                   // node now refers to the node "smith"
node = node.firstChild();                         // node now refers to the node "john"
node = node.parentNode();                         // node now refers to "smith" again
node = node.nextSibling();                        // node now refers to "throckmorton"
node = node.firstChild().nextSibling();           // node now refers to "evangeline"


As you can see, functions you'll be using frequently are firstChild(), lastChild(), parentNode(), nextSibling() and previousSibling(). There are also functions for accessing the DTD (Document Type Declaration) of a document.


| < 1 > | 2 | 3 | Next page >> | Printer-friendly page |

Comment List


Topic: Author:
Time:
base64 help sarin n 28.05.2002 09:20

hello sir
i am a student and i have a project module to encode an image file into an xml and decode it and generate
an image file afetr decoding and they have asked me to use jaxp and only using dom not any SAX parser i am
working in java if you have the source code and if you can help me in getting it .it will be a great help can any one help me just mail me at sarin_n21@yahoo.com


XML at start isn't well-formed Mike Moran 28.01.2001 15:03

It's a minor thing, but I thought I'd mention that the childless elements
in the example should look like this: "", and not "".
See: http://www.w3.org/TR/2000/REC-xml-20001006#sec-starttags


   RE: XML at start isn't well-formed Mike Moran 28.01.2001 15:09

&gt; It's a minor thing, but I thought I'd mention that the childless elements
&gt; in the example should look like this: "", and not "".
&gt; See: http://www.w3.org/TR/2000/REC-xml-20001006#sec-starttags

Looks like the XML example got munged. Maybe this will work:

"It's a minor thing, but I thought I'd mention that the childless elements
in the example should look like this: "&lt;foo/&gt;", and not "&lt;foo&gt;".
See: http://www.w3.org/TR/2000/REC-xml-20001006#sec-starttags


     RE: XML at start isn\'t well-formed Gunnstein Lye 20.08.2001 20:10

Fixed, thanks.




Forgot your password?

Register a new user

Results

Polls