You are viewing a plain text version of this content. The canonical link for it is here.
Posted to c-users@xerces.apache.org by Manish Yadav <ma...@orkash.com> on 2011/04/05 15:04:13 UTC

xml parsing problem

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

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

i want to change <value>name</value> to <value>next</next>

my cpp file code is following:

#include<string.h>
#include<iostream>
#include<sstream>
#include<sys/types.h>
#include<unistd.h>
#include<errno.h>
#include<sys/stat.h>
#include "parser1.hpp"
#include <typeinfo>
using namespace xercesc ;
using namespace std;

GetConfig::GetConfig()
{
XMLPlatformUtils::Initialize();
  TAG_configuration =XMLString::transcode("configuration");
  TAG_property = XMLString::transcode("property");
TAG_name=XMLString::transcode("name");
TAG_value=XMLString::transcode("value");
m_ConfigFileParser=new XercesDOMParser;
}
GetConfig::~GetConfig()
{
  delete m_ConfigFileParser;

XMLString::release( &TAG_configuration );
XMLString::release( &TAG_property );
//XMLString::release( &TAG_value );
//XMLString::release( &TAG_name );
XMLPlatformUtils::Terminate();
}
void GetConfig :: readConfigFile(string& configFile)
throw( std::runtime_error )
{
  /*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* doc = m_ConfigFileParser->getDocument();
DOMElement *root = doc->getDocumentElement();
const XMLCh* tag = xercesc::XMLString::transcode("property");
const XMLCh* tag1 = xercesc::XMLString::transcode("value");
const XMLCh* tag2 = xercesc::XMLString::transcode("name");
//DOMNodeList * list= doc->getElementbyTagName("tag");
//cout<<moot;

//DOMNode * rootElem = doc->getDocumentElement();


cout << "\nfirst";


  //DOMNode *root =  doc->getFirstChild() ;

cout << "\nsecond\n";
cout<< root<<"\n" ;

if ( root )
  {
cout<<"hi\n";
   DOMElement* property = dynamic_cast<DOMElement*>( 
root->getElementsByTagName( tag )->item( 0) );

   if ( property )
  {
     cout<<"hello";
  DOMElement* name = dynamic_cast<DOMElement*>( 
property->getElementsByTagName( tag2 )->item( 0 ) );

if ( name )
{
   cout<<"\nmanish\n";

     DOMElement* value = dynamic_cast<DOMElement*>( 
property->getElementsByTagName( tag1 )->item( 0 ) );

     if ( value )
{
       cout<<XMLString::transcode(value->getTextContent());
        cout<<"\n";
       value->setTextContent( XMLString::transcode(" next") ); // this 
will update the element named "value"
     }
   }
}
}

int main()
     {
        string configFile="/home/manish.yadav/Desktop/sel.xml";

        GetConfig appConfig;

    appConfig.readConfigFile(configFile);


        return 0;
     }

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?





Re: xml parsing problem

Posted by Vitaly Prapirny <ma...@mebius.net>.
Hi,

I suggest you to search the DOMLSSerializer class in the Xerces-C++
documentation and samples. Hope this helps.

Good luck!
	Vitaly

Manish Yadav wrote:
> 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
>
> <?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>
>
> i want to change <value>name</value> to <value>next</next>
>
> my cpp file code is following:
>
> #include<string.h>
> #include<iostream>
> #include<sstream>
> #include<sys/types.h>
> #include<unistd.h>
> #include<errno.h>
> #include<sys/stat.h>
> #include "parser1.hpp"
> #include <typeinfo>
> using namespace xercesc ;
> using namespace std;
>
> GetConfig::GetConfig()
> {
> XMLPlatformUtils::Initialize();
> TAG_configuration =XMLString::transcode("configuration");
> TAG_property = XMLString::transcode("property");
> TAG_name=XMLString::transcode("name");
> TAG_value=XMLString::transcode("value");
> m_ConfigFileParser=new XercesDOMParser;
> }
> GetConfig::~GetConfig()
> {
> delete m_ConfigFileParser;
>
> XMLString::release( &TAG_configuration );
> XMLString::release( &TAG_property );
> //XMLString::release( &TAG_value );
> //XMLString::release( &TAG_name );
> XMLPlatformUtils::Terminate();
> }
> void GetConfig :: readConfigFile(string& configFile)
> throw( std::runtime_error )
> {
> /*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* doc = m_ConfigFileParser->getDocument();
> DOMElement *root = doc->getDocumentElement();
> const XMLCh* tag = xercesc::XMLString::transcode("property");
> const XMLCh* tag1 = xercesc::XMLString::transcode("value");
> const XMLCh* tag2 = xercesc::XMLString::transcode("name");
> //DOMNodeList * list= doc->getElementbyTagName("tag");
> //cout<<moot;
>
> //DOMNode * rootElem = doc->getDocumentElement();
>
>
> cout << "\nfirst";
>
>
> //DOMNode *root = doc->getFirstChild() ;
>
> cout << "\nsecond\n";
> cout<< root<<"\n" ;
>
> if ( root )
> {
> cout<<"hi\n";
> DOMElement* property = dynamic_cast<DOMElement*>(
> root->getElementsByTagName( tag )->item( 0) );
>
> if ( property )
> {
> cout<<"hello";
> DOMElement* name = dynamic_cast<DOMElement*>(
> property->getElementsByTagName( tag2 )->item( 0 ) );
>
> if ( name )
> {
> cout<<"\nmanish\n";
>
> DOMElement* value = dynamic_cast<DOMElement*>(
> property->getElementsByTagName( tag1 )->item( 0 ) );
>
> if ( value )
> {
> cout<<XMLString::transcode(value->getTextContent());
> cout<<"\n";
> value->setTextContent( XMLString::transcode(" next") ); // this will
> update the element named "value"
> }
> }
> }
> }
>
> int main()
> {
> string configFile="/home/manish.yadav/Desktop/sel.xml";
>
> GetConfig appConfig;
>
> appConfig.readConfigFile(configFile);
>
>
> return 0;
> }
>
> 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?
>
>
>
>