PDA

View Full Version : Extracting text from QDomNodes



Matt Smith
25th February 2007, 19:33
Hi everyone,

I'm trying to set up a DOM-based parser to extract blog post templates for an application I'm writing. The XML format I'm using goes like this:


<?xml version="1.0"?>
<QuickpostTemplates>
<Template>
<title>Title to be displayed in menu</title>
<templateString>The actual template</templateString>
</Template>
[and as many more templates as the user requires]
</QuickpostTemplates>

This is the function I've been using to extract the title and template strings from the XML:


QString templateFile;
QString errorString;
QString currentTitle, currentTemplate;
int errorLine, errorCol;
QDomNodeList titles, templateStrings;
int numTemplates, t;
QTextStream ts( stdout );

// Define templateFile; omitted

if( QFile::exists( templateFile ) ) {
QDomDocument domDoc( "quickpostTemplates" );
QFile file( templateFile );
if( !domDoc.setContent( &file, true, &errorString, &errorLine, &errorCol ) )
[Warning message box]
else {
titles = domDoc.elementsByTagName( "title" );
templateStrings = domDoc.elementsByTagName( "templateString" );

if( titles.size() ) {
quickpostTemplateActions.clear();
numTemplates = ( titles.size() >= templateStrings.size() ?
titles.size() : templateStrings.size() );
qDebug( "Populating template menu" );
for( t = 0; t < numTemplates; t++ ) {
currentTitle = titles.item( t ).nodeValue();
currentTemplate = templateStrings.item( t ).nodeValue();
ts << titles.item( t ).nodeValue();
ts << templateStrings.item( t ).nodeValue();
quickpostTemplateActions.append( new QuickpostTemplate( t, currentTitle, currentTemplate, 0 ) );
templateMenu->addAction( quickpostTemplateActions[t] );
}
}
else
qDebug( "No templates found." );
}
}

The titles and template strings are stored in two separate QLists of QDomNodes, and the loop steps through both to extract the strings from each list in turn. The problem is that there are two methods for extracting the text from the nodes: either nodeName.nodeValue() or nodeName.toText().data(), and neither or them seem to work for me. They both keep extracting blank text, and the result is that the templateMenu (QMenu object) is populated by actions identified by blank strings.

If I replaced the lines beginning with "ts <<" with the following:


ts << titles.item( t );
ts << templateStrings.item( t );

then the entire element, opening and closing tags and all, are sent to standard output.

Can anyone see what I'm doing wrong?

wysota
25th February 2007, 19:43
How about:

QDomDocument doc = ...
QDomElement root = doc.documentElement();
for(QDomElement elem = root.firstChildElement("Template");
!elem.isNull();
elem = elem.nextSiblingElement("Template")){
qDebug("TITLE: %s", qPrintable(elem.firstChildElement("Title").text());
qDebug("CONTENT: %s", qPrintable(elem.firstChildElement("templateString").text()));
}

jacek
25th February 2007, 19:48
The docs say:
QString QDomNode::nodeValue () const
Returns the value of the node.
The meaning of the value depends on the subclass:
(...)
All the other subclasses do not have a node value and will return an empty string.
Unfortunately QDomElement falls into "other subclasses" category.

Try:
titles.item( t ).toElement().text();

Matt Smith
25th February 2007, 20:27
Thanks an awful lot, I am not all that familiar with DOM and did not think of that. It works now.