让 Xerces 解析字符串而不是文件

发布于 2024-10-12 00:41:35 字数 658 浏览 6 评论 0原文

我知道如何仅使用 XercesDOMParser 从 xml 文件创建完整的 dom:

xercesc::XercesDOMParser parser = new xercesc::XercesDOMParser();
parser->parse(path_to_my_file);
parser->getDocument(); // From here on I can access all nodes and do whatever i want

嗯,这可行......但是如果我想解析字符串怎么办?就像

std::string myxml = "<root>...</root>";
xercesc::XercesDOMParser parser = new xercesc::XercesDOMParser();
parser->parse(myxml);
parser->getDocument(); // From here on I can access all nodes and do whatever i want

我正在使用版本 3 一样。查看 AbstractDOMParser 内部,我看到该解析方法及其重载版本,仅解析文件。

如何从字符串中解析?

I know how to create a complete dom from an xml file just using XercesDOMParser:

xercesc::XercesDOMParser parser = new xercesc::XercesDOMParser();
parser->parse(path_to_my_file);
parser->getDocument(); // From here on I can access all nodes and do whatever i want

Well, that works... but what if I'd want to parse a string? Something like

std::string myxml = "<root>...</root>";
xercesc::XercesDOMParser parser = new xercesc::XercesDOMParser();
parser->parse(myxml);
parser->getDocument(); // From here on I can access all nodes and do whatever i want

I'm using version 3. Looking inside the AbstractDOMParser I see that parse method and its overloaded versions, only parse files.

How can I parse from a string?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

心的位置 2024-10-19 00:41:35

创建一个 MemBufInputSource解析

xercesc::MemBufInputSource myxml_buf(myxml.c_str(), myxml.size(),
                                     "myxml (in memory)");
parser->parse(myxml_buf);

Create a MemBufInputSource and parse that:

xercesc::MemBufInputSource myxml_buf(myxml.c_str(), myxml.size(),
                                     "myxml (in memory)");
parser->parse(myxml_buf);
三人与歌 2024-10-19 00:41:35

使用 XercesDOMParser::parse() 的以下重载:

void XercesDOMParser::parse(const InputSource& source);

向其传递 MemBufInputSource:

MemBufInputSource src((const XMLByte*)myxml.c_str(), myxml.length(), "dummy", false);
parser->parse(src);

Use the following overload of XercesDOMParser::parse():

void XercesDOMParser::parse(const InputSource& source);

passing it a MemBufInputSource:

MemBufInputSource src((const XMLByte*)myxml.c_str(), myxml.length(), "dummy", false);
parser->parse(src);
奶气 2024-10-19 00:41:35

我用另一种方式做。如果这是不正确的,请告诉我原因。似乎有效。
这就是 parse 所期望的:

DOMDocument* DOMLSParser::parse(const DOMLSInput * source )

因此您需要放入 DOMLSInput 而不是 InputSource:

xercesc::DOMImplementation * impl = xercesc::DOMImplementation::getImplementation();
xercesc::DOMLSParser *parser = (xercesc::DOMImplementationLS*)impl)->createLSParser(xercesc::DOMImplementation::MODE_SYNCHRONOUS, 0);
xercesc::DOMDocument *doc;

xercesc::Wrapper4InputSource source (new xercesc::MemBufInputSource((const XMLByte *) (myxml.c_str()), myxml.size(), "A name");
parser->parse(&source);

Im doing it another way. If this is incorrect, please tell me why. It seems to work.
This is what parse expects:

DOMDocument* DOMLSParser::parse(const DOMLSInput * source )

So you need to put in a DOMLSInput instead of a an InputSource:

xercesc::DOMImplementation * impl = xercesc::DOMImplementation::getImplementation();
xercesc::DOMLSParser *parser = (xercesc::DOMImplementationLS*)impl)->createLSParser(xercesc::DOMImplementation::MODE_SYNCHRONOUS, 0);
xercesc::DOMDocument *doc;

xercesc::Wrapper4InputSource source (new xercesc::MemBufInputSource((const XMLByte *) (myxml.c_str()), myxml.size(), "A name");
parser->parse(&source);
比忠 2024-10-19 00:41:35

您可以使用 xercesc/framework/MemBufInputSource.cpp 中的 MemBufInputSource,并且头文件 MemBufInputSource.hpp 包含大量文档,与上面的答案类似:

#include <xercesc/framework/MemBufInputSource.hpp>

char* myXMLBufString = "<root>hello xml</root>";
MemBufInputSource xmlBuf((const XMLByte*)myXMLBufString, 23, "myXMLBufName", false);

但请注意,除非您首先初始化系统,如下所示(取自 xerces-c-3.2.3/samples/src/SAX2Count/SAX2Count.cpp)

bool                         recognizeNEL = false;
char                         localeStr[64];
memset(localeStr, 0, sizeof localeStr);

// Initialize the XML4C2 system
try {
    if (strlen(localeStr)) {
        XMLPlatformUtils::Initialize(localeStr);
    } else {
        XMLPlatformUtils::Initialize();
    }
    if (recognizeNEL) {
        XMLPlatformUtils::recognizeNEL(recognizeNEL);
    }
} catch (const XMLException& toCatch) {
    XERCES_STD_QUALIFIER cerr << "Error during initialization! Message:\n"
            << StrX(toCatch.getMessage()) << XERCES_STD_QUALIFIER endl;
    return 1;
}

当然,读取文件不需要考虑这种类型的准备,因为您只需将文件路径传递给解析器采用的程序。因此,对于那些遇到段错误的人来说,这可能就是答案。

You may use MemBufInputSource as found in the xercesc/framework/MemBufInputSource.cpp, and the header file, MemBufInputSource.hpp contains extensive documentation, as similar to answers above:

#include <xercesc/framework/MemBufInputSource.hpp>

char* myXMLBufString = "<root>hello xml</root>";
MemBufInputSource xmlBuf((const XMLByte*)myXMLBufString, 23, "myXMLBufName", false);

But take note, this doesn't seem to work unless you first initialize the system, as below (taken from the xerces-c-3.2.3/samples/src/SAX2Count/SAX2Count.cpp)

bool                         recognizeNEL = false;
char                         localeStr[64];
memset(localeStr, 0, sizeof localeStr);

// Initialize the XML4C2 system
try {
    if (strlen(localeStr)) {
        XMLPlatformUtils::Initialize(localeStr);
    } else {
        XMLPlatformUtils::Initialize();
    }
    if (recognizeNEL) {
        XMLPlatformUtils::recognizeNEL(recognizeNEL);
    }
} catch (const XMLException& toCatch) {
    XERCES_STD_QUALIFIER cerr << "Error during initialization! Message:\n"
            << StrX(toCatch.getMessage()) << XERCES_STD_QUALIFIER endl;
    return 1;
}

Of course reading a file wouldn't require thinking about this type of prep since you just pass a file path to the program which the parser takes. So for those experiencing seg faults, this could be the answer.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文