C++ c++;dom解析器问题

C++ c++;dom解析器问题,c++,xml,xml-parsing,domparser,C++,Xml,Xml Parsing,Domparser,我想更改一个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&g

我想更改一个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>
#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;
    }

fs.default.name
名称
我只想删除
名称
节点,然后在下一个中放置一个新节点。我该怎么做

我也用C++编写代码,但是我被困在中间。我该怎么办?我的代码如下:

<?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>
#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;
    }
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括“parser.hpp”
使用名称空间xercesc;
使用名称空间std;
GetConfig::GetConfig()
{
XMLPlatformUtils::Initialize();
TAG_configuration=XMLString::transcode(“配置”);
TAG_property=XMLString::transcode(“属性”);
TAG_value=XMLString::转码(“值”);
Tag_name=XMLString::transcode(“名称”);
m_ConfigFileParser=新的XercestomParser;
}
GetConfig::~GetConfig()
{
删除m_ConfigFileParser;
XMLString::发布(&TAG_配置);
XMLString::release(&TAG_属性);
XMLString::release(&TAG_值);
XMLPlatformUtils::Terminate();
}
void GetConfig::readConfigFile(字符串和配置文件)
{
结构统计文件状态;
int-iretStat=stat(configFile.c_str(),&fileStatus);
if(iretStat==enoint)
抛出(std::runtime_error(“路径文件名不存在,或者路径为空字符串”);
否则如果(iretStat==ENOTDIR)
抛出(std::runtime_error(“路径的组件不是目录”);
else if(iretStat==ELOOP)
抛出(std::runtime_error(“在遍历路径时遇到太多符号链接”);
else if(iretStat==EACCES)
抛出(std::runtime_错误(“权限被拒绝”);
else if(iretStat==enametolong)
抛出(std::runtime_错误(“无法读取文件”);
//配置DOM解析器。
m_ConfigFileParser->setValidationScheme(XercesDOMParser::Val_Never);
m_ConfigFileParser->setdonamespace(false);
m_ConfigFileParser->setDoSchema(false);
m_ConfigFileParser->setLoadExternalDTD(false);
m_ConfigFileParser->parse(configFile.c_str());
DOMDocument*xmlDoc=m_ConfigFileParser->getDocument();
doElement*elementRoot=xmlDoc->getDocumentElement();
DOMNodeList*children=elementRoot->getChildNodes();
int main()
{
字符串configFile=“/home/manish.yadav/Desktop/simple.xml”;
GetConfig-appConfig;
appConfig.readConfigFile(configFile);
返回0;
}
现在我不知道如何遍历此文档。以下是我的问题:

  • 我怎样才能到达
  • 如何将
    name
    的值更改为
    next

我的想法是删除实体,然后用不同的值再次添加它,但我也不知道怎么做。请用示例代码解释,或者建议其他方法。

m\u ConfigFileParser->parse(configFile.c\u str())之后;
执行以下操作(考虑到“configuration”是根元素):

DOMDocument*doc=m_ConfigFileParser.getDocument();
doElement*root=dynamic_cast(doc->getFirstChild());
如果(根){
doElement*property=dynamic_cast(根->getElementsByTagName(“属性”)->项(0));
如果(财产){
DOMELENT*value=dynamic_cast(属性->getElementsByTagName(“值”)->项(0));
如果(值){
value->setTextContent(“next”);//这将更新名为“value”的元素
}
}
}

是否执行
setTextContent
执行?是否将生成的xml保存到文件中?抱歉,响应太晚。否,我没有将生成的xml保存到文件中,我检查了代码,如果(根目录)后,它将不起作用。请用更多的代码给我更多的解释。代码正在运行,但我看不到xml文件中的更改。如何保存生成的xml我不理解。请解释我应该如何更改文件中的值?请帮助我