我一直在尝试获取解码后的XML的值,所有3个值。
XML文件如下所示(它有更多的节点,但这只是一个测试预览)
<Response ID="number" Version="2.0">
<Issuer xmlns=":assertion">
check1
</Issuer>
<Status>
<StatusCode Value="Success" />
checkcheck2
</Status>
<Assertion ID="somenumber" IssueInstant="datestamp" Version="2.0"
xmlns="urn:oasis:names:tc:SAML:2.0:assertion">
<Issuer>
checkcheckcheck3
</Issuer>
</Assertion>
</Response>
我试图以以下方式从“状态”节点中获取值(不要介意包含我不需要的内容,我已经尝试了很多东西,只是暂时保留它们,以防我需要它们,当它工作时将删除它们):
#include <iostream>
#include <sstream>
#include <iostream>
#include <cstring>
#include <string>
#include <fstream>
#include <vector>
#include "rapidxml.hpp"
#include "rapidxml_print.hpp"
#include "base64.hpp"
using namespace std;
int main()
{
rapidxml::xml_node<> *root_node;
rapidxml::xml_node<> *second_node;
//costum base64 encoder and decoder this works as it should
help::base64_decode;
string xmlFile;
// Base64 response from form data contains xml
xmlFile = help::base64_decode("the base64 encoded SAMLResponse");
//put the b64 decoded xml in a string
stringstream decodedXml(xmlFile);
rapidxml::xml_document<> doc;
// test the decoded b64
cout << xmlFile << endl;
// Read file into vector<char>
vector<char> buffer((istreambuf_iterator<char>(decodedXml)), istreambuf_iterator<char>());
buffer.push_back('\0');
doc.parse<0>(&buffer[0]);
root_node = doc.first_node("Response");
// this returs Issuer
cout << root_node->first_node()->name() << endl;
// go to next sibling of the root node ?? (that should be Status)
second_node = root_node->next_sibling();
// gives me a exited with code=3221225477 in 0.485 seconds on compiling
cout << second_node->first_node()->name() << endl;
我做错了什么,或者说,我误解了next_sibling()
函数的哪一部分。据我所知,next_sibling()
将转到与声明为root_node
的节点(在本例中是Issuer)处于同一级别的下一个节点。
2条答案
按热度按时间qv7cva1a1#
根节点--根据定义--不能有兄弟节点。相反,您需要
->first_node()->next_sibling
和co,或者更好:按名称搜索子节点:注意
parse_trim_whitespace
的使用,如果没有它,value()
函数将包含大量空白。m4pnthwp2#
如果有人想知道我做了什么来修复不能通过字符串输入搜索节点。
我写了一个搜索兄弟节点的函数。
这现在允许我搜索与;
rapidxml::xml_node<> *issuer_node = find_sibling(issuer_node->first_node(), "Assertion");
这将通过函数来尝试并找到Assert节点。如果在兄弟节点中找不到它,而不是崩溃,它将返回一个带有的xml文档。