473,405 Members | 2,444 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

c++ xml parsing problem

1
hi every body
i'm new to c++ and xerces. i'm triying to make change in an xml file using xerces dom parser. my xml file which i want to change is following : its name is sel.xml

Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0"?>
  2. <?xml-stylesheet type="text/xsl" href="configuration.xsl"?>
  3.  
  4. <!-- Put site-specific property overrides in this file. -->
  5.  
  6. <configuration>
  7. <property>
  8.   <name>fs.default.name</name>
  9.   <value> name</value>
  10.   </property>
  11.  
  12. </configuration>
  13.  
  14. i want to change <value>name</value> to <value>next</next>
  15.  
  16. my cpp file code is following:
  17.  
  18. #include<string.h>
  19. #include<iostream>
  20. #include<sstream>
  21. #include<sys/types.h>
  22. #include<unistd.h>
  23. #include<errno.h>
  24. #include<sys/stat.h>
  25. #include "parser1.hpp"
  26. #include <typeinfo>
  27. using namespace xercesc ;
  28. using namespace std;
  29.  
  30. GetConfig::GetConfig()
  31. {
  32. XMLPlatformUtils::Initialize();
  33.  TAG_configuration =XMLString::transcode("configuration");
  34.  TAG_property = XMLString::transcode("property");
  35. TAG_name=XMLString::transcode("name");
  36. TAG_value=XMLString::transcode("value");
  37. m_ConfigFileParser=new XercesDOMParser;
  38. }
  39. GetConfig::~GetConfig()
  40. {
  41.  delete m_ConfigFileParser;
  42.  
  43. XMLString::release( &TAG_configuration );
  44. XMLString::release( &TAG_property );
  45. //XMLString::release( &TAG_value );
  46. //XMLString::release( &TAG_name );
  47. XMLPlatformUtils::Terminate();
  48. }
  49. void GetConfig :: readConfigFile(string& configFile)
  50. throw( std::runtime_error )
  51. {
  52.  /*struct stat fileStatus;     
  53. int iretStat = stat(configFile.c_str(), &fileStatus);
  54.    if( iretStat == ENOENT )
  55.           throw ( std::runtime_error("Path file_name does not exist, or path is an empty string.") );
  56.        else if( iretStat == ENOTDIR )
  57.           throw ( std::runtime_error("A component of the path is not a directory."));
  58.        else if( iretStat == ELOOP )
  59.           throw ( std::runtime_error("Too many symbolic links encountered while traversing the path."));
  60.    else if( iretStat == EACCES )
  61.           throw ( std::runtime_error("Permission denied."));
  62.        else if( iretStat == ENAMETOOLONG )
  63.           throw ( std::runtime_error("File can not be read\n"));
  64. */
  65.        // Configure DOM parser.
  66.  
  67.        m_ConfigFileParser->setValidationScheme( XercesDOMParser::Val_Never );
  68.        m_ConfigFileParser->setDoNamespaces( false );
  69.        m_ConfigFileParser->setDoSchema( false );
  70.        m_ConfigFileParser->setLoadExternalDTD( false );
  71.  
  72.     m_ConfigFileParser->parse( configFile.c_str() );
  73.  
  74. DOMDocument* doc = m_ConfigFileParser->getDocument();
  75. DOMElement *root = doc->getDocumentElement();
  76. const XMLCh* tag = xercesc::XMLString::transcode("property");
  77. const XMLCh* tag1 = xercesc::XMLString::transcode("value");
  78. const XMLCh* tag2 = xercesc::XMLString::transcode("name");
  79. //DOMNodeList * list= doc->getElementbyTagName("tag");
  80. //cout<<moot;
  81.  
  82. //DOMNode * rootElem = doc->getDocumentElement();
  83.  
  84.  
  85. cout << "\nfirst";
  86.  
  87.  
  88.  //DOMNode *root =  doc->getFirstChild() ;
  89.  
  90. cout << "\nsecond\n";
  91. cout<< root<<"\n" ;
  92.  
  93. if ( root )
  94.  {
  95. cout<<"hi\n";
  96.   DOMElement* property = dynamic_cast<DOMElement*>( root->getElementsByTagName( tag )->item( 0) );
  97.  
  98.   if ( property )
  99.  {
  100.     cout<<"hello";
  101.  DOMElement* name = dynamic_cast<DOMElement*>( property->getElementsByTagName( tag2 )->item( 0 ) );
  102.  
  103. if ( name )
  104. {
  105.   cout<<"\nmanish\n";
  106.  
  107.     DOMElement* value = dynamic_cast<DOMElement*>( property->getElementsByTagName( tag1 )->item( 0 ) );
  108.  
  109.     if ( value ) 
  110. {     
  111.       cout<<XMLString::transcode(value->getTextContent()); 
  112.        cout<<"\n";
  113.       value->setTextContent( XMLString::transcode(" next") ); // this will update the element named "value"
  114.     }
  115.   }
  116. }
  117. }
  118.  
  119. int main()
  120.     {
  121.        string configFile="/home/manish.yadav/Desktop/sel.xml"; 
  122.  
  123.        GetConfig appConfig;
  124.  
  125.    appConfig.readConfigFile(configFile);
  126.  
  127.  
  128.        return 0;
  129.     }
i compiled the program successfully and the program is running correctly it shows the out put like that

first
second
0x1314e50
hi
hello
manish
name
the extra output are becoz i want to check the flow control of program

now my problem is that i used the method value->setTextContent( XMLString::transcode(" next") );
it is not working ;
in my xml file i saw the old value. the value is not changing there what should i do so that the value change in sel.xml file too?
what should i do to change the value in xml file ?
what i 'm doing wrong?
i goggled the matter lot but someone saying i cant make change in xml file .
to change xml file i have to read the content of file and create a new file .
can anybody explain what should i do?
i read the dom Programming guide, api Documentation but nothing substantial.
what is the method or way to read from old xml file and write it to a new file ?
can any body explain with code ?
how to make new xml file using dom c++ parser?
Apr 5 '11 #1
0 1211

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Mike Medland | last post by:
Hi, I am currently using Python 2.2.3, apache 1.3.27 and mod_python 2.7.8. Under Solaris 8 on a sun. The configuration appears to be working fine but, the problem that i am having is in parsing...
0
by: burn_hall | last post by:
Hi, I have a problem and can't figure it out and need your help, please look at the following code and the output also a xml file snippet is down there too. Looking at the output I don't know why...
4
by: silviu | last post by:
I have the following XML string that I want to parse using the SAX parser. If I remove the portion of the XML string between the <audit> and </audit> tags the SAX is parsing correctly. Otherwise...
2
by: littlefitzer | last post by:
Hi, I have come across a tricky little problem, I hope maybe one of you can help. The problem I am having is that I need to parse two seperate values from an XML document using XSL. The two...
0
by: pulvertum | last post by:
Hello, I need an org.w3c.dom.Document so I can pass them to do a transformation of my XML using XSL (to another XML as result). I've tried several solutions but didn't have the desired result:...
1
by: David Hirschfield | last post by:
Anyone out there use simpleparse? If so, I have a problem that I can't seem to solve...I need to be able to parse this line: """Cen2 = Cen(OUT, "Cep", "ies", wh, 544, (wh/ht));""" with this...
1
by: danep | last post by:
Hi, I'm fairly new to AJAX, but I've been able to retrieve HTML and plain-text documents without any trouble. However, I haven't figured out how to retrieve it in XML format. Basically, here's...
13
by: otrWalter | last post by:
I'm trying to display that type, name and value to class properties. Yes, I know about print_r(). I'm just trying to build a display format for this information. AFAIK, the standard PHP tools to...
5
by: Timo | last post by:
I haven't been using ANSI-C for string parsing for some time, so even this simple task is problematic: I have a string tmp_str, which includes date + time + newline in format: "25.6.2008 21:49"....
1
by: avpkills2002 | last post by:
I seem to be getting this weird problem in Internet explorer. I have written a code for parsing a XML file and displaying the output. The code works perfectly fine with ffx(Firefox).However is not...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.