简体   繁体   中英

c++ dom parser problem

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.

After m_ConfigFileParser->parse( configFile.c_str() ); do the following (considering "configuration" is the root element):

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"
    }
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM