首页 > 解决方案 > 使用 XPath 在 QDomDocument 中搜索节点

问题描述

我被 C# 及其在命名空间中的 XML 操作类宠坏了(可以肯定的是)System.Xml。我可以将 XML 文件加载到XmlDocument. 我可以在整个文档中搜索与 XPath 表达式匹配的节点,使用XmlNode.SelectNodes( "an xpath expression" ). 结果是一个XmlNodeList包含XmlNode我可以迭代的对象。

现在我正在使用 C++ Qt(版本 4.7.1 和 4.8,但特定版本可能并不重要)。我可以将 XML 文件加载到QDomDocument. 但是,我很沮丧,因为我无法像在 C# 中那样使用 XPath 表达式搜索文档。

QXmlQuery我使用在 XML 文件中查找内容的成功有限。如果我以正确的方式编写查询,我可以获得一个QStringList结果,迭代它QStringList,然后将数据存储在某个地方以备后用。

但是,我仍然希望能够QDomNode直接通过 XPath 表达式获取文档中的对象集合。一个特定的用例是找到一个“名称”属性具有特定值的元素,然后用新元素替换该元素。这就是为什么我想要QDomNode对象本身,而不仅仅是QXmlQuery可以提供的基于字符串或其他 XML 内容的表示。对于刚才提到的特定用例,我是通过使用QDomElement.elementsByTagName()和迭代这些元素来获得的,但它不像 XPath 那样灵活也不酷。

只是一厢情愿吗?是否值得努力开发一些实现QAbstractXmlReceiver接口的新类?或者,我最终会得到一个与 中的QDomNode对象没有直接关系的新数据集合QDomDocument吗?

标签: c++qtxpathqxmlqueryqdomdocument

解决方案


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>

推荐阅读