2011-03-30 98 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