xmldomutil.cpp
来自「IBM的解析xml的工具Xerces的源代码」· C++ 代码 · 共 937 行 · 第 1/2 页
CPP
937 行
{ pObj->Release(); return E_FAIL; } hr = pObj->QueryInterface(iid, pVal); if (S_OK != hr) *pVal = NULL; pObj->Release(); break; } case DOMNode::COMMENT_NODE: { CXMLDOMComment *pObj = NULL; hr = CPooledXMLDOMCommentObj::CreateInstance(&pObj); if (S_OK != hr) return hr; pObj->AddRef(); pObj->SetOwnerDoc(pDoc); try { pObj->comment = static_cast<DOMComment*> (node); } catch(DOMException& ex) { pObj->Release(); return MakeHRESULT(ex); } catch(...) { pObj->Release(); return E_FAIL; } hr = pObj->QueryInterface(iid, pVal); if (S_OK != hr) *pVal = NULL; pObj->Release(); break; } case DOMNode::DOCUMENT_NODE: { CXMLDOMDocumentObj *pObj = NULL; hr = CXMLDOMDocumentObj::CreateInstance(&pObj); if (S_OK != hr) return hr; pObj->AddRef(); pObj->SetOwnerDoc(pDoc); try { pObj->m_Document = static_cast<XERCES_CPP_NAMESPACE_QUALIFIER DOMDocument*> (node); } catch(DOMException& ex) { pObj->Release(); return MakeHRESULT(ex); } catch(...) { pObj->Release(); return E_FAIL; } hr = pObj->QueryInterface(iid, pVal); if (S_OK != hr) *pVal = NULL; pObj->Release(); break; } case DOMNode::DOCUMENT_TYPE_NODE: { CXMLDOMDocumentTypeObj *pObj = NULL; hr = CXMLDOMDocumentTypeObj::CreateInstance(&pObj); if (S_OK != hr) return hr; pObj->AddRef(); pObj->SetOwnerDoc(pDoc); try { pObj->documentType = static_cast<DOMDocumentType*> (node); } catch(DOMException& ex) { pObj->Release(); return MakeHRESULT(ex); } catch(...) { pObj->Release(); return E_FAIL; } hr = pObj->QueryInterface(iid, pVal); if (S_OK != hr) *pVal = NULL; pObj->Release(); break; } case DOMNode::DOCUMENT_FRAGMENT_NODE: { CXMLDOMDocumentFragmentObj *pObj = NULL; hr = CXMLDOMDocumentFragmentObj::CreateInstance(&pObj); if (S_OK != hr) return hr; pObj->AddRef(); pObj->SetOwnerDoc(pDoc); try { pObj->documentFragment = static_cast<DOMDocumentFragment*> (node); } catch(DOMException& ex) { pObj->Release(); return MakeHRESULT(ex); } catch(...) { pObj->Release(); return E_FAIL; } hr = pObj->QueryInterface(iid, pVal); if (S_OK != hr) *pVal = NULL; pObj->Release(); break; } case DOMNode::NOTATION_NODE: { CXMLDOMNotationObj *pObj = NULL; hr = CXMLDOMNotationObj::CreateInstance(&pObj); if (S_OK != hr) return hr; pObj->AddRef(); pObj->SetOwnerDoc(pDoc); try { pObj->notation = static_cast<DOMNotation*> (node); } catch(DOMException& ex) { pObj->Release(); return MakeHRESULT(ex); } catch(...) { pObj->Release(); return E_FAIL; } hr = pObj->QueryInterface(iid, pVal); if (S_OK != hr) *pVal = NULL; pObj->Release(); break; } default: hr = E_NOTIMPL; break; } return hr;}class xmlstream {public: xmlstream() { m_length = 0; m_alloc = 0; m_buffer = 0; m_next = 0; } ~xmlstream() { delete [] m_buffer; } xmlstream& operator<<(const XMLCh* other) { // // get length of string // unsigned long len = 0; for(const XMLCh* source = other; *source; source++,len++); // // append to stream // append(other,len); return *this; } xmlstream& operator<<(const XMLCh other) { append(&other,1); return *this; } BSTR SysAllocString() { if(m_length > 0) return SysAllocStringLen(m_buffer,m_length); return 0; }private: void append(const XMLCh* other,unsigned long length) { const XMLCh* source = NULL; if(m_length + length > m_alloc) { unsigned long chunk = 4096; if(length > chunk) chunk += length; XMLCh* newbuf = new XMLCh[m_alloc+ chunk]; m_alloc += chunk; m_next = newbuf + m_length; // // copy old content into new buffer // XMLCh* dest = newbuf; source = m_buffer; for(unsigned long i = 0; i < m_length; i++,dest++,source++) { *dest = *source; } delete [] m_buffer; m_buffer = newbuf; } source = other; for(unsigned long i = 0; i < length; i++,source++,m_next++) { *m_next = *source; } m_length += length; } unsigned long m_length; unsigned long m_alloc; XMLCh* m_buffer; XMLCh* m_next;};// ---------------------------------------------------------------------------// outputContent//// Write document content from a string to a C++ ostream. Escape the// XML special characters (<, &, etc.) unless this is suppressed by the// command line option.// ---------------------------------------------------------------------------void outputContent(xmlstream& target, const XMLCh* toWrite){ int length = XMLString::stringLen(toWrite); int index; for (index = 0; index < length; index++) { switch (toWrite[index]) { case chAmpersand : target << XMLStrL("&"); break; case chOpenAngle : target << XMLStrL("<"); break; case chCloseAngle: target << XMLStrL(">"); break; case chDoubleQuote : target << XMLStrL("""); break; default: // If it is none of the special characters, print it as such target << toWrite[index]; break; } } return;}xmlstream& operator<<(xmlstream& target, const DOMNode* toWrite){ // Get the name and value out for convenience const XMLCh* nodeName = toWrite->getNodeName(); const XMLCh* nodeValue = toWrite->getNodeValue(); switch (toWrite->getNodeType()) { case DOMNode::TEXT_NODE: { outputContent(target, nodeValue); break; } case DOMNode::PROCESSING_INSTRUCTION_NODE : { target << XMLStrL("<?") << nodeName << XMLStrL(' ') << nodeValue << XMLStrL("?>"); break; } case DOMNode::DOCUMENT_NODE : { // // Bug here: we need to find a way to get the encoding name // for the default code page on the system where the program // is running, and plug that in for the encoding name. // XERCES_CPP_NAMESPACE_QUALIFIER DOMDocument* document=(XERCES_CPP_NAMESPACE_QUALIFIER DOMDocument*)toWrite; target << XMLStrL("<?xml version=\"") << document->getVersion() << XMLStrL("\""); const XMLCh* str = document->getEncoding(); if (str != 0) target << XMLStrL(" encoding=\"") << str << XMLStrL("\""); if(document->getStandalone()) target << XMLStrL(" standalone=\"yes\""); target << XMLStrL("?>"); DOMNode* child = toWrite->getFirstChild(); while( child != 0) { target << child; child = child->getNextSibling(); } break; } case DOMNode::ELEMENT_NODE : { // Output the element start tag. target << XMLStrL('<') << nodeName; // Output any attributes on this element DOMNamedNodeMap* attributes = toWrite->getAttributes(); int attrCount = attributes->getLength(); for (int i = 0; i < attrCount; i++) { DOMNode* attribute = attributes->item(i); target << XMLStrL(' ') << attribute->getNodeName() << XMLStrL(" = \""); // Note that "<" must be escaped in attribute values. outputContent(target, attribute->getNodeValue()); target << XMLStrL('"'); } // // Test for the presence of children, which includes both // text content and nested elements. // DOMNode* child = toWrite->getFirstChild(); if (child != 0) { // There are children. Close start-tag, and output children. target << XMLStrL(">"); while( child != 0) { target << child; child = child->getNextSibling(); } // Done with children. Output the end tag. target << XMLStrL("</") << nodeName << XMLStrL(">"); } else { // // There were no children. Output the short form close of // the element start tag, making it an empty-element tag. // target << XMLStrL("/>"); } break; } case DOMNode::ENTITY_REFERENCE_NODE: { DOMNode* child; for (child = toWrite->getFirstChild(); child != 0; child = child->getNextSibling()) target << child; break; } case DOMNode::CDATA_SECTION_NODE: { target << XMLStrL("<![CDATA[") << nodeValue << XMLStrL("]]>"); break; } case DOMNode::COMMENT_NODE: { target << XMLStrL("<!--") << nodeValue << XMLStrL("-->"); break; } case DOMNode::DOCUMENT_TYPE_NODE: { DOMDocumentType* doctype = (DOMDocumentType*)toWrite;; target << XMLStrL("<!DOCTYPE ") << nodeName ; const XMLCh* id = doctype->getPublicId(); if (id != 0) target << XMLStrL(" PUBLIC \"") << id << XMLStrL("\""); id = doctype->getSystemId(); if (id != 0) target << XMLStrL(" SYSTEM \"") << id << XMLStrL("\""); id = doctype->getInternalSubset(); if (id !=0) target << XMLStrL(" [ ") << id << XMLStrL("]"); target << XMLStrL(">"); break; } case DOMNode::ENTITY_NODE: { DOMEntity* entity = (DOMEntity*)toWrite;; target << XMLStrL("<!ENTITY ") << nodeName; const XMLCh* id = entity->getPublicId(); if (id != 0) target << XMLStrL("PUBLIC \"") << id << XMLStrL("\""); id = entity->getSystemId(); if (id != 0) target << XMLStrL("SYSTEM \"") << id << XMLStrL("\""); id = entity->getNotationName(); if (id != 0) target << XMLStrL("NDATA \"") << id << XMLStrL("\""); break; } default: target << XMLStrL("<!-- Unrecognized node type -->"); } return target;}void GetXML(const DOMNode* node, _bstr_t &text){ xmlstream stream; stream << node; text = _bstr_t(stream.SysAllocString(),false);}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?