溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

xerces-c++修改節(jié)點(diǎn)屬性值

發(fā)布時(shí)間:2020-08-03 22:09:52 來源:網(wǎng)絡(luò) 閱讀:869 作者:kmpei17 欄目:編程語言
#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


向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI