2012-03-21 5 views
1

음 Visual Studio 2010을 사용하여 C++에서 PugiXML을 사용하여 요소의 내용을 가져 왔지만 그 내용을 볼 때 값을 얻지 못하는 것이 있습니다. "<"은 값을 얻지 못하기 때문에 "<"이 요소를 닫지 않아도 "<"문자에 도달 할 때까지 내용을 가져옵니다. 태그를 무시하는 경우에도 닫는 태그에 도달 할 때까지 가져오고 내부 태그 내부의 텍스트 만 있으면됩니다.요소 (또는 태그)의 내용을 가져 오는 PugiXML C++

그리고 나는 또한 내가 요소를 가져올 경우, 예를 들어 외부 XML을 얻는 방법을 알고 싶습니다

pugi :: xpath_node_set 도구 = doc.select_nodes ("/ 메쉬/경계/B");

Attribute Href: http://www.google.com 
Value: Link Till here 
Name: a 

: 여기

#include "pugixml.hpp" 

#include <iostream> 
#include <conio.h> 
#include <stdio.h> 

using namespace std; 

int main//21 
    () { 
    string source = "<mesh name='sphere'><bounds><b id='hey'> <a DeriveCaptionFrom='lastparam' name='testx' href='http://www.google.com'>Link Till here<b>it will stop here and ignore the rest</b> text</a></b> 0 1 1</bounds></mesh>"; 

    int from_string; 
    from_string = 1; 

    pugi::xml_document doc; 
    pugi::xml_parse_result result; 
    string filename = "xgconsole.xml"; 
    result = doc.load_buffer(source.c_str(), source.size()); 
    /* result = doc.load_file(filename.c_str()); 
    if(!result){ 
     cout << "File " << filename.c_str() << " couldn't be found" << endl; 
     _getch(); 
     return 0; 
    } */ 

     pugi::xpath_node_set tools = doc.select_nodes("/mesh/bounds/b/a[@href='http://www.google.com' and @DeriveCaptionFrom='lastparam']"); 

     for (pugi::xpath_node_set::const_iterator it = tools.begin(); it != tools.end(); ++it) { 
      pugi::xpath_node node = *it; 
      std::cout << "Attribute Href: " << node.node().attribute("href").value() << endl; 
      std::cout << "Value: " << node.node().child_value() << endl; 
      std::cout << "Name: " << node.node().name() << endl; 

     } 

    _getch(); 
    return 0; 
} 

출력입니다 : 내가

이 내용 "링크 여기까지"가 될 것입니다 전체 콘텐츠를 위해 할 일을 여기 아래에 주어진 동일 내가 충분히 명확했으면 좋겠다. 미리 감사드립니다.

답변

2

이것이 XML의 작동 방식입니다. < 또는 >을 귀하의 가치에 포함시킬 수 없습니다. 그들을 벗어나 (예 : &lt;&gt;과 같은 HTML 엔티티 사용) CDATA section을 정의하십시오.

4

내 심령 술사는 노드의 모든 하위 노드 (내부 텍스트라고도 함)의 연결된 텍스트를 가져 오는 방법을 알고 싶다고 말합니다.

그 작업을 수행하는 가장 쉬운 방법은 같은 XPath를 사용하는 것입니다 : 분명히 당신은 하위 트리에서 PCDATA/CDATA 값을 연결 자신의 재귀 함수를 쓸 수

pugi::xml_node node = doc.child("mesh").child("bounds").child("b"); 
string text = pugi::xpath_query(".").evaluate_string(); 

을; 사용하여 내장 재귀 이송 설비 등 find_node으로,도 (C++ 11 람다 구문을 사용하여) 일 것이다 :

string text; 
text.find_node([&](pugi::xml_node n) -> bool { if (n.type() == pugi::node_pcdata) result += n.value(); return false; }); 

지금, 당신은 태그의 전체 내용 (일명 외부 XML을 얻으려면), 당신은 출력 문자열 스트림 노드, 즉 수 있습니다

ostringstream oss; 
node.print(oss); 
string xml = oss.str(); 

내부 XML을 얻기 노드의 자식을 반복하고, 즉, 결과에

ostringstream oss; 
for (pugi::xml_node_iterator it = node.begin(); it != node.end(); ++it) 
    it->print(oss); 
string xml = oss.str(); 
1

를 자신의 외부 XML을 추가 필요합니다 내가 싸워 왔어 많이 모든 요소와 하위 노드를 포함하는 서브 트리를 구문 분석의 문제를 - 가장 쉬운 방법은 거의 여기에 무엇 같습니다

은이 코드를 사용한다 :

대신 oNode의
ostringstream oss; 
oNode.print(oss, "", format_raw); 
sResponse = oss.str(); 

원하는 노드를 사용합니다, 필요한 경우 모든 함수 앞에 pugi ::를 사용하십시오.