c++ dom解析器问题
我想更改 XML 文件。我正在使用 DOM 解析器。我的 XML 文件如下:
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="configuration.xsl"?>
<!-- Put site-specific property overrides in this file. -->
<configuration>
<property>
<name>fs.default.name</name>
<value> name</value>
</property>
</configuration>
我只想删除
节点并放置一个新节点
。我该怎么做?
我也用 C++ 编写了代码,但我陷入了困境。我应该怎么办?我的代码如下:
#include<string.h>
#include<iostream>
#include<sstream>
#include<sys/types.h>
#include<unistd.h>
#include<errno.h>
#include<sys/stat.h>
#include "parser.hpp"
using namespace xercesc ;
using namespace std;
GetConfig::GetConfig()
{
XMLPlatformUtils::Initialize();
TAG_configuration =XMLString::transcode("configuration");
TAG_property = XMLString::transcode("property");
TAG_value=XMLString::transcode("value");
Tag_name=XMLString::transcode("name");
m_ConfigFileParser=new XercesDOMParser;
}
GetConfig::~GetConfig()
{
delete m_ConfigFileParser;
XMLString::release( &TAG_configuration );
XMLString::release( &TAG_property );
XMLString::release( &TAG_value );
XMLPlatformUtils::Terminate();
}
void GetConfig :: readConfigFile(string& configFile)
{
struct stat fileStatus;
int iretStat = stat(configFile.c_str(), &fileStatus);
if( iretStat == ENOENT )
throw ( std::runtime_error("Path file_name does not exist, or path is an empty string.") );
else if( iretStat == ENOTDIR )
throw ( std::runtime_error("A component of the path is not a directory."));
else if( iretStat == ELOOP )
throw ( std::runtime_error("Too many symbolic links encountered while traversing the path."));
else if( iretStat == EACCES )
throw ( std::runtime_error("Permission denied."));
else if( iretStat == ENAMETOOLONG )
throw ( std::runtime_error("File can not be read\n"));
// Configure DOM parser.
m_ConfigFileParser->setValidationScheme( XercesDOMParser::Val_Never );
m_ConfigFileParser->setDoNamespaces( false );
m_ConfigFileParser->setDoSchema( false );
m_ConfigFileParser->setLoadExternalDTD( false );
m_ConfigFileParser->parse( configFile.c_str() );
DOMDocument* xmlDoc = m_ConfigFileParser->getDocument();
DOMElement* elementRoot = xmlDoc->getDocumentElement();
DOMNodeList* children = elementRoot->getChildNodes();
int main()
{
string configFile="/home/manish.yadav/Desktop/simple.xml";
GetConfig appConfig;
appConfig.readConfigFile(configFile);
return 0;
}
现在我不知道如何遍历这个文档。我的问题如下:
- 我怎样才能到达
? - 如何更改
到的值名称 <值>下一个
?
我的想法是删除该实体,然后以不同的值再次添加它,但我也不知道该怎么做。请用示例代码进行解释,或提出有关如何执行此操作的任何其他想法。
I want to change an XML file. I'm using DOM Parser. My XML file is the following:
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="configuration.xsl"?>
<!-- Put site-specific property overrides in this file. -->
<configuration>
<property>
<name>fs.default.name</name>
<value> name</value>
</property>
</configuration>
I just want to remove the <value>name</name>
node and put a new node <value>next</value>
. How can I do this?
I wrote code in C++ also, but I'm stuck in the middle. What should I do? My code is the following:
#include<string.h>
#include<iostream>
#include<sstream>
#include<sys/types.h>
#include<unistd.h>
#include<errno.h>
#include<sys/stat.h>
#include "parser.hpp"
using namespace xercesc ;
using namespace std;
GetConfig::GetConfig()
{
XMLPlatformUtils::Initialize();
TAG_configuration =XMLString::transcode("configuration");
TAG_property = XMLString::transcode("property");
TAG_value=XMLString::transcode("value");
Tag_name=XMLString::transcode("name");
m_ConfigFileParser=new XercesDOMParser;
}
GetConfig::~GetConfig()
{
delete m_ConfigFileParser;
XMLString::release( &TAG_configuration );
XMLString::release( &TAG_property );
XMLString::release( &TAG_value );
XMLPlatformUtils::Terminate();
}
void GetConfig :: readConfigFile(string& configFile)
{
struct stat fileStatus;
int iretStat = stat(configFile.c_str(), &fileStatus);
if( iretStat == ENOENT )
throw ( std::runtime_error("Path file_name does not exist, or path is an empty string.") );
else if( iretStat == ENOTDIR )
throw ( std::runtime_error("A component of the path is not a directory."));
else if( iretStat == ELOOP )
throw ( std::runtime_error("Too many symbolic links encountered while traversing the path."));
else if( iretStat == EACCES )
throw ( std::runtime_error("Permission denied."));
else if( iretStat == ENAMETOOLONG )
throw ( std::runtime_error("File can not be read\n"));
// Configure DOM parser.
m_ConfigFileParser->setValidationScheme( XercesDOMParser::Val_Never );
m_ConfigFileParser->setDoNamespaces( false );
m_ConfigFileParser->setDoSchema( false );
m_ConfigFileParser->setLoadExternalDTD( false );
m_ConfigFileParser->parse( configFile.c_str() );
DOMDocument* xmlDoc = m_ConfigFileParser->getDocument();
DOMElement* elementRoot = xmlDoc->getDocumentElement();
DOMNodeList* children = elementRoot->getChildNodes();
int main()
{
string configFile="/home/manish.yadav/Desktop/simple.xml";
GetConfig appConfig;
appConfig.readConfigFile(configFile);
return 0;
}
Now I don't know how to traverse this document. Here are my questions:
- How can I reach to
<value>
? - How can I change value of
<value> name</value>
to<value> next</value>
?
My idea is to remove the entity and then add it again with different value, but I don't know how to do this, either. Please explain with example code, or suggest any other ideas on how to do this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在
m_ConfigFileParser->parse( configFile.c_str() );
之后执行以下操作(考虑“configuration”是根元素):After
m_ConfigFileParser->parse( configFile.c_str() );
do the following (considering "configuration" is the root element):