You are viewing a plain text version of this content. The canonical link for it is here.
Posted to c-dev@xerces.apache.org by baranidharan <ba...@sifycorp.com> on 2009/07/31 08:10:59 UTC

How to add the attribute

Hi all,
           I am using the xercesc c++ for XML operations, i have the XML 
document given below ,i am trying to add one attribute to that <node > 
tag ,using setAttribute() function but  attribute not adding in to that 
, my cpp program given below , please tell me how to add the attribute 
in to the Dom element.

Thanks,
K Barani

XML file
--------------------------------
<agent>
 <node >
 <mtr mid="1" tp="pf">
        <pfo name="Processor">
        <ctr name="% Processor Time">
                <ins name="_Total"  id="10" pi="60"  cl="1" />
        </ctr>
        </pfo>
  </mtr>
  </node>
 </agent>

-----------------------------------


myprogram.cpp
----------------------
#include<iostream>
#include<string>
                                                                                                                             

// Xml parser
#include <xercesc/util/PlatformUtils.hpp>
#include <xercesc/parsers/XercesDOMParser.hpp>
#include <xercesc/dom/DOM.hpp>
#include <xercesc/sax/HandlerBase.hpp>
#include <xercesc/util/XMLString.hpp>
                                                                                                                             

using namespace std;
using namespace xercesc;
                                                                                                                             

int main()
{
                                                                                                                             

  DOMDocument *doc;
  static char *xmlConfig 
="/extra/Linuxagent/Sample/XML_sample/domm/Xmlalter/Agent1.xml";
                                                                                                                             

  try {
        XMLPlatformUtils::Initialize();
      }
  catch (const XMLException& toCatch) {
         char* message = XMLString::transcode(toCatch.getMessage());
         cout<<"Error: during initialization"<<endl;
         XMLString::release(&message);
                                                                                                                             

        }
                                                                                                                             

  XercesDOMParser* parser = new XercesDOMParser();
                                                                                                                             

  ErrorHandler* errHandler = (ErrorHandler*) new HandlerBase();
  parser->setErrorHandler(errHandler);
                                                                                                                             

  try {
       parser->parse(xmlConfig);
      }
  catch (const XMLException& toCatch) {
            char* message = XMLString::transcode(toCatch.getMessage());
            cout<<"Error: xmlparser XMLException"<<endl;
            XMLString::release(&message);
        }
  catch (const DOMException& toCatch) {
            char* message = XMLString::transcode(toCatch.msg);
            cout<<"Error: xmlparser DOMException"<<endl;
            XMLString::release(&message);
        }
  catch (...) {
            cout<<"Error: xmlparser Unexpected Exception"<<endl;;
        }
                                                                                                                             

  doc = parser->getDocument();
                                                                                                                             

  XMLCh *xname = XMLString::transcode("node");
  DOMNodeList *xmlnodeList;
  DOMNode *xmlNode;
  xmlnodeList = doc->getElementsByTagName(xname);
  XMLString::release(&xname);
  int len = xmlnodeList->getLength();
                                                                                                                             

  for(len=0; len < xmlnodeList->getLength(); len++)
  {
    xmlNode    = xmlnodeList->item(len);
    DOMElement* elem = (DOMElement*) xmlNode;
                                                                                                                             

    XMLCh *na = XMLString::transcode("id");
    XMLCh *we = XMLString::transcode("100");
    elem->setAttribute(na,we);
  }
                                                                                                                             

                                                                                                                             

 return 0;
}



Get your world in your inbox!

Mail, widgets, documents, spreadsheets, organizer and much more with your Sifymail WIYI id!
Log on to http://www.sify.com

********** DISCLAIMER **********
Information contained and transmitted by this E-MAIL is proprietary to 
Sify Limited and is intended for use only by the individual or entity to 
which it is addressed, and may contain information that is privileged, 
confidential or exempt from disclosure under applicable law. If this is a 
forwarded message, the content of this E-MAIL may not have been sent with 
the authority of the Company. If you are not the intended recipient, an 
agent of the intended recipient or a  person responsible for delivering the 
information to the named recipient,  you are notified that any use, 
distribution, transmission, printing, copying or dissemination of this 
information in any way or in any manner is strictly prohibited. If you have 
received this communication in error, please delete this mail & notify us 
immediately at admin@sifycorp.com

---------------------------------------------------------------------
To unsubscribe, e-mail: c-dev-unsubscribe@xerces.apache.org
For additional commands, e-mail: c-dev-help@xerces.apache.org


RE: How to add the attribute

Posted by Jesse Pelton <js...@PKC.com>.
Your code apparently never writes the updated DOM.  The DOMPrint sample
app demonstrates how to perform serialization of a DOM document. 

-----Original Message-----
From: baranidharan [mailto:baranidharan.kuppusamy@sifycorp.com] 
Sent: Friday, July 31, 2009 2:11 AM
To: c-dev@xerces.apache.org
Subject: How to add the attribute

Hi all,
           I am using the xercesc c++ for XML operations, i have the XML

document given below ,i am trying to add one attribute to that <node > 
tag ,using setAttribute() function but  attribute not adding in to that 
, my cpp program given below , please tell me how to add the attribute 
in to the Dom element.

Thanks,
K Barani

XML file
--------------------------------
<agent>
 <node >
 <mtr mid="1" tp="pf">
        <pfo name="Processor">
        <ctr name="% Processor Time">
                <ins name="_Total"  id="10" pi="60"  cl="1" />
        </ctr>
        </pfo>
  </mtr>
  </node>
 </agent>

-----------------------------------


myprogram.cpp
----------------------
#include<iostream>
#include<string>
 


// Xml parser
#include <xercesc/util/PlatformUtils.hpp>
#include <xercesc/parsers/XercesDOMParser.hpp>
#include <xercesc/dom/DOM.hpp>
#include <xercesc/sax/HandlerBase.hpp>
#include <xercesc/util/XMLString.hpp>
 


using namespace std;
using namespace xercesc;
 


int main()
{
 


  DOMDocument *doc;
  static char *xmlConfig 
="/extra/Linuxagent/Sample/XML_sample/domm/Xmlalter/Agent1.xml";
 


  try {
        XMLPlatformUtils::Initialize();
      }
  catch (const XMLException& toCatch) {
         char* message = XMLString::transcode(toCatch.getMessage());
         cout<<"Error: during initialization"<<endl;
         XMLString::release(&message);
 


        }
 


  XercesDOMParser* parser = new XercesDOMParser();
 


  ErrorHandler* errHandler = (ErrorHandler*) new HandlerBase();
  parser->setErrorHandler(errHandler);
 


  try {
       parser->parse(xmlConfig);
      }
  catch (const XMLException& toCatch) {
            char* message = XMLString::transcode(toCatch.getMessage());
            cout<<"Error: xmlparser XMLException"<<endl;
            XMLString::release(&message);
        }
  catch (const DOMException& toCatch) {
            char* message = XMLString::transcode(toCatch.msg);
            cout<<"Error: xmlparser DOMException"<<endl;
            XMLString::release(&message);
        }
  catch (...) {
            cout<<"Error: xmlparser Unexpected Exception"<<endl;;
        }
 


  doc = parser->getDocument();
 


  XMLCh *xname = XMLString::transcode("node");
  DOMNodeList *xmlnodeList;
  DOMNode *xmlNode;
  xmlnodeList = doc->getElementsByTagName(xname);
  XMLString::release(&xname);
  int len = xmlnodeList->getLength();
 


  for(len=0; len < xmlnodeList->getLength(); len++)
  {
    xmlNode    = xmlnodeList->item(len);
    DOMElement* elem = (DOMElement*) xmlNode;
 


    XMLCh *na = XMLString::transcode("id");
    XMLCh *we = XMLString::transcode("100");
    elem->setAttribute(na,we);
  }
 


 


 return 0;
}



Get your world in your inbox!

Mail, widgets, documents, spreadsheets, organizer and much more with
your Sifymail WIYI id!
Log on to http://www.sify.com

********** DISCLAIMER **********
Information contained and transmitted by this E-MAIL is proprietary to 
Sify Limited and is intended for use only by the individual or entity to

which it is addressed, and may contain information that is privileged, 
confidential or exempt from disclosure under applicable law. If this is
a 
forwarded message, the content of this E-MAIL may not have been sent
with 
the authority of the Company. If you are not the intended recipient, an 
agent of the intended recipient or a  person responsible for delivering
the 
information to the named recipient,  you are notified that any use, 
distribution, transmission, printing, copying or dissemination of this 
information in any way or in any manner is strictly prohibited. If you
have 
received this communication in error, please delete this mail & notify
us 
immediately at admin@sifycorp.com

---------------------------------------------------------------------
To unsubscribe, e-mail: c-dev-unsubscribe@xerces.apache.org
For additional commands, e-mail: c-dev-help@xerces.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: c-dev-unsubscribe@xerces.apache.org
For additional commands, e-mail: c-dev-help@xerces.apache.org