QDomDocument
以下是我使用XPath 表达式在 a 中搜索节点的实用函数。它使用QDomNodeModel
@Alejandro 建议的类,可从https://adared.ch/qdomnodemodel-qxmlquery下载。它基于https://www.qtcentre.org/threads/37645-QAbstractXmlNodeModel-implementation-QDomNodeModel-QXmlQuery的使用示例。感谢 Stanislaw Adaszewski,他提供了QDomNodeModel
课程和使用示例。
其中有一些方法QDomNodeModel
被注释为未实现。但是,对于我需要搜索的简单 XML 内容,QDomNodeModel
按原样就足够了。
//
/// @brief Search for nodes in a QDomDocument using an XPath.
/// @note I cannot return a QDomNodeList, because it has no public methods for adding items to it.
/// @param[in] doc The document to search.
/// @param[in] fromNode The node in the document to start searching from.
/// e.g., to search the whole document, use <code>doc.documentElement()</code>.
/// @param[in] xpath The XPath expression.
/// @return A list of found nodes.
//
QList<QDomNode> findNodes( QDomDocument const & doc, QDomNode const & fromNode, QString const & xpath )
{
qDebug( "%s", __FUNCTION__ );
QList<QDomNode> foundNodes;
//------------------------------
// The name pool that everybody shares.
QXmlNamePool pool;
//------------------------------
// The model that wraps the document.
QDomNodeModel model( pool, doc );
//------------------------------
// The query.
// XQuery10 means the default XQuery 1.0 language, as opposed to XSLT20.
QXmlQuery query( /*QXmlQuery::XQuery10,*/ pool );
// Operate on the given node.
QXmlNodeModelIndex fromIndex = model.fromDomNode( fromNode );
query.setFocus( QXmlItem( fromIndex ) );
// The query statement.
query.setQuery( xpath );
if ( !query.isValid() )
{
qDebug( "Query is not valid" );
return foundNodes;
}
//------------------------------
// The destination for the result of the query.
QXmlResultItems result;
//------------------------------
// Evaluate the query.
query.evaluateTo( &result );
if ( result.hasError() )
{
qDebug( "Query evaluation failed" );
return foundNodes;
}
//------------------------------
// The result of the query.
qDebug( "Query result:" );
while ( !result.next().isNull() )
{
QXmlNodeModelIndex index = result.current().toNodeModelIndex();
QDomNode node = model.toDomNode( index );
qDebug( " %d %s: %s", node.nodeType(), qPrintable( node.nodeName() ), qPrintable( node.nodeValue() ) );
foundNodes << node;
}
return foundNodes;
}
在我的应用程序中,我加载了一个 XML 文件,并使用上述实用程序函数来搜索它。
//------------------------------
// The path of the XML file.
QString path = "settings.xml";
//------------------------------
// Open the file.
QFile file( path );
if ( !file.open( QIODevice::ReadOnly ) )
{
qDebug( "Failed to open '%s': %s", qPrintable( path ), qPrintable( file.errorString() ) );
return;
}
//------------------------------
// Load the file into a document.
QDomDocument doc;
QString error;
int line;
int column;
if ( !doc.setContent( &file, &error, &line, &column ) )
{
qDebug( "%s(%d,%d): %s", qPrintable( path ), line, column, qPrintable( error ) );
return;
}
//------------------------------
// The document root element.
QDomElement rootElem = doc.documentElement();
//------------------------------
// Search for an element whose name attribute has a certain value.
QString name = "Alice";
QString xpath = QString( "setting[@name='%1']" ).arg( name );
QList<QDomNode> foundNodes = findNodes( doc, rootElem, xpath );
//------------------------------
// Did I find it?
if ( foundNodes.size() > 0 )
{
QDomElement foundElem = foundNodes.at( 0 ).toElement();
// Do something with that element.
...
}
要搜索的示例 XML 内容。
<?xml version='1.0'?>
<settings>
<setting name="Bob">12</setting>
<setting name="Carol">34</setting>
<setting name="Ted">56</setting>
<setting name="Alice">78</setting>
</settings>