Różnice między wybraną wersją a wersją aktualną.
Both sides previous revision Previous revision Next revision | Previous revision Next revision Both sides next revision | ||
xerces_dom [2008/04/15 01:03] kmioduszewski |
xerces_dom [2008/04/15 02:01] kmioduszewski |
||
---|---|---|---|
Linia 139: | Linia 139: | ||
</code> | </code> | ||
+ | |||
+ | |||
+ | |||
Linia 183: | Linia 186: | ||
<!ELEMENT body (#PCDATA)> | <!ELEMENT body (#PCDATA)> | ||
</code> | </code> | ||
- | Klasy pomocnicze: | + | Klasy pomocnicze ''support.h'': |
<code cpp> | <code cpp> | ||
- | #include<string> | + | #include <string> |
- | #include<xercesc/util/XMLString.hpp> | + | #include <iostream> |
+ | #include <xercesc/util/XMLString.hpp> | ||
+ | #include <xercesc/sax/HandlerBase.hpp> | ||
+ | |||
//klasa pomocnicza ułatwiająca współpracę z XMLCh* | //klasa pomocnicza ułatwiająca współpracę z XMLCh* | ||
class UniString { | class UniString { | ||
Linia 196: | Linia 201: | ||
xmlString(xercesc::XMLString::replicate(str)) | xmlString(xercesc::XMLString::replicate(str)) | ||
{} | {} | ||
- | + | ||
UniString(const char* const str) | UniString(const char* const str) | ||
: | : | ||
Linia 202: | Linia 207: | ||
xmlString(xercesc::XMLString::transcode(str)) | xmlString(xercesc::XMLString::transcode(str)) | ||
{} | {} | ||
+ | |||
UniString(const std::string& str) | UniString(const std::string& str) | ||
: | : | ||
Linia 208: | Linia 213: | ||
xmlString(xercesc::XMLString::transcode(str.c_str())) | xmlString(xercesc::XMLString::transcode(str.c_str())) | ||
{} | {} | ||
- | + | ||
~UniString() | ~UniString() | ||
{ | { | ||
Linia 214: | Linia 219: | ||
xercesc::XMLString::release(&xmlString); | xercesc::XMLString::release(&xmlString); | ||
} | } | ||
+ | |||
const char* toCString() { return cString; } | const char* toCString() { return cString; } | ||
const XMLCh* toXMLString() { return xmlString; } | const XMLCh* toXMLString() { return xmlString; } | ||
- | + | ||
std::ostream& print(std::ostream& s) const | std::ostream& print(std::ostream& s) const | ||
{ | { | ||
Linia 223: | Linia 228: | ||
return(s); | return(s); | ||
} | } | ||
+ | |||
private : | private : | ||
char* cString; | char* cString; | ||
XMLCh* xmlString; | XMLCh* xmlString; | ||
+ | |||
}; | }; | ||
- | + | ||
- | std::ostream& operator<<( std::ostream& s, const UniString& str ) { return s << str.print(); } | + | std::ostream& operator<<( std::ostream& s, const UniString& str ) { return str.print(s); } |
+ | |||
//klasa pomocnicza przechowująca nazwy tagów oraz atrybutów | //klasa pomocnicza przechowująca nazwy tagów oraz atrybutów | ||
class docNames { | class docNames { | ||
Linia 244: | Linia 249: | ||
const UniString ATTR_DATE; | const UniString ATTR_DATE; | ||
- | TagNames() | + | docNames() |
: | : | ||
TAG_NOTES("notes"), | TAG_NOTES("notes"), | ||
Linia 255: | Linia 260: | ||
ATTR_DATE("date") | ATTR_DATE("date") | ||
{} | {} | ||
+ | }; | ||
+ | |||
+ | class myErrorHandler : xercesc::HandlerBase { | ||
+ | void warning( const xercesc::SAXParseException& e ) { | ||
+ | std::cout << "warning: " << UniString(e.getMessage()) | ||
+ | << " | line: " << e.getLineNumber() << std::endl; | ||
+ | } | ||
+ | void error( const xercesc::SAXParseException& e ) { | ||
+ | std::cout << "error: " << UniString(e.getMessage()) | ||
+ | << " | line: " << e.getLineNumber() << std::endl; | ||
+ | } | ||
+ | void fatalError( const xercesc::SAXParseException& e ) { | ||
+ | std::cout << "fatalError: " << UniString(e.getMessage()) | ||
+ | << " | line: " <<e.getLineNumber() << std::endl; | ||
+ | } | ||
+ | void resetErrors() {} | ||
}; | }; | ||
</code> | </code> | ||
+ | ''parser.cpp'' | ||
+ | <code cpp> | ||
+ | #include "support.h" | ||
+ | xercesc::DOMDocument* parseXML() { | ||
+ | xercesc::XercesDOMParser* parser = new xercesc::XercesDOMParser(); | ||
+ | parser->setValidationScheme(xercesc::XercesDOMParser::Val_Auto); | ||
+ | parser->setDoNamespaces(true); | ||
+ | |||
+ | xercesc::ErrorHandler* errHandler = (xercesc::ErrorHandler*) new myErrorHandler(); | ||
+ | parser->setErrorHandler(errHandler); | ||
+ | |||
+ | UniString src = UniString("dupa.xml"); | ||
+ | try { | ||
+ | parser->parse(src.toXMLString()); | ||
+ | } | ||
+ | catch (...) { | ||
+ | //... | ||
+ | } | ||
+ | |||
+ | if(parser->getErrorCount()==0) { | ||
+ | return parser->getDocument(); | ||
+ | } else { | ||
+ | return NULL; | ||
+ | } | ||
+ | } | ||
+ | |||
+ | int main() { | ||
+ | try { | ||
+ | xercesc::XMLPlatformUtils::Initialize(); | ||
+ | } | ||
+ | catch (const xercesc::XMLException& e) { | ||
+ | std::cerr << UniString(e.getMessage()) << std::endl; | ||
+ | return 1; | ||
+ | } | ||
+ | |||
+ | xercesc::DOMDocument* xmlDoc = parseXML(); | ||
+ | if(xmlDoc == NULL) | ||
+ | return -1; | ||
+ | |||
+ | |||
+ | xercesc::XMLPlatformUtils::Terminate(); | ||
+ | |||
+ | int a; | ||
+ | std::cin >> a; | ||
+ | return 0; | ||
+ | } | ||
+ | </code> | ||
<code cpp> | <code cpp> | ||
//... | //... |