Extracting text from QDomNodes
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:
Code:
<?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:
Code:
QString currentTitle, currentTemplate;
int errorLine, errorCol;
int numTemplates, t;
// Define templateFile; omitted
if( QFile::exists( templateFile
) ) { 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:
Code:
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?
Re: Extracting text from QDomNodes
How about:
Code:
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()));
}
Re: Extracting text from QDomNodes
The docs say:
Quote:
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:
Code:
titles.item( t ).toElement().text();
Re: Extracting text from QDomNodes
Thanks an awful lot, I am not all that familiar with DOM and did not think of that. It works now.