2011-03-30 5 views
1

XML 파일을 변경하고 싶습니다. DOM 파서를 사용하고 있습니다. 내 XML 파일은 다음과 같은 : 난 그냥 <value>name</name> 노드를 제거하고 새 노드 <value>next</value>을 데려 가고 싶다는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> 

. 어떻게해야합니까?

나는 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>으로 어떻게 바꿀 수 있습니까?

제 아이디어는 엔터티를 제거한 다음 다른 값으로 다시 추가하는 것입니다.하지만이 방법도 잘 모릅니다. 예제 코드로 설명하거나이를 수행하는 방법에 대한 다른 아이디어를 제안하십시오. m_ConfigFileParser->parse(configFile.c_str());

답변

0

는 다음을 (루트 요소를 "구성"되어 고려)을 수행합니다

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

+0

'setTextContent'가 실행됩니까? 결과 XML을 파일에 저장 했습니까? –

+0

늦게 응답하여 죄송합니다. 아니, XML에 결과 파일을 저장하지 않았다면, 나는 (루트) 이후에 작동하지 않는 코드를 확인한다. 제발 더 많은 코드와 함께 더 많은 설명을주세요 – user513164