温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

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

发布时间:2020-08-03 22:09:52 来源:网络 阅读:862 作者: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问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI