纯粹是试出来的结果,但是也值得总结。手里有模板,内容类似<id>{$id$}</id>,通过模板我要写入数据并且保存成所要的xml,项和模板一样,就是内容我现在有了。
所涉及到的操作:
/************************************************************************/ /* xxx */ /************************************************************************/ //CXML.cpp #include <string> #include <iostream> #include <sstream> #include <stdexcept> #include <list> #include <sys/types.h> #include <sys/stat.h> #include <errno.h> #include <iconv.h> #include "CXML.h " bool XMLStringTranslate::UTF8_2_GB2312(char *in, int inLen, char *out, int outLen) //码型转换 { const char *pin = in; char *pout = out; int inLen_ = inLen + 1; int outLen_ = outLen; iconv_t cd = iconv_open("gbk", "UTF-8 "); if((int)cd == -1) { return false; } iconv(cd, &pin, (size_t*)&inLen_, &pout, (size_t*)&outLen_); iconv_close(cd); return true; } bool XMLStringTranslate::TranslatorUTF8ToChinese(string &strTranslatorMsg) { char* pstrSource = const_cast <char*> (strTranslatorMsg.c_str()); char *pstrDestination = new char[strTranslatorMsg.length() * 21]; memset(pstrDestination, '/0 ', strTranslatorMsg.length() * 21); if(!UTF8_2_GB2312(pstrSource, strTranslatorMsg.length(), pstrDestination, strTranslatorMsg.length())) return false; strTranslatorMsg = pstrDestination; delete [] pstrDestination; return true; } CXML::CXML() { try { //Initialize Xerces-C library XMLPlatformUtils::Initialize(); } catch(xercesc::XMLException & excp) { char* msg = XMLString::transcode(excp.getMessage()); printf( "XML toolkit initialization error: %s/n ", msg); XMLString::release(&msg); } //XMLTan = new XMLStringTranslate("utf-8"); XMLTan = new XMLStringTranslate("gb2312"); //创建 XercesDOMParser 对象,用于解析文档 m_DOMXmlParser = new XercesDOMParser; } CXML::~CXML() { try { delete XMLTan; XMLPlatformUtils::Terminate(); } catch(XMLException& excp) { char* msg = XMLString::transcode(excp.getMessage()); printf( "XML toolkit terminate error: %s/n ", msg); XMLString::release(&msg); } } /* GetNodeValue(char *pszString, char *pstr) * @in pstring for the original string * @out the result string * eg: {$name$} for in and name for out * return: success for 0, false for other */ int GetNodeValue(char *pszString, char *pstr) { char *pos; int nFind; if (pszString == NULL) return -1; nFind = 0; for (pos = pszString; *pos != '/0' && *pos != NULL; pos++) { if (*pos == '$' && *(pos + 1) == '}') { *pstr = '/0'; break; } if (*pos == '{' && *(pos + 1) == '$') { nFind = 1; ++pos; ++pos; } *pstr = *pos; pstr++; } if (nFind == 1) return 0; else return -1; } void CXML::xmlParser(string & xmlFile) throw(std::runtime_error) { //获取文件信息状态 struct stat fileStatus; char *pszNodeValue; char szBakFile[256]; bool bRet = true; XMLCh tempStr[100]; DOMNode *pNodeName; XMLString::transcode("LS", tempStr, 99); XMLFormatTarget* FormatTargetPtr; DOMNodeList *NodeListPtr = NULL; int iretStat = stat(xmlFile.c_str(), &fileStatus); if(iretStat == ENOENT) throw (std::runtime_error( "file_name does not exist, or path is an empty string. ") ); else if(iretStat == ENOTDIR) throw (std::runtime_error( "A component of the path is not a directory. ")); // else if( iretStat == ELOOP) // throw ( std::runtime_error( "Too many symbolic links encountered while traversing the path. ")); else if(iretStat == EACCES) throw (std::runtime_error( "ermission denied. ")); else if(iretStat == ENAMETOOLONG) throw (std::runtime_error("File can not be read/n ")); //配置DOMParser m_DOMXmlParser->setValidationScheme(XercesDOMParser::Val_Auto); m_DOMXmlParser->setDoNamespaces(false); m_DOMXmlParser->setDoSchema(false); m_DOMXmlParser->setLoadExternalDTD(false); try { //调用 Xerces C 类库提供的解析接口 m_DOMXmlParser->parse(xmlFile.c_str()); //获得DOM树 xmlDoc = m_DOMXmlParser->getDocument(); pRoot = xmlDoc->getDocumentElement(); if (!pRoot) { throw(std::runtime_error("empty XML document ")); } // create an iterator to visit all text nodes. DOMNodeIterator* iterator = xmlDoc->createNodeIterator(pRoot, DOMNodeFilter::SHOW_TEXT, NULL, true); for (DOMNode * current = iterator->nextNode(); current != 0; current = iterator->nextNode()) { string strValue = XMLTan->translate(current->getNodeValue()); XMLTan->TranslatorUTF8ToChinese(strValue); std::cout<<strValue<<endl; strcpy(szBakFile, strValue.c_str()); pszNodeValue = new char[sizeof(szBakFile)]; if (pszNodeValue == NULL) return; if (GetNodeValue(szBakFile, pszNodeValue) != 0) continue; std::cout << pszNodeValue << endl; NodeListPtr = xmlDoc->getElementsByTagName(X(pszNodeValue)); pNodeName = NodeListPtr->item(0); if (pNodeName == NULL) continue; pNodeName->setTextContent(X(" ")); } } catch(xercesc::XMLException & excp) { char* msg = xercesc::XMLString::transcode(excp.getMessage()); ostringstream errBuf; XMLString::release(&msg); } DOMImplementation *impl = DOMImplementationRegistry::getDOMImplementation(tempStr); DOMWriter* DOMWriterPtr = ((DOMImplementationLS*)impl)->createDOMWriter(); FormatTargetPtr = new LocalFileFormatTarget("C://Documents and Settings//Administrator//桌面//ttttt.xml"); bRet = DOMWriterPtr->writeNode(FormatTargetPtr, *xmlDoc); } XMLStringTranslate::XMLStringTranslate(const char * const encoding):fFormatter(0), m_value(0),fEncodingUsed(0),toFill(0) { XMLFormatTarget * myFormTarget = this; fEncodingUsed = XMLString::transcode(encoding); fFormatter = new XMLFormatter(fEncodingUsed, myFormTarget, XMLFormatter::NoEscapes, XMLFormatter::UnRep_CharRef); toFill=new XMLCh[kTmpBufSize]; clearbuffer(); } XMLStringTranslate::~XMLStringTranslate() { if(fFormatter) delete fFormatter; if(m_value) free(m_value); if(toFill) free(toFill); fFormatter=0; fEncodingUsed=0; m_value=0; toFill=0; } void XMLStringTranslate::writeChars(const XMLByte* const toWrite, const unsigned int count, XMLFormatter* const formatter) { if(m_value) free(m_value); m_value=0; m_value=new char[count + 1]; memset(m_value,0,count + 1); memcpy(m_value,(char *)toWrite,count + 1); } void XMLStringTranslate::clearbuffer() { if(!toFill) return; for(int i=0; i < kTmpBufSize; i++) toFill[i]=0; } string XMLStringTranslate::translate(const XMLCh* const value) //实现从 XMLCh* 到 string类型的转换 { *fFormatter << value; string strValue = string(m_value); return strValue; } const XMLCh * const XMLStringTranslate::translate(const char * const value) { clearbuffer(); const unsigned int srcCount=XMLString::stringLen(value); unsigned char fCharSizeBuf[kCharBufSize]; XMLTranscoder * pTranscoder=(XMLTranscoder *)fFormatter->getTranscoder(); unsigned int bytesEaten; unsigned int size=pTranscoder->transcodeFrom((XMLByte *)value, srcCount, toFill, kTmpBufSize, bytesEaten, fCharSizeBuf); toFill[size]=0; string t1=string(value); string t2=translate(toFill); assert(t1==t2); return toFill; }
头文件
/************************************************************************/ /* */ /************************************************************************/ //CXML.h #ifndef XML_PARSER_HPP #define XML_PARSER_HPP #include <xercesc/util/TransService.hpp> #include <xercesc/dom/DOM.hpp> #include <xercesc/dom/DOMDocument.hpp> #include <xercesc/dom/DOMDocumentType.hpp> #include <xercesc/dom/DOMElement.hpp> #include <xercesc/dom/DOMImplementation.hpp> #include <xercesc/dom/DOMImplementationLS.hpp> #include <xercesc/dom/DOMNodeIterator.hpp> #include <xercesc/dom/DOMNodeList.hpp> #include <xercesc/dom/DOMText.hpp> #include <xercesc/dom/DOMAttr.hpp> #include <xercesc/parsers/XercesDOMParser.hpp> #include <xercesc/util/XMLUni.hpp> #include <xercesc/framework/XMLFormatter.hpp> #include <xercesc/util/XMLString.hpp> #include <xercesc/framework/LocalFileFormatTarget.hpp> #include <stdlib.h> #include <string> #include <vector> #include <stdexcept> using namespace std; using namespace xercesc; class XMLStringTranslate; class CXML { public: CXML(); ~CXML(); //XMLTransService::Codes tranServiceCode; void xmlParser(string&) throw(std::runtime_error); private: DOMDocument* xmlDoc; DOMElement *pRoot; XMLStringTranslate *XMLTan; xercesc::XercesDOMParser *m_DOMXmlParser; //定义解析对象 }; class XMLStringTranslate : public XMLFormatTarget { public: XMLStringTranslate(const char * const encoding); bool TranslatorUTF8ToChinese(string &strTranslatorMsg); bool UTF8_2_GB2312(char *in, int inLen, char *out, int outLen); string translate(const XMLCh* const value); const XMLCh * const translate(const char * const value); virtual ~XMLStringTranslate(); protected: XMLFormatter * fFormatter; XMLCh * fEncodingUsed; XMLCh * toFill; char * m_value; private: enum Constants { kTmpBufSize = 16 * 1024, kCharBufSize = 16 * 1024 }; void clearbuffer(); virtual void writeChars(const XMLByte* const toWrite, const unsigned int count, XMLFormatter* const formatter); }; /******************************************************************************** * This is a simple class that lets us do easy (though not terribly efficient) * trancoding of char* data to XMLCh data. ********************************************************************************/ class XStr { public : /* Constructors and Destructor */ XStr(const char* const toTranscode) { /* Call the private transcoding method */ fUnicodeForm = XMLString::transcode(toTranscode); } ~XStr() { XMLString::release(&fUnicodeForm); } /* Getter methods */ const XMLCh* unicodeForm() const { return fUnicodeForm; } private : /* * Private data member * * fUnicodeForm * This is the Unicode XMLCh format of the string. */ XMLCh* fUnicodeForm; }; #define X(str) XStr(str).unicodeForm() #endif
在生成xml后对照发现,我的思路是错的,按照原来的意思,应该是<id>{$id$}</id>,但会出现<id>{$taskid$}</id>欠缺的是找到一个方法,知道DOMNode的项的名字。。