/* initialise xerces */
try { XMLPlatformUtils::Initialize (); } catch (const XMLException & toCatch) { char *pMsg = XMLString::transcode (toCatch.getMessage ()); userlog ("Error during Xerces-c Initialization.\n" " Exception message: %s", pMsg); delete[]pMsg; return -1; } |
#include <xercesc/util/PlatformUtils.hpp>
#include <xercesc/framework/XMLFormatter.hpp> #include <xercesc/util/XMLString.hpp> #include <xercesc/util/XMLUniDefs.hpp> #include <xercesc/framework/MemBufInputSource.hpp> #include <xercesc/framework/LocalFileInputSource.hpp>
#include <xercesc/parsers/DOMParser.hpp>
#include <xercesc/dom/DOM_Node.hpp>
/* parse a XML file */
char *xmlFileName = "./myfile.xml"; DOMParser *parser = 0; DOM_Document document; DOM_Element topLevel;
LocalFileInputSource source (XMLString::transcode (xmlFileName));
// // Create our parser, then set the parsing options. // discovers errors during the course of parsing the XML document. // parser = new DOMParser (); parser->setValidationScheme (DOMParser::Val_Never); parser->setDoNamespaces (false); parser->setDoSchema (false); parser->setValidationSchemaFullChecking (false); parser->setCreateEntityReferenceNodes (false); parser->setToCreateXMLDeclTypeNode (true); // // Parse the XML file, catching any XML exceptions that might propagate // out of it. // try { parser->parse (source); int errorCount = parser->getErrorCount (); if (errorCount > 0) { printf("%d error(s) occured during parsing config\n", errorCount); goto clean; } } catch (const XMLException & e) { printf("An error occured during parsing \n Message: %s\n" "", e.getMessage ()); goto clean; } catch (const DOM_DOMException & e) { printf("A DOM error occured during parsing config\n" "Exception code: %d\n", e.code); goto clean; } catch (...) { printf ("An error occured during parsing config\n"); goto clean; } … |
#include <xercesc/sax/ErrorHandler.hpp>
class ExampleErrorHandler: public ErrorHandler { virtual void anyError(char* type, const SAXParseException& exception) ; public: /** Default constructor */ ExampleErrorHandler(){}; /** Destructor */ ~ExampleErrorHandler() {}; void warning(const SAXParseException& exception){anyError("warning", exception); }; void error(const SAXParseException& exception) {anyError("error", exception); }; void fatalError(const SAXParseException& exception){anyError("fatal error", exception); }; void resetErrors() {}; };
void ExampleErrorHandler::anyError(char* type, const SAXParseException& exception){
printf("Parser %s line %d column %d: %ls %ls : %ls", type, exception.getLineNumber () , exception.getColumnNumber () , exception.getPublicId () , exception.getSystemId () , exception.getMessage ()); } |
ExampleErrorHandler handler;
parser = new DOMParser (); parser->setErrorHandler(&handler); // // Parse the XML file, catching any XML exceptions that might propagate // out of it. // try { parser->parse (source); } … |
/* creates an empty dom tree */
DOM_DOMImplementation impl; DOM_Document doc = impl.createDocument (0, // root element namespace URI. rootname, // root element name DOM_DocumentType ());// document type object (DTD). /* fetch the root element */ DOM_Element rootElem = doc.getDocumentElement (); |
//Add new (empty) Element to the root element
DOM_Element parentNode = …;// parent is known DOM_Element prodElem = doc->createElement (tagName); parentNode->appendChild (prodElem); |
parentNode->removeChild (prodElem);
|
DOM_Element parent = …; // parent is known
DOM_Node child; child = parent.getFirstChild (); while (child != 0) { //work with child … //pickup next child child = child.getNextSibling (); } |
switch (child.getNodeType ())
{ case DOM_Node::ELEMENT_NODE: … break; case DOM_Node::ATTRIBUTE_NODE: … break; case DOM_Node::TEXT_NODE: … break; case DOM_Node::CDATA_SECTION_NODE: … break; case DOM_Node::ENTITY_REFERENCE_NODE: … break; case DOM_Node::ENTITY_NODE: … break; case DOM_Node::PROCESSING_INSTRUCTION_NODE: … break; case DOM_Node::COMMENT_NODE: … break; case DOM_Node::DOCUMENT_NODE: … break; case DOM_Node::DOCUMENT_TYPE_NODE: … break; case DOM_Node::DOCUMENT_FRAGMENT_NODE: … break; case DOM_Node::NOTATION_NODE: … break; case DOM_Node::XML_DECL_NODE: … break; default: … } |
DOM_Element parent = …; // parent is known
DOM_Node child; DOM_Text value; child = parent.getFirstChild (); while (child != 0) { //work with child if (child.getNodeType () == DOM_Node::TEXT_NODE) { value = (DOM_Text &) child; break; } //pickup next child child = child.getNextSibling (); } DOMString unicodeValue = value.getData (); //if you need the ascii value char* asciiValue = unicodeValue.transcode (); //work with your value … //free the value delete []asciiValue ; |
DOM_Element parent = …; // parent is known
DOM_Node child; DOM_Text value; bool childFound = false; child = parent.getFirstChild (); while (child != 0) { //work with child if (child.getNodeType () == DOM_Node::TEXT_NODE) { value = (DOM_Text &) child; childFound = true; break; } //pickup next child child = child.getNextSibling (); } //now , maybe create a text node if (!childFound) { value = doc->createTextNode (); parent.appendChild (value); } DOMString unicodeValue(asciiValue); value.setData(unicodeValue); |
DOMString unicodename(asciiname);
DOMString unicodevalue(asciivalue); DOM_Element element = …;// element is known //Add new attribute to the element element->setAttribute(unicodename, unicodevalue); |
DOMString unicodename(asciiname);
DOM_Element element = …;// element is known //Add new attribute to the element element->removeAttribute(unicodename); |
//loop through this element attributes and fill the config structure
DOMString unicodename; DOMString unicodevalue; DOM_NamedNodeMap attributes; DOM_Element element = …;// element is known attributes = element.getAttributes (); int attrCount; attrCount = attributes.getLength (); for (i = 0; i < attrCount; i ) { DOM_Node attribute = attributes.item (i); //work with the attribute unicodename = attribute.getNodeName (); unicodevalue= attribute. getNodeValue (); //if need ascii values, get them char* asciiname= unicodename.transcode (); char* asciivalue = unicodevalue.transcode (); … //but don't forget to release them delete []asciiname; delete []asciivalue; } |