Chinaunix首页 | 论坛 | 博客
  • 博客访问: 536091
  • 博文数量: 55
  • 博客积分: 1520
  • 博客等级: 上尉
  • 技术积分: 661
  • 用 户 组: 普通用户
  • 注册时间: 2009-04-19 22:01
文章存档

2013年(1)

2012年(1)

2011年(7)

2010年(16)

2009年(30)

我的朋友

分类: C/C++

2010-03-02 17:03:23

#include
#include
#include
#include
#include
#include
#include
#include
#include

#include

#if defined(XERCES_NEW_IOSTREAMS)
#include
#else
#include
#endif

XERCES_CPP_NAMESPACE_USE

class XStr
{
        public:
                XStr(const char* const toTranscode)
                {
                        fUnicodeForm = XMLString::transcode(toTranscode);
                }
                ~XStr()
                {
                        XMLString::release(&fUnicodeForm);
                }
                const XMLCh* unicodeForm() const
                {
                        return fUnicodeForm;
                }
        private:
                XMLCh*   fUnicodeForm;
};

#define X(str) XStr(str).unicodeForm()

std::string xml_string =
""
""
"10001"
""
"pica"
""
""
""
""
""
"
"
""
""
""
""
""
""
""
""
""
""
""
""
""
""
""
""
""
"
"
""
""
""
""
""
""
""
""
""
""
""
""
"
"
""
""
""
""
""
""
""
""
""
""
""
""
""
""
""
"
"
"
";

XERCES_CPP_NAMESPACE_USE;

int store(std::string config[][4])
{
        DOMImplementation*  impl =  DOMImplementationRegistry::getDOMImplementation(X("LS"));
        DOMLSSerializer*    theSerializer = ((DOMImplementationLS*)impl)->createLSSerializer();
        DOMLSOutput*        theOutputDesc = ((DOMImplementationLS*)impl)->createLSOutput();
        MemBufFormatTarget* target = new MemBufFormatTarget();
        theOutputDesc->setByteStream(target);
        if (impl != NULL)
        {
                try
                {
                        MemBufInputSource* xml_mem = new MemBufInputSource(
                                        (const XMLByte* )xml_string.c_str(),
                                        xml_string.length(),
                                        "xml",
                                        false);

                        XercesDOMParser* parser = new XercesDOMParser();
                        parser->parse( *xml_mem );

                        DOMDocument* xml_doc = parser->getDocument();
                        DOMElement* xml_root = xml_doc->getDocumentElement();
                        if (NULL == xml_root)
                        {
                                fprintf(stderr, "XML is NULL! [%s:%d]\n", __FILE__, __LINE__);
                                delete xml_mem;
                                delete parser;
                                delete target;
                                theOutputDesc->release();
                                theSerializer->release();
                                return -1;
                        }

                        int i = 0;
                        std::string key_prev = "";
                        std::string key      = "";
                        std::string sql      = "";
                        std::string temp     = "";
                        while ( true )
                        {
                                if (key_prev=="-" && sql.length()!=0)
                                {
                                        //Execute SQL
                                        std::cout<< sql<< std::endl;
                                        sql = "";
                                }

                                key = config[i][0];
                                if (key.length() == 0)
                                {
                                        //写入数据库完毕
                                        delete xml_mem;
                                        delete parser;
                                        delete target;
                                        theOutputDesc->release();
                                        theSerializer->release();
                                        break;
                                }

                                if (key==key_prev && sql.length()!=0)
                                {
                                        sql += ",";
                                }

                                if ( sql.length()==0)
                                {
                                        sql = "update " + key + " set ";
                                }

                                std::string xpath = config[i][1];
                                //解析XPATH指定的结点
                                DOMXPathNSResolver* resolver = xml_doc->createNSResolver(xml_root);
                                DOMXPathResult* result = xml_doc->evaluate(
                                                X(xpath.c_str()),
                                                xml_root,
                                                resolver,
                                                DOMXPathResult::ORDERED_NODE_SNAPSHOT_TYPE,
                                                NULL);

                                XMLSize_t nLength = result->getSnapshotLength();
                                temp = "";
                                if (nLength > 0)
                                {
                                        result->snapshotItem(0);
                                        DOMNode*  curentNode = result->getNodeValue();
                                        if (*config[i][2].c_str() == '@')
                                                //代表要取相应结点的属性
                                        {
                                                DOMNamedNodeMap* attrs = curentNode->getAttributes();
                                                DOMAttr* attr = (DOMAttr*)attrs->getNamedItem(X("id"));
                                                if (NULL != attr)
                                                {
                                                        char* to_char = XMLString::transcode(attr->getValue());
                                                        temp = to_char;
                                                        free(to_char);
                                                }
                                        }
                                        else if(config[i][2] == "value")
                                                //代表要取相应结点的cdata
                                        {
                                                char* to_char = XMLString::transcode(curentNode->getTextContent());
                                                temp = to_char;
                                                free(to_char);
                                        }
                                        else
                                                //代表取子结点串
                                        {
                                                theSerializer->write(curentNode, theOutputDesc);
                                                temp = (char *)target->getRawBuffer();
                                        }
                                }

                                if (key != "-")
                                {
                                        sql = sql + config[i][3] + "=\"" + temp + "\"";
                                }
                                else
                                {
                                        if (temp.length() != 0)
                                                sql = sql + " where " + config[i][3] + "=\"" + temp + "\"";
                                        else
                                                std::cout<< "It`s dangerous!!!!!"<< std::endl; //sql将会针对全部数据进行操作,十分危险
                                }

                                result->release();
                                resolver->release();

                                key_prev = key;
                                ++i;
                        }//end of while
                        return 0;    
                }
                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;
                }//end of try
        }
        else
        {
                delete target;
                theOutputDesc->release();
                theSerializer->release();
                return -1;
        }//end of impl`s if
        return -1;
}

int main(int argc, char *argv[])
{

        XMLPlatformUtils::Initialize();
        std::string config[][4] = {
                {"corporation", "/corporation/eid", "@id", "eid"},
                {"corporation", "/corporation/basic/corp_name", "node", "corp_name"},
                {"-", "/corporation/eid/eeid", "value", "eid"},
                {"testable", "/corporation/rules/huabo", "node", "rules"},
                {"testable", "/corporation/accounts", "node", "accounts"},
                {"-", "/corporation/eid", "value", "eid"},
                {"", "", ""}
        };

        if (store(config))
        {
                std::cout<< "successful"<< std::endl;
        }
        XMLPlatformUtils::Terminate();
        return 0;
}

阅读(1969) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~