-->

在的Xerces-C XPath支持(XPath support in Xerces-C)

2019-07-29 23:28发布

我支持它使用Xerces-C XML解析遗留C ++应用程序。 我被宠坏的.Net和习惯了使用XPath选择从DOM树节点。

有没有什么办法让访问的Xerces-C一些有限的XPath功能? 我正在寻找类似的selectNodes(“/为/酒吧/巴兹”)。 我可以手动做到这一点,但XPath是如此漂亮的对比。

Answer 1:

见了Xerces常见问题。

http://xerces.apache.org/xerces-c/faq-other-2.html#faq-9

请问的Xerces-C ++支持的XPath? No.Xerces-C ++ 2.8.0和Xerces-C ++ 3.0.1仅具有用于处理架构身份限制目的局部XPath实现。 对于全XPath的支持,您可以参考Apache的Xalan的C ++或其他开源项目一样帕坦。

这是很容易做你想要使用但xalan的东西。



Answer 2:

下面是XPath计算的使用Xerces 3.1.2工作的例子。

示例XML

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<root>
    <ApplicationSettings>hello world</ApplicationSettings>
</root>

C ++

#include <iostream>
#include <xercesc/dom/DOM.hpp>
#include <xercesc/dom/DOMDocument.hpp>
#include <xercesc/dom/DOMElement.hpp>
#include <xercesc/util/TransService.hpp>
#include <xercesc/parsers/XercesDOMParser.hpp>

using namespace xercesc;
using namespace std;

int main()
{
  XMLPlatformUtils::Initialize();
  // create the DOM parser
  XercesDOMParser *parser = new XercesDOMParser;
  parser->setValidationScheme(XercesDOMParser::Val_Never);
  parser->parse("sample.xml");
  // get the DOM representation
  DOMDocument *doc = parser->getDocument();
  // get the root element
  DOMElement* root = doc->getDocumentElement();

  // evaluate the xpath
  DOMXPathResult* result=doc->evaluate(
      XMLString::transcode("/root/ApplicationSettings"),
      root,
      NULL,
      DOMXPathResult::ORDERED_NODE_SNAPSHOT_TYPE,
      NULL);

  if (result->getNodeValue() == NULL)
  {
    cout << "There is no result for the provided XPath " << endl;
  }
  else
  {
    cout<<TranscodeToStr(result->getNodeValue()->getFirstChild()->getNodeValue(),"ascii").str()<<endl;
  }

  XMLPlatformUtils::Terminate();
  return 0;
}

编译并运行 (假定为标准的Xerces库安装和C ++文件名为xpath.cpp)

g++ -g -Wall -pedantic -L/opt/lib -I/opt/include -DMAIN_TEST xpath.cpp -o xpath -lxerces-c
./xpath

结果

hello world


Answer 3:

根据FAQ ,的Xerces-C支持部分的XPath 1个实现:

相同的发动机通过DOM文档提供::评估API,让用户执行只涉及一个DOMElement节点,没有谓语测试,并允许“//”运算符仅作为第一步简单的XPath查询。

您使用的DOMDocument ::评估()来计算表达式,然后返回一个DOMXPathResult 。



文章来源: XPath support in Xerces-C