c ++ dom解析器问题

时间:2011-03-30 04:33:18

标签: c++ xml xml-parsing domparser

我想更改XML文件。我正在使用DOM Parser。我的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>

我只想删除<value>name</name>节点并添加一个新节点<value>next</value>。我怎么能这样做?

我也用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;
    }

现在我不知道如何遍历这个文件。以下是我的问题:

  • 如何联系到<value>
  • 如何将<value> name</value>的值更改为<value> next</value>

我的想法是删除实体然后再添加不同的值,但我也不知道如何做到这一点。请解释示例代码,或建议如何执行此操作的任何其他想法。

1 个答案:

答案 0 :(得分:0)

m_ConfigFileParser->parse( configFile.c_str() );之后执行以下操作(考虑“配置”是根元素):

DOMDocument* doc = m_ConfigFileParser.getDocument();
DOMElement* root = dynamic_cast<DOMElement*>( doc->getFirstChild() );
if ( root ) {
  DOMElement* property = dynamic_cast<DOMElement*>( root->getElementsByTagName( "property" )->item( 0 ) );
  if ( property ) {
    DOMElement* value = dynamic_cast<DOMElement*>( property->getElementsByTagName( "value" )->item( 0 ) );
    if ( value ) {
      value->setTextContent( " next" ); // this will update the element named "value"
    }
  }
}