码迷,mamicode.com
首页 > 编程语言 > 详细

xerces-c++修改节点属性值

时间:2016-05-09 07:11:38      阅读:239      评论:0      收藏:0      [点我收藏+]

标签:message   include   public   

#include <syslog.h>
#include <xercesc/dom/DOM.hpp>
#include <xercesc/parsers/XercesDOMParser.hpp>
#include <xercesc/framework/LocalFileFormatTarget.hpp>
using namespace xercesc;
class XMLReader
{
    public:
        XMLReader(): m_configFileParser(NULL), m_rootNode(NULL) { };
        ~XMLReader();
        bool Initialize()
        {
            try
            {
                XMLPlatformUtils::Initialize();
            }
            catch( XMLException &e )
            {
                char *message = XMLString::transcode( e.getMessage() );
                syslog(LOG_ERR, "XML toolkit initialization error: %s", message);
                XMLString::release( &message );
                return false;
            }
            m_rootNode          = XMLString::transcode("beans");
            m_configFileParser = new XercesDOMParser;
            return true;
        };
        void readConfigFile(const char *);
    private:
        xercesc::XercesDOMParser *m_configFileParser;
        XMLCh* m_rootNode;
};
XMLReader::~XMLReader()
{
    try
    {
        delete m_configFileParser;
        m_configFileParser = NULL;
        XMLString::release( &m_rootNode );
    }
    catch( ... )
    {
        syslog(LOG_ERR, "Unknown exception encountered in TagNamesdtor");
    }
    try
    {
        XMLPlatformUtils::Terminate();
    }
    catch( xercesc::XMLException& e )
    {
        char *message = xercesc::XMLString::transcode( e.getMessage() );
        syslog(LOG_ERR, "XML ttolkit teardown error: %s" , message);
        XMLString::release( &message );
    }
}
void XMLReader::readConfigFile(const char *configFile)
{
    m_configFileParser->setValidationScheme( XercesDOMParser::Val_Never );
    m_configFileParser->setDoNamespaces( false );
    m_configFileParser->setDoSchema( false );
    m_configFileParser->setLoadExternalDTD( false );
    try
    {
        m_configFileParser->parse( configFile );
        DOMDocument* xmlDoc = m_configFileParser->getDocument();
        DOMElement* elementRoot = xmlDoc->getDocumentElement();
        if( !elementRoot )
        {
            syslog(LOG_ERR, "empty document!");
        };
        
        XMLCh *sslContextTag = XMLString::transcode("sslContext");
        //DOMNodeList *elementRoot = xmlDoc->getElementsByTagName(sslContextTag);
        DOMNodeList *children = xmlDoc->getElementsByTagName(sslContextTag); //elementRoot->getChildNodes();
        const  XMLSize_t nodeCount = children->getLength();
        XMLCh *keyStore      = XMLString::transcode("keyStore");
        XMLCh *keyStoreTag   = XMLString::transcode("keyStorePassword");
        XMLCh *trustStoreTag = XMLString::transcode("trustStorePassword");
        for( XMLSize_t xx = 0; xx < nodeCount; ++xx )
        {
            DOMNode* currentNode = children->item(xx);
            if( currentNode->getNodeType() == DOMNode::ELEMENT_NODE )
            {
                DOMElement* currentElement = dynamic_cast< xercesc::DOMElement* >( currentNode );
                if( XMLString::equals(currentElement->getTagName(), sslContextTag) && currentElement->hasAttribute(keyStore))
                {
                    XMLCh *xmlch_pass = XMLString::transcode("password");
                    currentElement->setAttribute(keyStoreTag  , xmlch_pass);
                    currentElement->setAttribute(trustStoreTag, xmlch_pass);
                    XMLString::release(&xmlch_pass);
                    break;
                }
            }
        }
        XMLString::release( &sslContextTag );
        XMLString::release( &keyStore );
        XMLString::release( &keyStoreTag );
        XMLString::release( &trustStoreTag );

        const XMLCh gLS[] = { chLatin_L, chLatin_S, chNull };
        DOMImplementation* impl = DOMImplementationRegistry::getDOMImplementation(gLS);
        DOMWriter* theSerializer = ((DOMImplementationLS*)impl)->createDOMWriter(); 
        if (theSerializer->canSetFeature(XMLUni::fgDOMXMLDeclaration,true) )
        {
            theSerializer->setFeature(XMLUni::fgDOMXMLDeclaration, true);
        }
        const char *outputXMLName = "test.xml";
        XMLFormatTarget *localFormTarget = new LocalFileFormatTarget(outputXMLName);
        try{
            theSerializer->writeNode(localFormTarget, *xmlDoc);
        }
        catch(...){
            syslog(LOG_ERR,"error");
        }
        delete localFormTarget;
    }
    catch( xercesc::XMLException& e )
    {
        char* message = xercesc::XMLString::transcode( e.getMessage() );
        syslog(LOG_ERR, "Error parsing file: %s" , message);
        XMLString::release( &message );
    }
}
#ifdef MAIN_TEST
/* This main is provided for unit test of the class. */
int main()
{
    const char *configFile ="activemq.xml";
    XMLReader appConfig;
    appConfig.Initialize();
    appConfig.readConfigFile(configFile);
    return 0;
}
#endif


xerces-c++修改节点属性值

标签:message   include   public   

原文地址:http://1470289.blog.51cto.com/1460289/1771332

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!