【问题标题】:c++ dom parser problemc++ dom解析器问题
【发布时间】:2011-03-30 04:33:17
【问题描述】:

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

我只想删除&lt;value&gt;name&lt;/name&gt; 节点并放置一个新节点&lt;value&gt;next&lt;/value&gt;。我该怎么做?

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

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

  • 如何联系&lt;value&gt;
  • 如何将&lt;value&gt; name&lt;/value&gt; 的值更改为&lt;value&gt; next&lt;/value&gt;

我的想法是删除实体,然后用不同的值再次添加它,但我也不知道该怎么做。请使用示例代码进行解释,或提出任何其他有关如何执行此操作的想法。

【问题讨论】:

    标签: c++ xml xml-parsing domparser


    【解决方案1】:

    m_ConfigFileParser-&gt;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"
        }
      }
    }
    

    【讨论】:

    • setTextContent 是否执行?您是否将生成的 xml 保存到文件中?
    • 抱歉回复晚了。不,我没有将生成的 xml 保存到文件中,我检查了在 if(root) 之后它不起作用的代码。请用更多代码给我更多解释
    • 您好,代码现在可以工作,但我看不到 xml 文件中的更改。如何保存生成的 xml 我不明白。你能解释一下我应该怎么做才能改变文件中的值吗?请帮助我
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-27
    • 1970-01-01
    相关资源
    最近更新 更多