C++ 使用XPath搜索QDomDocument中的节点

C++ 使用XPath搜索QDomDocument中的节点,c++,qt,xpath,qxmlquery,qdomdocument,C++,Qt,Xpath,Qxmlquery,Qdomdocument,我被System.XML名称空间中的C#及其XML操作类宠坏了(当然,我已经腐烂了)。我可以将XML文件加载到XmlDocument中。我可以使用XmlNode.SelectNodes(“XPath表达式”)在整个文档中搜索与XPath表达式匹配的节点。结果是一个XmlNodeList,它包含我可以迭代的xmlnodel对象 现在我使用的是C++ QT(版本4.7.1和4.8,但是特定的版本可能并不重要)。我可以将XML文件加载到QDomDocument中。但是,我感到沮丧的是,我不能像在C#中

我被
System.XML
名称空间中的C#及其XML操作类宠坏了(当然,我已经腐烂了)。我可以将XML文件加载到
XmlDocument
中。我可以使用
XmlNode.SelectNodes(“XPath表达式”)
在整个文档中搜索与XPath表达式匹配的节点。结果是一个
XmlNodeList
,它包含我可以迭代的
xmlnodel
对象

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

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

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


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

下面是我使用XPath表达式在
QDomDocument
中搜索节点的实用函数。它使用@Alejandro建议的
QDomNodeModel
类,可从下载。它基于中的用法示例。感谢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文件的路径。
QString path=“settings.xml”;
//------------------------------
//打开文件。
QFile文件(路径);
如果(!file.open(QIODevice::ReadOnly))
{
qDebug(“未能打开“%s”:%s”、qPrintable(路径)、qPrintable(file.errorString());
返回;
}
//------------------------------
//将文件加载到文档中。
QDOM文件文档;
QString错误;
内线;
int列;
if(!doc.setContent(&file,&error,&line,&column))
{
qDebug(“%s(%d,%d):%s”,qPrintable(路径),行,列,qPrintable(错误));
返回;
}
//------------------------------
//文档根元素。
QdomeElement rootElem=doc.documentElement();
//------------------------------
//搜索名称属性具有特定值的元素。
QString name=“Alice”;
qstringxpath=QString(“设置[@name='%1']”)。arg(名称);
QList foundNodes=findNodes(doc、rootElem、xpath);
//------------------------------
//我找到了吗?
如果(foundNodes.size()>0)
{
QdomeElement foundElem=foundNodes.at(0).toElement();
//用那个元素做点什么。
...
} 
要搜索的示例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>

12
34
56
78

非常奇怪的是,作为Qt团队为DOM和XQuery/XPath/XSLT保留单独数据模型的设计选择,他们没有用一些现成的
QAbstractXmlNodeModel实现来填补这一空白。你可以在“谢谢你”找到一个,亚历杭德罗。这是一个极好的建议。该链接提供的
QDomNodeModel
源代码,以及位于的
QXmlQuery
使用示例,已充分开发,可以按原样为我工作。