This is a DOM example, but it only differs from SAX in how you set initialize your parser. The string parse code is all the same:
const bool LoadFromStream = true; // Set to false for file i/o
const std::string MyFileName ("C:\TEMP\TEST.XML");
const std::string MyStream ("<?xml version='1.0' encoding='utf-8' ?><TAG>My Data</TAG>");
char *BufferID = NULL;
MemBufInputSource *MemBufIS = NULL;
DOMParser *Parser = new DOMParser;
Parser->setCreateEntityReferenceNodes(false);
Parser->setDoNamespaces(false);
Parser->setDoSchema(false);
Parser->setDoValidation(false);
Parser->setToCreateXMLDeclTypeNode(true);
Parser->setValidationScheme(false);
if (!LoadFromStream)
Parser->parse(MyFileName.c_str());
else
{
MemBufIS = new MemBufInputSource(
(const XMLByte*)MyStream.c_str(), // Stream pointer
MyStream.size()+ 1, // Byte (not character) count
&BufferID, // Buffer ID (becomes System ID)
false); // Copy (not adopt) caller's buffer
if (MemBufIS == NULL)
{
// Handle errors here
}
Parser->parse(*MemBufIS);
} // End if (LoadFromStream)
DOM_Node Root = Parser->getDocument();
DOM_Document Document = Parser->getDocument();
if (MemBufIS != NULL)
delete MemBufIS;
If this code looks familiar, it's because I've posted it before in response to the same question as it applied to a char string. I simply changed the char * references to std::string and used the c_str() and size() functions in place of a pointer and strlen() respectively.
HTH,
Don
At 10:10 AM 3/19/2002 +0100, you wrote:
Hi all,--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
How is it possible to parse the contents of a normal std::string...?
example:
01: string myString ="<?xml version=/"1.0/"?><test><blah>blah</blah></test>"; // or something....
02: try {
03: XMLPlatformUtils::Initialize();
04: } catch (XMLException& e) {
05: cerr << "Error during init!" << endl
06: << "Message: " << e.getMessage() << endl;
07: }
08:
09: SAX2XMLReader* parser = XMLReaderFactory::createXMLReader();
10: SAX2ElementHandler myHandler;
11: parser->setContentHandler(&myHandler);
12: parser->setErrorHandler(&myHandler);
13:
14: try {
15: parser->parse(....?????..........); // What to put here? myString does not work...
16: } catch (const XMLException& e) {
17: cerr << "Error during parse..." << endl;
18: } catch (...) {
19: cerr << "Other error..." << endl;
20: }
So my problem is what to do on line 15...
Many thanks for your help!
Harm de Laat
Informatiefabriek
The Netherlands
