// XSAnnotation methods void XSAnnotation::writeAnnotation(DOMNode* node, ANNOTATION_TARGET targetType) { XercesDOMParser *parser = new (fMemoryManager) XercesDOMParser(0, fMemoryManager); parser->setDoNamespaces(true); parser->setValidationScheme(XercesDOMParser::Val_Never); DOMDocument* futureOwner = (targetType == W3C_DOM_ELEMENT) ? ((DOMElement*)node)->getOwnerDocument() : (DOMDocument*)node; MemBufInputSource* memBufIS = new (fMemoryManager) MemBufInputSource ( (const XMLByte*)fContents , XMLString::stringLen(fContents)*sizeof(XMLCh) , "" , false , fMemoryManager ); memBufIS->setEncoding(XMLUni::fgXMLChEncodingString); try { parser->parse(*memBufIS); } catch (const XMLException&) { // REVISIT: should we really eat this? } DOMNode* newElem = futureOwner->importNode((parser->getDocument())->getDocumentElement(), true); node->insertBefore(newElem, node->getFirstChild()); delete parser; delete memBufIS; }
XERCES_CPP_NAMESPACE_BEGIN DOMDocument * XIncludeDOMDocumentProcessor::doXIncludeDOMProcess(const DOMDocument * const source, XMLErrorReporter *errorHandler, XMLEntityHandler* entityResolver /*=NULL*/){ XIncludeUtils xiu(errorHandler); DOMImplementation* impl = source->getImplementation(); DOMDocument *xincludedDocument = impl->createDocument(); try { /* set up the declaration etc of the output document to match the source */ xincludedDocument->setDocumentURI( source->getDocumentURI()); xincludedDocument->setXmlStandalone( source->getXmlStandalone()); xincludedDocument->setXmlVersion( source->getXmlVersion()); /* copy entire source document into the xincluded document. Xincluded document can then be modified in place */ DOMNode *child = source->getFirstChild(); for (; child != NULL; child = child->getNextSibling()){ if (child->getNodeType() == DOMNode::DOCUMENT_TYPE_NODE){ /* I am simply ignoring these at the moment */ continue; } DOMNode *newNode = xincludedDocument->importNode(child, true); xincludedDocument->appendChild(newNode); } DOMNode *docNode = xincludedDocument->getDocumentElement(); /* parse and include the document node */ xiu.parseDOMNodeDoingXInclude(docNode, xincludedDocument, entityResolver); xincludedDocument->normalizeDocument(); } catch(const XMLErrs::Codes) { xincludedDocument->release(); return NULL; } catch(...) { xincludedDocument->release(); throw; } return xincludedDocument; }
int main(int argC, char*argV[]) { // Initialize the XML4C2 system. try { XMLPlatformUtils::Initialize();/*init*/ } catch(const XMLException& toCatch) { char *pMsg = XMLString::transcode(toCatch.getMessage()); XERCES_STD_QUALIFIER cerr << "Error during Xerces-c Initialization.\n" << " Exception message:" << pMsg; XMLString::release(&pMsg); return 1; } DOMImplementation* impl = DOMImplementationRegistry::getDOMImplementation(X("LS"));/**Implementation*/ DOMLSSerializer* theSerializer = ((DOMImplementationLS*)impl)->createLSSerializer();/** createLSSerializer*/ DOMLSOutput *theOutputDesc = ((DOMImplementationLS*)impl)->createLSOutput();/**createLSOutput*/ MemBufFormatTarget* target = new MemBufFormatTarget(); theOutputDesc->setByteStream(target); if (impl != NULL) { try { DOMDocument* doc = impl->createDocument( 0, // root element namespace URI. /**可用资源标识*/ X("corporation"), // root element name /**a large companny*/ 0); // document type object (DTD). /**mem mannager mode object*/ doc->setXmlStandalone(true); DOMElement* rootElem = doc->getDocumentElement(); rootElem->setAttribute(X("xmlns"), X("com:cmcc:corporation")); rootElem->setAttribute(X("xmlns:xsi"), X("http://www.w3.org/2001/XMLSchema-instance")); rootElem->setAttribute(X("xsi:schemaLocation"), X("com:cmcc:corporation corporation.xsd")); //add node DOMElement* prodElem = doc->createElement(X("eid")); rootElem->appendChild(prodElem); DOMText* prodDataVal = doc->createTextNode(X("100000")); prodElem->appendChild(prodDataVal); XercesDOMParser* parser = new XercesDOMParser(); //add basic child MemBufInputSource* basic_mem = new MemBufInputSource( (const XMLByte* )basic_string.c_str(), strlen(basic_string.c_str()), "basic", false); parser->parse( *basic_mem ); DOMDocument* xmlDoc = parser->getDocument(); DOMElement* basic_root = xmlDoc->getDocumentElement(); DOMNode* newnode = doc->importNode((DOMNode* )basic_root, true); rootElem->appendChild(newnode); delete basic_mem; //add the rules child MemBufInputSource* rules_mem = new MemBufInputSource( (const XMLByte* )rules_string.c_str(), rules_string.length(), "rules", false); parser->parse( *rules_mem ); xmlDoc = parser->getDocument(); DOMElement* rules_root = xmlDoc->getDocumentElement(); newnode = doc->importNode((DOMNode* )rules_root, true); rootElem->appendChild(newnode); delete rules_mem; //add the accounts child MemBufInputSource* accounts_mem = new MemBufInputSource( (const XMLByte* )accounts_string.c_str(), accounts_string.length(), "accounts", false); parser->parse( *accounts_mem ); xmlDoc = parser->getDocument(); DOMElement* accounts_root = xmlDoc->getDocumentElement(); newnode = doc->importNode((DOMNode* )accounts_root, true); rootElem->appendChild(newnode); delete accounts_mem; theSerializer->write(doc, theOutputDesc); std::cout<< target->getRawBuffer()<< std::endl; delete target; delete parser; theOutputDesc->release(); theSerializer->release(); doc->release(); } catch(const OutOfMemoryException&) { XERCES_STD_QUALIFIER cerr << "OutOfMemoryException" << XERCES_STD_QUALIFIER endl; } catch (const DOMException& e) { XERCES_STD_QUALIFIER cerr << "DOMException code is: " << e.code << XERCES_STD_QUALIFIER endl; } catch (...) { XERCES_STD_QUALIFIER cerr << "An error occurred creating the document" << XERCES_STD_QUALIFIER endl; } } else { XERCES_STD_QUALIFIER cerr << "Requested implementation is not supported" << XERCES_STD_QUALIFIER endl; } XMLPlatformUtils::Terminate(); return 0; }