| |
|
 |
Parsing XML with QT's DOM classes
|
Accessing document information
Now you know how to navigate a document using DOM nodes, so now you may want to actually access the information. Here's how:
First, traverse the document using the tree traversing algorithm of your choice. When you reach the node you want to have a closer look at, you could access it like in this example:
#include <qdom.h>
#include <qstring.h>
// Node is a QDomNode which refers to the node we are looking at
QString NodeName = Node.nodeName();
QString NodeValue = Node.nodeValue();
// this gives you the value of the attribute named "src"
QString srcValue = Node.attributes().nameItem( "src" ).nodeValue()
// this gives you the value of the second attribute, since numbers start with 0
QString srcValue = Node.attributes().item( 1 ).nodeValue()
|
This is a very simple example. In a real world application you'd probably want to check which node type you are dealing with (e.g. use QDomNode::isElement() to check whether it is an element node), then convert it to that type (e.g. use QDomNode::toElement() to convert it) and finally use the special functions related to that node type. For example, the QDomElement class has a function setTagName(QString) that allows you to change the name of an element (which means changing the tag name). The QDomText class has no such function, since the name of a text node always is "#text".
An important note: When you need to create new nodes, use QDomDocument's createElement(), createTextNode(), createComment() etc. They will create nodes that has ownerDocument() set correctly, and can be inserted directly into the DOM tree using QDomNode functions such as insertBefore() and appendChild().
After you have edited the document, you might want to save it back to disk. These few lines of code will do the trick:
#include <qdom.h>
#include <qtextstream.h>
// File is a pointer to a valid QFile object,
// Doc is a pointer to the QDomDocument
if ( File->open( IO_WriteOnly ) )
{
QTextStream stream( File );
stream << Doc->toString();
File->close();
}
|
I hope this is enough information to get you started. Later I might publish a simple xml tree viewer/editor I've been working on. It's not a very useful application, but it demonstrates the DOM principles quite well. Stay tuned!
Suggested reading
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
|
|
> 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
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: "<foo/>", and not "<foo>".
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.
|
|
 |
|
|