You are viewing a plain text version of this content. The canonical link for it is here.
Posted to c-users@xalan.apache.org by "Tankel, Ifat" <If...@icomverse.com> on 2002/02/26 10:52:35 UTC

removeChild of xalanNode (found by xpath) from parsed xml file th row XalanDomExeption

Hello

I need to 
- read xml file from a disk or from a string buffer
- get an xpath and search its nodes in this file/buffer been read
- remove this node  or update this node 
In order to try this I have created a class named myDom (I used the
SimpleXpathApi example) 
In the constructor I call initialize method to init the class members.
in main 
I create a vector of myDom * ( mean while it is size 1 but I need to have
lots of myDom instances in the future)
I call init method which use XalanSourceTreeParserLiaison to parseXMLStream
which comes from a file
I call removeNode method which use the evaluator to search the node
according to the Xpath I have and call removeChild method
I delete the vector elements.


I run this by myDom file.xml /doc //name
where as the file.xml 
<?xml version="1.0">
<doc>
	<name>
		<first> Emily </first>
		<last> Farmer </last>
	</name>
...
</doc>
but I get XalanDomExeption  NO_MODIFICATION_ALLOWED_ERR and I do not
understand way?

My code  is
myDom.cpp
#include "myDom.hpp"

int myDom::_initCounter=0;
XalanSourceTreeInit * myDom::_sourceTreeInit;

XalanSourceTreeDOMSupport * myDom::_domSupport;
XalanSourceTreeParserLiaison * myDom::_parserLiaison;

XPathEvaluator * myDom::_evaluatorI;
XPathEvaluator * myDom::_evaluatorII;

myDom::myDom() {
  cout<< "-----------constractor start" <<endl;
  ++_initCounter;
  if(_initCounter==1)
    initialize();
  
  _fileSource=NULL;
  _bufferSource=NULL;
  _xalanDocument=NULL;
  _contextNode=NULL;

  cout <<"------------- constractor finished " <<endl;
}

myDom::~myDom() {
cout<< "-----------distractor start" <<endl;

 if (_bufferSource) {
   delete _bufferSource;
   cout <<"------------- delete _bufferSource-------" <<endl;
 }
 if (_fileSource) {
   delete _fileSource;
   cout <<"------------- delete _fileSource---------" <<endl;
 }
 --_initCounter;
 if (_initCounter==0)
   terminate();
 cout <<"-------- distractor finished " <<endl;
}

void myDom::initialize() {
cout<< "-----------initialize start" <<endl;
  XMLPlatformUtils::Initialize();
  XPathEvaluator::initialize();

  _sourceTreeInit = new XalanSourceTreeInit;
  _domSupport = new XalanSourceTreeDOMSupport;
  _parserLiaison = new XalanSourceTreeParserLiaison();
  _evaluatorI = new XPathEvaluator();
  _evaluatorII = new XPathEvaluator();
 cout <<"------------- initializer finished " <<endl;
}

void myDom::terminate(){
  cout <<"------------- terminate start " <<endl;
  
  if (_evaluatorII){
    delete _evaluatorII;
    cout <<"------------- delete _evaluatorII----------" <<endl;
  }
  if (_evaluatorI){
    delete _evaluatorI;
    cout <<"------------- delete _evaluatorI----------" <<endl;
  }
  if ( _parserLiaison) {
    delete _parserLiaison;
    cout <<"------------- delete _parserLiaison-------" <<endl;
  }
  if ( _domSupport) {
    delete _domSupport;
    cout <<"------------- delete _domSupport----------" <<endl;
  }
  if ( _sourceTreeInit){
    delete _sourceTreeInit;
    cout <<"------------- delete _sourceTreeInit------" <<endl;
  } 
  XPathEvaluator::terminate();
  XMLPlatformUtils::Terminate();
  cout <<"------------- terminate finished " <<endl;

}

void myDom::init(string fileName) {
  cout <<"------------- init(file) " <<endl;
  _fileSource = new
LocalFileInputSource(c_wstr(XalanDOMString(fileName.c_str())));
  _xalanDocument = _parserLiaison->parseXMLStream(*_fileSource);
  if (! _xalanDocument)
    cout<< "xalanDocument was not created " <<endl;
  cout <<"------------- init(file) finished " <<endl;
}

void myDom::init(char * buffer) {
  cout <<"------------- init(buff) " <<endl;
  _bufferSource = new MemBufInputSource((XMLByte *)
buffer,strlen(buffer)*sizeof(char),"test",false);
  _xalanDocument = _parserLiaison->parseXMLStream(*_bufferSource);
  if (! _xalanDocument)
    cout<< "xalanDocument was not created " <<endl;
  cout <<"------------- init(buff) finished " <<endl;
}

const string myDom::getValue(string xpath) {
  cout <<"------------- getValue start" <<endl;
  if (! _xalanDocument) {
    cout<< "xalanDocument is empty " <<endl;
    exit(0);
  }
  XObjectPtr *_result;
  _result = new XObjectPtr(
			   _evaluatorI->evaluate(*_domSupport,
						_xalanDocument,
	
XalanDOMString(xpath.c_str()).c_str(),
	
_xalanDocument->getDocumentElement()));
  string tmpStr="";
  if (_result->null()==false){
    tmpStr=domStringToString((*_result)->str());
  }

  if (_result){
    cout << "result=" << _result <<endl;
    delete _result;
    cout <<"------------- delete _result " <<endl;
  }  
  cout <<"------------- getValue finished " <<endl;
  return tmpStr;
}

const string myDom::domStringToString(XalanDOMString domString) {
  cout <<"------------- domStringToString start " <<endl;
  string tmpString="";
  CharVectorType charVector=domString.transcode() ;
  
  for (uint i=0; i<charVector.size(); i++) {
    tmpString += charVector[i];
  }
  cout << "tmpString="<<tmpString <<endl;
  
  cout <<"-------------domStringToString finished" <<endl;
  return tmpString; 
}

XalanNode* myDom::getNode(XPathEvaluator * evaluator,string xpath="/") {
cout <<"------------- getNode start" <<endl;
 if (! _xalanDocument ) {
   cout<< "xalanDocument is empty " <<endl;
   exit(0);
 }
 if (! evaluator ) {
   cout<< "evaluator is empty " <<endl;
   exit(0);
 }
 _contextNode=NULL;
 _contextNode = evaluator->selectSingleNode(*_domSupport,
					    _xalanDocument,
	
XalanDOMString(xpath.c_str()).c_str());
 
 if (_contextNode == 0){
   cerr << "Warning -- No nodes matched the location path \""
	<< xpath
	<< "\"."
	<< endl
	<< "Execution cannot continue..."
	<< endl
	<< endl;
 }
 else {
   cout << "contextnode type="<<_contextNode->getNodeType() <<endl;
   cout << "contextnode value="<<_contextNode->getNodeValue() <<endl;
   cout << "contextnode name="<<_contextNode->getNodeName() <<endl;
 }
 cout <<"------------- getNode finished " <<endl;
 return _contextNode;
}

void myDom::removeNode(string xpath){
  cout <<"-------------removeNode start " <<endl;
  try{
    XalanNode * xpathNode=this->getNode(_evaluatorI,xpath);
    cout << "parent node type=" <<
xpathNode->getParentNode()->getNodeType()<< endl;
    cout << "parent node name=" <<
xpathNode->getParentNode()->getNodeName()<< endl;
    (xpathNode->getParentNode())->removeChild(xpathNode);
    cout << "remove child finished" <<endl;
    cout <<"-------------removeNode finished " <<endl;
  }
  catch (const XalanDOMException & e) {
    cout << "XalanDomExeption was thrown code=" << e.getExceptionCode()
<<endl
	 << e.getMessage().c_str() <<endl
	 << e.getType().c_str() <<endl
	 << e.getLineNumber <<endl
	 << e.getColumnNumber() <<endl;
    throw;
  }
  catch(...){
    cout << "Exeption was thrown" <<endl;
    throw;
  } 
}

int
main(
			int				argc,
			const char*		argv[])
{
#if !defined(XALAN_NO_NAMESPACES)
	using std::cerr;
	using std::cout;
	using std::endl;
#endif

	int		theResult = 0;
	if (argc != 4){
	  cerr << "Usage: SimpleXPathAPI XMLFilePath Context
XPathExpression" << endl;
	  theResult = -1;
	}
	else{
	  try{
	    vector<myDom *> domInstances;
	    domInstances.resize(1,NULL);
	    cout << "domInstances vector size=" << domInstances.size()
<<endl;
	    uint k,j,n;
	    // to initiate the vector
	    for (k=0; k<domInstances.size();k++){
	      domInstances[k] =  new myDom();
	     
	      if (! domInstances[k])
		cerr <<" no myDom Instance " << k << "was created"<<endl;
 
	      cout << "myDom instance " << k << "was created" <<endl;
	    }// end for 

	    for (j=0;j<domInstances.size();j++)

	    if (domInstances[j]){
	      cout << "myDom instance ---"<< j << "-----start " <<endl;
	      string tmp1= string(argv[1]); 
	      domInstances[j]->init(tmp1);
	      
	      string tmp2= string(argv[2]);
	      string tmp3= string(argv[3]);
	      cout << "myDom instance ---"<< j << "-----end " <<endl;
	    }
	    domInstances[0]->removeNode(string(argv[3]));

	    // delete all vector elements 
	    for  (n=0; n<domInstances.size();n++){
	      if (! domInstances[n])
		cerr <<"myDom Instance" << n << " was NOT created" <<endl;
	      else{ 
		delete domInstances[n];
		cout << "domInstance" << n << "was delete" <<endl;
	      }
	    }// end for

	  }
	  catch(...)
	    {
	      cerr << "Exception caught!" << endl;
	      theResult = -1;
	    }
	}
	return theResult;
};

myDom.hpp

#if !defined(MY_SIMPLE_XPATH_HEADER_GUARD_1357924680)
#define MY_SIMPLE_XPATH_HEADER_GUARD_1357924680



#include <Include/PlatformDefinitions.hpp>
#if defined(XALAN_OLD_STREAM_HEADERS)
#include <iostream.h>
#else
#include <iostream>
#endif

#include <string>
#include <vector>
#include <util/PlatformUtils.hpp>
#include <framework/LocalFileInputSource.hpp>
#include <framework/MemBufInputSource.hpp>

#include <XalanDOM/XalanDocument.hpp>
#include <XalanDOM/XalanElement.hpp>
#include <XalanDOM/XalanDOMString.hpp>
#include <XalanDOM/XalanDOMException.hpp> // way it was ok without it too?

#include <XPath/XObject.hpp>
#include <XPath/XPathEvaluator.hpp>
#include <XPath/NodeRefList.hpp>

#include <XalanSourceTree/XalanSourceTreeDOMSupport.hpp>
#include <XalanSourceTree/XalanSourceTreeInit.hpp>
#include <XalanSourceTree/XalanSourceTreeParserLiaison.hpp>
#include <XalanSourceTree/XalanSourceTreeElement.hpp>

class myDom
{

public:

  myDom();
  ~myDom();
  void init(char * buffer);
  void init(string fileName);  
  const string getValue(string xpath);
  XalanNode* getNode(XPathEvaluator * evaluator,string xpath="/");
  void removeNode(string xpath);

private:

  static int _initCounter;
  
  static XalanSourceTreeInit * _sourceTreeInit;
  static XalanSourceTreeDOMSupport * _domSupport;
  static XalanSourceTreeParserLiaison * _parserLiaison;

  static XPathEvaluator * _evaluatorI;
  static XPathEvaluator * _evaluatorII;

  // only one of the next variable is initiated using init
  LocalFileInputSource * _fileSource;
  MemBufInputSource * _bufferSource;
  
  XalanDocument * _xalanDocument;//not been deleted at the distructor since
ParserLiaison owns it
  XalanNode * _contextNode;//not been deleted at the distructor since delete
cause segmentation fault-way? 

  void initialize();
  void terminate();
  const string domStringToString(XalanDOMString domString); 
 
};


#endif	// MY_SIMPLE_XPATH_HEADER_GUARD_1357924680

hope some one can help
regards 
Ifat Tankel



 <<...OLE_Obj...>> 
ifat.tankel@comverse.com
Comverse Divisional Developer
03-6454768
053-762891



 <<...OLE_Obj...>> 
ifat.tankel@comverse.com
Comverse Divisional Developer
EMEA1 E7-General 3log
03-6454768
053-762891