I Use a DOMLSInput as following:
(here 'parser' is a DOMLSParser made from createLSParser )
DOMDocument* loadDoc(const std::string& xmlfile) {
DOMDocument* rslt = NULL;
if ( ! xmlfile.empty() ) {
DOMLSInput* input =
((DOMImplementationLS*)impl)->createLSInput();
XMLByte* xmlraw = (XMLByte*)(xmlfile.c_str());
MemBufInputSource* mbis = new
MemBufInputSource(xmlraw,xmlfile.size(),X("myfile.xml"), true);
input->setByteStream(mbis);
input->setEncoding(XMLUni::fgUTF8EncodingString); //This must
be done.
try {
rslt = parser->parse(input);
}
catch ( ... ) {
//handle errors here..
}
}
return rslt;
}
As far as I understand it, the following should also work, (but
doesn't seem to do so reliably)
DOMDocument* loadDoc(const std::string& xmlfile) {
DOMDocument* rslt = NULL;
if ( ! xmlfile.empty() ) {
DOMLSInput* input =
((DOMImplementationLS*)impl)->createLSInput();
input->setStringData((XMLCh*)xmlfile.c_str()); //very naughty cast,
but we set the encoding next....
input->setEncoding(XMLUni::fgUTF8EncodingString); //This must
be done.
try {
rslt = parser->parse(input);
}
catch ( ... ) {
//handle errors here..
}
}
return rslt;
}
I am not sure about what memory needs freeing/releasing - I am not
good at xercesc memory management.
Hope that helps.
-b.
On 24 Mar 2009, at 08:57, cpplove wrote:
unsigned int zzz = strlen( (const char*)file); // verified this
MemBufInputSource* memIS = new MemBufInputSource (
(const XMLByte*) file ,
zzz ,
filename ,
false );
// memIS->setEncoding(XMLUni::fgUTF8EncodingString); // tried turning
this
on
// the file being load is
UTF-8
// saved as UTF-8 encoding
parser2->parse(*memIS); // this fails and my handler's events are
never called
parser2->parse(filename); // this works but i want to avoid this
in
favor
// of parsing from the
buffer.