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>
<?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>
To copy to clipboard, switch view to plain text mode
This is the function I've been using to extract the title and template strings from the XML:
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." );
}
}
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." );
}
}
To copy to clipboard, switch view to plain text mode
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 );
ts << titles.item( t );
ts << templateStrings.item( t );
To copy to clipboard, switch view to plain text mode
then the entire element, opening and closing tags and all, are sent to standard output.
Can anyone see what I'm doing wrong?
Bookmarks