문제

XML 구문 분석에 XERCES-C를 사용하는 레거시 C ++ 응용 프로그램을 지원하고 있습니다. .NET에 의해 망쳐졌고 XPath를 사용하여 DOM 트리에서 선택한 노드를 사용하는 데 익숙합니다.

XERCES-C에서 제한된 XPATH 기능에 액세스 할 수있는 방법이 있습니까? SelectNodes ( "/for/bar/baz")와 같은 것을 찾고 있습니다. 수동으로 할 수는 있지만 XPath는 비교하여 너무 좋습니다.

도움이 되었습니까?

해결책

Xerces FAQ를 참조하십시오.

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 ++ 또는 Pathan과 같은 다른 오픈 소스 프로젝트를 참조하십시오.

그러나 Xalan을 사용하고 싶은 일을하는 것은 매우 쉽습니다.

다른 팁

다음은 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

에 따르면 자주하는 질문, XERCES-C는 부분 XPath 1 구현을 지원합니다.

동일한 엔진은 DomDocument :: API를 통해 사용할 수 있도록 제공하여 사용자가 Domlement 노드와 관련된 간단한 XPATH 쿼리를 수행 할 수 있도록하며, 술어 테스트없이 "//"연산자를 초기 단계로만 허용합니다.

너는 사용한다 domdocument :: evaluate () 표현을 평가하려면 a domxpathresult.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top