Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/154.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xerces C++;-加载、读取和保存备选方案? 我正在寻找一个教程来加载一个XML文件,读它,修改它,最后用C++保存它。我正在使用Linux Ubuntu并尝试使用Xerces。有了Google和很多时间,我只能加载一个XML文件: #include <xercesc/parsers/XercesDOMParser.hpp> #include <xercesc/dom/DOM.hpp> #include <xercesc/sax/HandlerBase.hpp> #include <xercesc/util/XMLString.hpp> #include <xercesc/util/PlatformUtils.hpp> #include <iostream> using namespace std; using namespace xercesc; int main (int argc, char* args[]) { try { XMLPlatformUtils::Initialize(); } catch (const XMLException& toCatch) { char* message = XMLString::transcode(toCatch.getMessage()); cout << "Error during initialization! :\n" << message << "\n"; XMLString::release(&message); return 1; } XercesDOMParser* parser = new XercesDOMParser(); parser->setValidationScheme(XercesDOMParser::Val_Always); parser->setDoNamespaces(true); // optional ErrorHandler* errHandler = (ErrorHandler*) new HandlerBase(); parser->setErrorHandler(errHandler); const char* xmlFile = "demo.xml"; try { parser->parse(xmlFile); } catch (const XMLException& toCatch) { char* message = XMLString::transcode(toCatch.getMessage()); cout << "Exception message is: \n" << message << "\n"; XMLString::release(&message); return -1; } catch (const DOMException& toCatch) { char* message = XMLString::transcode(toCatch.msg); cout << "Exception message is: \n" << message << "\n"; XMLString::release(&message); return -1; } catch (...) { cout << "Unexpected Exception \n" ; return -1; } DOMNode* docRootNode; // DOMNode* aNode; DOMDocument* doc; doc = parser->getDocument(); docRootNode = doc->getDocumentElement(); cout << docRootNode->getAttributes() << endl; //returns Hex delete parser; delete errHandler; return 0; } #包括 #包括 #包括 #包括 #包括 #包括 使用名称空间std; 使用名称空间xercesc; int main(int argc,char*args[]{ 试一试{ XMLPlatformUtils::Initialize(); } catch(constxmleexception&toCatch){ char*message=XMLString::transcode(toCatch.getMessage()); cout setErrorHandler(errHandler); const char*xmlFile=“demo.xml”; 试一试{ 解析器->解析(xmlFile); } catch(constxmleexception&toCatch){ char*message=XMLString::transcode(toCatch.getMessage()); CUT LBXML +是C++的最佳选择,功能非常完善,包括XPath、字符集转换(Glibmm)XML库中的所有东西,它使用传统的DOM和SAX API,这取决于你问的对象,一个可能的问题是库的依赖性非常重(由于使用Glibmm),它似乎是C++唯一合适的XML库。_C++_Xml_Linux_Xerces - Fatal编程技术网 解析(xmlFile); } catch(constxmleexception&toCatch){ char*message=XMLString::transcode(toCatch.getMessage()); CUT LBXML +是C++的最佳选择,功能非常完善,包括XPath、字符集转换(Glibmm)XML库中的所有东西,它使用传统的DOM和SAX API,这取决于你问的对象,一个可能的问题是库的依赖性非常重(由于使用Glibmm),它似乎是C++唯一合适的XML库。,c++,xml,linux,xerces,C++,Xml,Linux,Xerces" /> 解析(xmlFile); } catch(constxmleexception&toCatch){ char*message=XMLString::transcode(toCatch.getMessage()); CUT LBXML +是C++的最佳选择,功能非常完善,包括XPath、字符集转换(Glibmm)XML库中的所有东西,它使用传统的DOM和SAX API,这取决于你问的对象,一个可能的问题是库的依赖性非常重(由于使用Glibmm),它似乎是C++唯一合适的XML库。,c++,xml,linux,xerces,C++,Xml,Linux,Xerces" />

Xerces C++;-加载、读取和保存备选方案? 我正在寻找一个教程来加载一个XML文件,读它,修改它,最后用C++保存它。我正在使用Linux Ubuntu并尝试使用Xerces。有了Google和很多时间,我只能加载一个XML文件: #include <xercesc/parsers/XercesDOMParser.hpp> #include <xercesc/dom/DOM.hpp> #include <xercesc/sax/HandlerBase.hpp> #include <xercesc/util/XMLString.hpp> #include <xercesc/util/PlatformUtils.hpp> #include <iostream> using namespace std; using namespace xercesc; int main (int argc, char* args[]) { try { XMLPlatformUtils::Initialize(); } catch (const XMLException& toCatch) { char* message = XMLString::transcode(toCatch.getMessage()); cout << "Error during initialization! :\n" << message << "\n"; XMLString::release(&message); return 1; } XercesDOMParser* parser = new XercesDOMParser(); parser->setValidationScheme(XercesDOMParser::Val_Always); parser->setDoNamespaces(true); // optional ErrorHandler* errHandler = (ErrorHandler*) new HandlerBase(); parser->setErrorHandler(errHandler); const char* xmlFile = "demo.xml"; try { parser->parse(xmlFile); } catch (const XMLException& toCatch) { char* message = XMLString::transcode(toCatch.getMessage()); cout << "Exception message is: \n" << message << "\n"; XMLString::release(&message); return -1; } catch (const DOMException& toCatch) { char* message = XMLString::transcode(toCatch.msg); cout << "Exception message is: \n" << message << "\n"; XMLString::release(&message); return -1; } catch (...) { cout << "Unexpected Exception \n" ; return -1; } DOMNode* docRootNode; // DOMNode* aNode; DOMDocument* doc; doc = parser->getDocument(); docRootNode = doc->getDocumentElement(); cout << docRootNode->getAttributes() << endl; //returns Hex delete parser; delete errHandler; return 0; } #包括 #包括 #包括 #包括 #包括 #包括 使用名称空间std; 使用名称空间xercesc; int main(int argc,char*args[]{ 试一试{ XMLPlatformUtils::Initialize(); } catch(constxmleexception&toCatch){ char*message=XMLString::transcode(toCatch.getMessage()); cout setErrorHandler(errHandler); const char*xmlFile=“demo.xml”; 试一试{ 解析器->解析(xmlFile); } catch(constxmleexception&toCatch){ char*message=XMLString::transcode(toCatch.getMessage()); CUT LBXML +是C++的最佳选择,功能非常完善,包括XPath、字符集转换(Glibmm)XML库中的所有东西,它使用传统的DOM和SAX API,这取决于你问的对象,一个可能的问题是库的依赖性非常重(由于使用Glibmm),它似乎是C++唯一合适的XML库。

Xerces C++;-加载、读取和保存备选方案? 我正在寻找一个教程来加载一个XML文件,读它,修改它,最后用C++保存它。我正在使用Linux Ubuntu并尝试使用Xerces。有了Google和很多时间,我只能加载一个XML文件: #include <xercesc/parsers/XercesDOMParser.hpp> #include <xercesc/dom/DOM.hpp> #include <xercesc/sax/HandlerBase.hpp> #include <xercesc/util/XMLString.hpp> #include <xercesc/util/PlatformUtils.hpp> #include <iostream> using namespace std; using namespace xercesc; int main (int argc, char* args[]) { try { XMLPlatformUtils::Initialize(); } catch (const XMLException& toCatch) { char* message = XMLString::transcode(toCatch.getMessage()); cout << "Error during initialization! :\n" << message << "\n"; XMLString::release(&message); return 1; } XercesDOMParser* parser = new XercesDOMParser(); parser->setValidationScheme(XercesDOMParser::Val_Always); parser->setDoNamespaces(true); // optional ErrorHandler* errHandler = (ErrorHandler*) new HandlerBase(); parser->setErrorHandler(errHandler); const char* xmlFile = "demo.xml"; try { parser->parse(xmlFile); } catch (const XMLException& toCatch) { char* message = XMLString::transcode(toCatch.getMessage()); cout << "Exception message is: \n" << message << "\n"; XMLString::release(&message); return -1; } catch (const DOMException& toCatch) { char* message = XMLString::transcode(toCatch.msg); cout << "Exception message is: \n" << message << "\n"; XMLString::release(&message); return -1; } catch (...) { cout << "Unexpected Exception \n" ; return -1; } DOMNode* docRootNode; // DOMNode* aNode; DOMDocument* doc; doc = parser->getDocument(); docRootNode = doc->getDocumentElement(); cout << docRootNode->getAttributes() << endl; //returns Hex delete parser; delete errHandler; return 0; } #包括 #包括 #包括 #包括 #包括 #包括 使用名称空间std; 使用名称空间xercesc; int main(int argc,char*args[]{ 试一试{ XMLPlatformUtils::Initialize(); } catch(constxmleexception&toCatch){ char*message=XMLString::transcode(toCatch.getMessage()); cout setErrorHandler(errHandler); const char*xmlFile=“demo.xml”; 试一试{ 解析器->解析(xmlFile); } catch(constxmleexception&toCatch){ char*message=XMLString::transcode(toCatch.getMessage()); CUT LBXML +是C++的最佳选择,功能非常完善,包括XPath、字符集转换(Glibmm)XML库中的所有东西,它使用传统的DOM和SAX API,这取决于你问的对象,一个可能的问题是库的依赖性非常重(由于使用Glibmm),它似乎是C++唯一合适的XML库。,c++,xml,linux,xerces,C++,Xml,Linux,Xerces,TinyXML不按照规范解析XML,因此我建议不要使用它,即使它适用于简单的文档。Xerces附带的示例CreateDOMDocument向您展示了如何将节点等添加到DOM文档中。到目前为止,您使用的代码创建了文档,因此您需要将第二个示例中的代码改编为dd节点、属性等 另外,请注意,当你说: cout << docRootNode->getAttributes() << endl; cout-getAttributes()如果您想查看如何使用Xerces-C+


TinyXML不按照规范解析XML,因此我建议不要使用它,即使它适用于简单的文档。

Xerces附带的示例CreateDOMDocument向您展示了如何将节点等添加到DOM文档中。到目前为止,您使用的代码创建了文档,因此您需要将第二个示例中的代码改编为dd节点、属性等

另外,请注意,当你说:

 cout << docRootNode->getAttributes() << endl; 

cout-getAttributes()如果您想查看如何使用Xerces-C++执行此操作的示例,请查看以下代码:


很久以前,我是作为一个大学项目写的。它很可能是基于一个过时的Xerces-C++版本,但我认为API的变化不会太大,不会成为一个问题。它至少会给你一个想法。

一个保存Xerces DOMDocument的示例。使用

DOMLSSerializer::write(const DOMNode* nodeToWrite, DOMLSOutput* const destination)
参见代码示例

还有一个过滤器实现的示例。您可以在DOMPrint示例中找到这一点

class DOMPrintFilter : public DOMLSSerializerFilter {
public:

    DOMPrintFilter(ShowType whatToShow = DOMNodeFilter::SHOW_ALL);
    ~DOMPrintFilter(){};

    virtual FilterAction acceptNode(const DOMNode*) const;
    virtual ShowType getWhatToShow() const {return fWhatToShow;};

private:
    // unimplemented copy ctor and assignement operator
    DOMPrintFilter(const DOMPrintFilter&);
    DOMPrintFilter & operator = (const DOMPrintFilter&);

    ShowType fWhatToShow;
};
#include "DOMPrintFilter.hpp"
#include <xercesc/util/XMLUniDefs.hpp>
#include <xercesc/util/XMLString.hpp>

static const XMLCh  element_person[]=
{
chLatin_p, chLatin_e, chLatin_r, chLatin_s, chLatin_o, chLatin_n, chNull
};

static const XMLCh  element_link[]=
{
chLatin_l, chLatin_i, chLatin_n, chLatin_k, chNull
};

DOMPrintFilter::DOMPrintFilter(ShowType whatToShow)
:fWhatToShow(whatToShow)
{}

DOMNodeFilter::FilterAction DOMPrintFilter::
acceptNode(const DOMNode* node) const
{
//
// The DOMLSSerializer shall call getWhatToShow() before calling
// acceptNode(), to show nodes which are supposed to be
// shown to this filter.
//
// REVISIT: In case the DOMLSSerializer does not follow the protocol,
//          Shall the filter honour, or NOT, what it claims
//          it is interested in ?
//
// The DOMLS specs does not specify that acceptNode() shall do
// this way, or not, so it is up the implementation,
// to skip the code below for the sake of performance ...
//
if ((getWhatToShow() & (1 << (node->getNodeType() - 1))) == 0)
    return DOMNodeFilter::FILTER_ACCEPT;

switch (node->getNodeType())
{
case DOMNode::ELEMENT_NODE:
    {
        // for element whose name is "person", skip it
        if (XMLString::compareString(node->getNodeName(),          element_person)==0)
            return DOMNodeFilter::FILTER_SKIP;
        // for element whose name is "line", reject it
        if (XMLString::compareString(node->getNodeName(), element_link)==0)
            return DOMNodeFilter::FILTER_REJECT;
        // for rest, accept it
        return DOMNodeFilter::FILTER_ACCEPT;

        break;
    }
case DOMNode::COMMENT_NODE:
    {
        // the WhatToShow will make this no effect
        return DOMNodeFilter::FILTER_REJECT;
        break;
    }
case DOMNode::TEXT_NODE:
    {
        // the WhatToShow will make this no effect
        return DOMNodeFilter::FILTER_REJECT;
        break;
    }
case DOMNode::DOCUMENT_TYPE_NODE:
    {
        // even we say we are going to process document type,
        // we are not able be to see this node since
        // DOMLSSerializerImpl (a XercesC's default implementation
        // of DOMLSSerializer) will not pass DocumentType node to
        // this filter.
        //
        return DOMNodeFilter::FILTER_REJECT;  // no effect
        break;
    }
case DOMNode::DOCUMENT_NODE:
    {
        // same as DOCUMENT_NODE
        return DOMNodeFilter::FILTER_REJECT;  // no effect
        break;
    }
default :
    {
        return DOMNodeFilter::FILTER_ACCEPT;
        break;
    }
}

return DOMNodeFilter::FILTER_ACCEPT;
}
类DOMPrintFilter:public domlserializerfilter{
公众:
DOMPrintFilter(ShowType whatToShow=DOMNodeFilter::SHOW_ALL);
~DOMPrintFilter(){};
虚拟FilterAction acceptNode(const DOMNode*)const;
虚拟ShowType getWhatToShow()常量{return fWhatToShow;};
私人:
//未实现的复制运算符和赋值运算符
DOMPrintFilter(constdomprintfilter&);
DOMPrintFilter&运算符=(const DOMPrintFilter&);
ShowType fWhatToShow;
};
#包括“DOMPrintFilter.hpp”
#包括
#包括
静态常量XMLCh元素_person[]=
{
chLatin_p,chLatin_e,chLatin_r,chLatin_s,chLatin_o,chLatin_n,chNull
};
静态常量XMLCh元素_链接[]=
{
chLatin_l,chLatin_i,chLatin_n,chLatin_k,chNull
};
DOMPrintFilter::DOMPrintFilter(ShowType whatToShow)
:fWhatToShow(whatToShow)
{}
DOMNodeFilter::FilterAction DOMPrintFilter::
acceptNode(const DOMNode*node)const
{
//
//DOMLSerializer应在调用之前调用getWhatToShow()
//acceptNode(),以显示假定为
//显示给此筛选器。
//
//重访:如果domlserializer不遵循协议,
//过滤器是否应尊重其声称的内容
//它对什么感兴趣?
//
//DOMLS规范没有指定acceptNode()应该做什么
//这样也好,不这样也好,所以这取决于执行情况,
//为了提高性能,要跳过下面的代码。。。
//
if((getWhatToShow()&(1 getNodeType()-1))==0)
返回DOMNodeFilter::FILTER\u ACCEPT;
开关(节点->getNodeType())
{
案例DOMNode::元素\节点:
{
//对于名称为“person”的元素,跳过它
if(XMLString::compareString(node->getNodeName(),element_person)==0)
返回DOMNodeFilter::FILTER\u SKIP;
//对于名称为“line”的元素,拒绝它
if(XMLString::compareString(节点->getNodeName(),元素链接)==0)
返回DOMNodeFilter::FILTER\u拒绝;
//剩下的,接受它吧
返回DOMNodeFilter::FILTER\u ACCEPT;
打破
}
case DOMNode::COMMENT_NODE:
{
//WhatToShow将使此无效
返回DOMNodeFilter::FILTER\u拒绝;
打破
}
case DOMNode::TEXT_节点:
{
//WhatToShow将使此无效
返回DOMNodeFilter::FILTER\u拒绝;
打破
}
案例DOMNode::文档类型\u节点:
{
//即使我们说要处理文档类型,
//我们无法看到此节点,因为
//DomlSerializerImpl(XercesC的默认实现
//DomlSerializer的)将不会将DocumentType节点传递给
//这个过滤器。
//
return DOMNodeFilter::FILTER_REJECT;//无效
打破
}
案例DOMNode::文档\u节点:
{
//与文档_节点相同
return DOMNodeFilter::FILTER_REJECT;//无效
打破
}
违约:
{
返回DOMNodeFilter::FILTER\u ACCEPT;
打破
}
}
返回DOMNodeFilter::FILTER\u ACCEPT;
}

以下链接是一个很好的教程,介绍了如何读取XML文件并使用XERCES解析它的基础知识

完成后,XERCES API应足以进行进一步的操作:

要编写(序列化)文档,请使用类DOMWriter

我希望您使用新的Xerces 3。*旧的2.7/2.8到处都有内存泄漏这对我有帮助,因为Xerces没有释放我的文件,因为我没有刷新或删除XMLFormatTarget。
class DOMPrintFilter : public DOMLSSerializerFilter {
public:

    DOMPrintFilter(ShowType whatToShow = DOMNodeFilter::SHOW_ALL);
    ~DOMPrintFilter(){};

    virtual FilterAction acceptNode(const DOMNode*) const;
    virtual ShowType getWhatToShow() const {return fWhatToShow;};

private:
    // unimplemented copy ctor and assignement operator
    DOMPrintFilter(const DOMPrintFilter&);
    DOMPrintFilter & operator = (const DOMPrintFilter&);

    ShowType fWhatToShow;
};
#include "DOMPrintFilter.hpp"
#include <xercesc/util/XMLUniDefs.hpp>
#include <xercesc/util/XMLString.hpp>

static const XMLCh  element_person[]=
{
chLatin_p, chLatin_e, chLatin_r, chLatin_s, chLatin_o, chLatin_n, chNull
};

static const XMLCh  element_link[]=
{
chLatin_l, chLatin_i, chLatin_n, chLatin_k, chNull
};

DOMPrintFilter::DOMPrintFilter(ShowType whatToShow)
:fWhatToShow(whatToShow)
{}

DOMNodeFilter::FilterAction DOMPrintFilter::
acceptNode(const DOMNode* node) const
{
//
// The DOMLSSerializer shall call getWhatToShow() before calling
// acceptNode(), to show nodes which are supposed to be
// shown to this filter.
//
// REVISIT: In case the DOMLSSerializer does not follow the protocol,
//          Shall the filter honour, or NOT, what it claims
//          it is interested in ?
//
// The DOMLS specs does not specify that acceptNode() shall do
// this way, or not, so it is up the implementation,
// to skip the code below for the sake of performance ...
//
if ((getWhatToShow() & (1 << (node->getNodeType() - 1))) == 0)
    return DOMNodeFilter::FILTER_ACCEPT;

switch (node->getNodeType())
{
case DOMNode::ELEMENT_NODE:
    {
        // for element whose name is "person", skip it
        if (XMLString::compareString(node->getNodeName(),          element_person)==0)
            return DOMNodeFilter::FILTER_SKIP;
        // for element whose name is "line", reject it
        if (XMLString::compareString(node->getNodeName(), element_link)==0)
            return DOMNodeFilter::FILTER_REJECT;
        // for rest, accept it
        return DOMNodeFilter::FILTER_ACCEPT;

        break;
    }
case DOMNode::COMMENT_NODE:
    {
        // the WhatToShow will make this no effect
        return DOMNodeFilter::FILTER_REJECT;
        break;
    }
case DOMNode::TEXT_NODE:
    {
        // the WhatToShow will make this no effect
        return DOMNodeFilter::FILTER_REJECT;
        break;
    }
case DOMNode::DOCUMENT_TYPE_NODE:
    {
        // even we say we are going to process document type,
        // we are not able be to see this node since
        // DOMLSSerializerImpl (a XercesC's default implementation
        // of DOMLSSerializer) will not pass DocumentType node to
        // this filter.
        //
        return DOMNodeFilter::FILTER_REJECT;  // no effect
        break;
    }
case DOMNode::DOCUMENT_NODE:
    {
        // same as DOCUMENT_NODE
        return DOMNodeFilter::FILTER_REJECT;  // no effect
        break;
    }
default :
    {
        return DOMNodeFilter::FILTER_ACCEPT;
        break;
    }
}

return DOMNodeFilter::FILTER_ACCEPT;
}