2

Visual Studio2010を使用してC++でPugiXMLを使用して要素のコンテンツを取得していますが、「<」が表示されると値の取得が停止するため、値は取得されません。 「<」が要素を閉じていなくても、「<」文字に達するまでのコンテンツ。タグを無視しても、終了タグに到達するまで取得したいのですが、少なくとも内部タグ内のテキストのみです。

また、たとえば要素をフェッチした場合に外部XMLを取得する方法も知りたいです。

pugi :: xpath_node_set tools = doc.select_nodes( "/ mesh / bounds / b"); 「ここまでリンク」というコンテンツ全体を取得するにはどうすればよいですか

この内容はここで与えられたものと同じです:

#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;
}

出力は次のとおりです。

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

私は十分に明確だったと思います、事前に感謝します

4

3 に答える 3

7

私の超能力は、ノードのすべての子の連結テキスト (別名内部テキスト) を取得する方法を知りたいと言っています。

これを行う最も簡単な方法は、次のように XPath を使用することです。

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

明らかに、サブツリーから PCDATA/CDATA 値を連結する独自の再帰関数を作成できます。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 を取得するには、ノードの子を反復処理し、結果に外側の xml を追加する必要があります。

ostringstream oss;
for (pugi::xml_node_iterator it = node.begin(); it != node.end(); ++it)
    it->print(oss);
string xml = oss.str();
于 2012-03-22T04:20:35.343 に答える
2

それが XML の仕組みです。<値に埋め込んだり、>右に配置したりすることはできません。&lt;それらをエスケープする (たとえば、 や などの HTML エンティティを使用する) か、 CDATA セクション&gt;を定義します。

于 2012-03-21T17:03:24.230 に答える
1

すべての要素とサブノードを含むサブツリーの解析の問題に苦労しました-最も簡単な方法は、ほとんどここに示されているものです:

次のコードを使用する必要があります。

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

oNode の代わりに必要なノードを使用し、必要に応じてすべての関数の前に pugi:: を使用します。

于 2013-05-05T14:28:46.197 に答える