2011-03-30 154 views
1

我想更改XML文件。我正在使用DOM解析器。我的XML文件如下:C++ dom解析器问题

<?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>

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

回答

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

它不工作,就可以说明为什么?没有错误 – user513164 2011-04-04 12:32:58

+0

'setTextContent'是否执行?您是否将结果xml保存到文件中? – 2011-04-04 16:13:06

+0

很晚回应抱歉。不,我没有将结果xml保存到文件中,我检查了它在if(root)之后没有工作的代码。请给我更多的解释与更多的代码 – user513164 2011-04-05 05:53:43