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 00:37] kmioduszewski |
xerces_dom [2008/04/15 10:19] kmioduszewski |
||
---|---|---|---|
Linia 139: | Linia 139: | ||
</code> | </code> | ||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
Linia 158: | Linia 166: | ||
<!DOCTYPE notes SYSTEM "note.dtd"> | <!DOCTYPE notes SYSTEM "note.dtd"> | ||
<notes owner="John Doe"> | <notes owner="John Doe"> | ||
- | |||
<note date="21/02/2008"> | <note date="21/02/2008"> | ||
<from>Jani</from> | <from>Jani</from> | ||
Linia 181: | Linia 188: | ||
<!ELEMENT body (#PCDATA)> | <!ELEMENT body (#PCDATA)> | ||
</code> | </code> | ||
- | Klasy pomocnicze: | + | Klasy pomocnicze ''support.h'': |
<code cpp> | <code cpp> | ||
+ | #include <string> | ||
+ | #include <iostream> | ||
#include <xercesc/util/XMLString.hpp> | #include <xercesc/util/XMLString.hpp> | ||
+ | #include <xercesc/sax/HandlerBase.hpp> | ||
+ | |||
+ | //klasa pomocnicza ułatwiająca współpracę z XMLCh* | ||
+ | class UniString { | ||
+ | private : | ||
+ | char* cString; | ||
+ | XMLCh* xmlString; | ||
- | //klasa pomocnicza przechowujaca nazwy tagów oraz atrybutów w formacie XMLCh* | + | public : |
- | class TagNames { | + | UniString(const XMLCh* const str) |
- | public: | + | : |
- | XMLCh* TAG_NOTES; | + | cString(xercesc::XMLString::transcode(str)), |
- | XMLCh* TAG_NOTE; | + | xmlString(xercesc::XMLString::replicate(str)) |
- | XMLCh* TAG_FROM; | + | {} |
- | XMLCh* TAG_HEADING; | + | |
- | XMLCh* TAG_BODY; | + | UniString(const char* const str) |
- | + | : | |
- | XMLCh* ATTR_OWNER; | + | cString(xercesc::XMLString::replicate(str)), |
- | XMLCh* ATTR_DATE; | + | xmlString(xercesc::XMLString::transcode(str)) |
- | + | {} | |
- | TagNames() | + | |
+ | UniString(const std::string& str) | ||
+ | : | ||
+ | cString(xercesc::XMLString::replicate(str.c_str())), | ||
+ | xmlString(xercesc::XMLString::transcode(str.c_str())) | ||
+ | {} | ||
+ | |||
+ | ~UniString() | ||
+ | { | ||
+ | xercesc::XMLString::release(&cString); | ||
+ | xercesc::XMLString::release(&xmlString); | ||
+ | } | ||
+ | |||
+ | const char* toCString() { return cString; } | ||
+ | const XMLCh* toXMLString() { return xmlString; } | ||
+ | |||
+ | std::ostream& print(std::ostream& s) const | ||
+ | { | ||
+ | s << cString; | ||
+ | return(s); | ||
+ | } | ||
+ | |||
+ | }; | ||
+ | |||
+ | std::ostream& operator<<( std::ostream& s, const UniString& str ) { return str.print(s); } | ||
+ | |||
+ | //klasa pomocnicza przechowująca nazwy tagów oraz atrybutów | ||
+ | class docNames { | ||
+ | public: | ||
+ | const UniString TAG_NOTES; | ||
+ | const UniString TAG_NOTE; | ||
+ | const UniString TAG_FROM; | ||
+ | const UniString TAG_HEADING; | ||
+ | const UniString TAG_BODY; | ||
+ | |||
+ | const UniString ATTR_OWNER; | ||
+ | const UniString ATTR_DATE; | ||
+ | |||
+ | docNames() | ||
: | : | ||
- | TAG_NOTES(xercesc::XMLString::transcode("notes")), | + | TAG_NOTES("notes"), |
- | TAG_NOTE(xercesc::XMLString::transcode("note")), | + | TAG_NOTE("note"), |
- | TAG_FROM(xercesc::XMLString::transcode("from")), | + | TAG_FROM("from"), |
- | TAG_HEADING(xercesc::XMLString::transcode("heading")), | + | TAG_HEADING("heading"), |
- | TAG_BODY(xercesc::XMLString::transcode("body")), | + | TAG_BODY("body"), |
- | + | ||
- | ATTR_OWNER(xercesc::XMLString::transcode("owner")), | + | ATTR_OWNER("owner"), |
- | ATTR_DATE(xercesc::XMLString::transcode("date")) | + | ATTR_DATE("date") |
{} | {} | ||
+ | }; | ||
- | ~TagNames() throw(){ | + | 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> | ||
+ | ''parser.cpp'' FIXME | ||
+ | <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("file.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; | ||
- | try{ | ||
- | xercesc::XMLString::release(&TAG_NOTES); | ||
- | xercesc::XMLString::release(&TAG_NOTE); | ||
- | xercesc::XMLString::release(&TAG_FROM); | ||
- | xercesc::XMLString::release(&TAG_HEADING); | ||
- | xercesc::XMLString::release(&TAG_BODY); | ||
- | xercesc::XMLString::release(&ATTR_OWNER); | + | xercesc::XMLPlatformUtils::Terminate(); |
- | xercesc::XMLString::release(&ATTR_DATE); | + | |
- | }catch( ... ){ | + | int a; |
- | std::cerr << "TagNames destructor error" << std::endl ; | + | std::cin >> a; |
- | } | + | return 0; |
- | } | + | |
} | } | ||
</code> | </code> | ||
- | |||
<code cpp> | <code cpp> | ||
//... | //... |