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 we...@gmx.de on 2008/05/21 10:47:42 UTC

How to append Document to another Document?

hi

I am trying to append a DOMDocument to another DOMDocument:


DOMDocument *dom_root1 = parser1->getDocument();
DOMDocument *dom_root2 = parser2->getDocument();
		
dom_root1->appendChild(dom_root2);


but I get the error:
A node is used in a different document than the one that created it

greatings
  Affe
-- 
Psssst! Schon vom neuen GMX MultiMessenger gehört?
Der kann`s mit allen: http://www.gmx.net/de/go/multimessenger

Re: How to append Document to another Document?

Posted by we...@gmx.de.
hi Alberto

Thanks, i didn't look carefully, now its ok

greetings
-- 
Ist Ihr Browser Vista-kompatibel? Jetzt die neuesten 
Browser-Versionen downloaden: http://www.gmx.net/de/go/browser

Re: How to append Document to another Document?

Posted by Alberto Massari <am...@datadirect.com>.
You are importing the nodes from root2 using root2 itself; you should 
use parser->getDocument() (my snippet uses dom_root1 for both 
appendChild and importNode)

Alberto


wernwa@gmx.de wrote:
> hi Alberto
>
> I get the same Error again, even with importNode. Could you please look at my code,
>
> thanks
>
>
> #include <iostream>
> #include <vector>
> #include <map>
>
> #include <xercesc/framework/StdOutFormatTarget.hpp>
> #include <xercesc/framework/LocalFileFormatTarget.hpp>
> #include <xercesc/util/XMLUri.hpp>
> #include <xercesc/parsers/XercesDOMParser.hpp>
> #include <xercesc/framework/MemBufInputSource.hpp>
> #include <xercesc/util/OutOfMemoryException.hpp>
> #include <xercesc/framework/MemBufFormatTarget.hpp>
> #include <xercesc/dom/DOMException.hpp>
>
> //XQilla includes
> #include <xqilla/xqilla-simple.hpp>
> #include <xqilla/ast/LocationInfo.hpp>
> #include <xqilla/context/MessageListener.hpp>
> #include <xqilla/utils/PrintAST.hpp>
> #include <xqilla/events/EventSerializer.hpp>
> #include <xqilla/events/NSFixupFilter.hpp>
> #include <xqilla/xerces/XercesConfiguration.hpp>
> #include <xqilla/fastxdm/FastXDMConfiguration.hpp>
> #include <xqilla/utils/XQillaPlatformUtils.hpp>
>
> #if defined(XERCES_HAS_CPP_NAMESPACE)
> XERCES_CPP_NAMESPACE_USE
> #endif
>
> using namespace std;
>
> #define QUERY_BUFFER_SIZE 32 * 1024
> #define BASEURI_BUFFER_SIZE 2 * 1024
>
>
> int main(int argc, char *argv[])
> {
>   
> 	XMLPlatformUtils::Initialize();
> 	XQillaPlatformUtils::initialize();
>
>
>     XercesDOMParser *parser = new XercesDOMParser();
>     parser->setValidationScheme(XercesDOMParser::Val_Auto);
>     parser->setDoNamespaces(true);
>     parser->setDoSchema(false);
>     parser->setValidationSchemaFullChecking(false);
>     parser->setCreateEntityReferenceNodes(false);
>
>
>     XercesDOMParser *parser2 = new XercesDOMParser();
>     parser2->setValidationScheme(XercesDOMParser::Val_Auto);
>     parser2->setDoNamespaces(true);
>     parser2->setDoSchema(false);
>     parser2->setValidationSchemaFullChecking(false);
>     parser2->setCreateEntityReferenceNodes(false);
>
>         std::string xml("\
> <test>\
>       <b b1n='b1'> some b1 text\
>     </b>\
>     <b b2n='b2'>some b2 text\
>     </b>\
>     <a url='test.xml' name='aaaa'/>\
> </test>\
> ");
>
>  MemBufInputSource*memBufIS = new MemBufInputSource
>  (
>         (const XMLByte*)xml.c_str()
>         , xml.length()
>         , "somedoc"
>         , false
>  );
>
>
>
>
>  try
>     {
>         parser->parse(*memBufIS);
> 	parser2->parse(*memBufIS);
>     }
>     catch (const OutOfMemoryException&)
>     {
>         cout<<"OutOfMemoryException"<<endl;
>     }
>     catch (const XMLException& e)
>     {
>         cout << "XMLException" << endl;
>     }
>     
>     catch (...)
>     {
>         cout << "some unnown exception" << endl;
>     }
>     DOMNode*root = parser->getDocument()->getFirstChild();
>     ((DOMDocument*)parser->getDocument())->setDocumentURI(0);
>     DOMNode*root2 = parser2->getDocument();
>     ((DOMDocument*)parser2->getDocument())->setDocumentURI(0);
>
> 	try{
> //
> //
> // Here is the import of the doc2
> //
> //
> 		DOMNode* n=root2->getFirstChild();
> 		while(n)
> 		{
>   			root->appendChild(((DOMDocument*)root2)->importNode(n,true));
>   			n = n->getNextSibling();
> 		}
>
> 	}
> 	catch (const DOMException& toCatch) {
>             char* message = XMLString::transcode(toCatch.msg);
>             cout << "Exception message is: \n"
>                  << message << "\n";
>             XMLString::release(&message);
> 		return -1;
>         }
>
>
>     XercesConfiguration xercesConf;
>     XQillaConfiguration *conf = &xercesConf;
>     XQilla xqilla;
>     int language = XQilla::XQUERY;
>     language |= XQilla::UPDATE;
>      Janitor<DynamicContext> contextGuard
>         (xqilla.createContext((XQilla::Language)language, conf));
>     DynamicContext *context = contextGuard.get();
>
>     context->setXPath1CompatibilityMode(false);  
>     MemBufFormatTarget*memtarget=0;
>
>
> try{
>     XQQuery *query = xqilla.parse(
>
>
>         //X("declare revalidation skip;/test/b")
>
>
> 		X("declare revalidation skip;\
> /test\
> ")
> 	
>
>         ,contextGuard.release()
>             );
>
> 	//DynamicContext *dynamic_context= new DynamicContext(query->createDynamicContext());
>     Janitor<DynamicContext> dynamic_context(query->createDynamicContext());
>     Node::Ptr node = xercesConf.createNode(root,dynamic_context.get());
>     dynamic_context->setContextItem(node.get());
>     dynamic_context->setContextPosition(1);
>     dynamic_context->setContextSize(1);
>
>
>     int mem=0;
> 	//int mem=1024;
>    memtarget = new MemBufFormatTarget(mem,
>                       dynamic_context->getMemoryManager());
>    EventSerializer writer("UTF-8", "1.1", memtarget,  
>             dynamic_context->getMemoryManager());
>    writer.addNewlines(true);
>    NSFixupFilter nsfilter(&writer, dynamic_context->getMemoryManager());
>    query->execute(&nsfilter, dynamic_context.get());
>
>     int count = memtarget->getLen()*sizeof(XMLByte);
>     string result((char*)memtarget->getRawBuffer(),
>                     count);
>     cout << "length :" << count << endl;
>         cout <<  result << endl;
>
>    }
>   catch(XQException &e) {}
>
>
>
> 	XMLPlatformUtils::Terminate();
>     XQillaPlatformUtils::terminate();
> return 0;
> }
>
> -------- Original-Nachricht --------
>   
>> Datum: Wed, 21 May 2008 12:39:15 +0200
>> Von: Alberto Massari <am...@datadirect.com>
>> An: c-users@xerces.apache.org
>> Betreff: Re: How to append Document to another Document?
>>     
>
>   
>> Hi Affe,
>> documents can only contain nodes created by the same document; so you 
>> first need to first import the nodes in the target document
>>
>> DOMNode* n=dom_root2->getFirstChild();
>> while(n)
>> {
>>   dom_root1->appendChild(dom_root1->importNode(n));
>>   n = n->getNextSibling();
>> }
>>
>> Alberto
>>
>> wernwa@gmx.de wrote:
>>     
>>> hi
>>>
>>> I am trying to append a DOMDocument to another DOMDocument:
>>>
>>>
>>> DOMDocument *dom_root1 = parser1->getDocument();
>>> DOMDocument *dom_root2 = parser2->getDocument();
>>> 		
>>> dom_root1->appendChild(dom_root2);
>>>
>>>
>>> but I get the error:
>>> A node is used in a different document than the one that created it
>>>
>>> greatings
>>>   Affe
>>>   
>>>       
>
>   



Re: How to append Document to another Document?

Posted by we...@gmx.de.
hi Alberto

I get the same Error again, even with importNode. Could you please look at my code,

thanks


#include <iostream>
#include <vector>
#include <map>

#include <xercesc/framework/StdOutFormatTarget.hpp>
#include <xercesc/framework/LocalFileFormatTarget.hpp>
#include <xercesc/util/XMLUri.hpp>
#include <xercesc/parsers/XercesDOMParser.hpp>
#include <xercesc/framework/MemBufInputSource.hpp>
#include <xercesc/util/OutOfMemoryException.hpp>
#include <xercesc/framework/MemBufFormatTarget.hpp>
#include <xercesc/dom/DOMException.hpp>

//XQilla includes
#include <xqilla/xqilla-simple.hpp>
#include <xqilla/ast/LocationInfo.hpp>
#include <xqilla/context/MessageListener.hpp>
#include <xqilla/utils/PrintAST.hpp>
#include <xqilla/events/EventSerializer.hpp>
#include <xqilla/events/NSFixupFilter.hpp>
#include <xqilla/xerces/XercesConfiguration.hpp>
#include <xqilla/fastxdm/FastXDMConfiguration.hpp>
#include <xqilla/utils/XQillaPlatformUtils.hpp>

#if defined(XERCES_HAS_CPP_NAMESPACE)
XERCES_CPP_NAMESPACE_USE
#endif

using namespace std;

#define QUERY_BUFFER_SIZE 32 * 1024
#define BASEURI_BUFFER_SIZE 2 * 1024


int main(int argc, char *argv[])
{
  
	XMLPlatformUtils::Initialize();
	XQillaPlatformUtils::initialize();


    XercesDOMParser *parser = new XercesDOMParser();
    parser->setValidationScheme(XercesDOMParser::Val_Auto);
    parser->setDoNamespaces(true);
    parser->setDoSchema(false);
    parser->setValidationSchemaFullChecking(false);
    parser->setCreateEntityReferenceNodes(false);


    XercesDOMParser *parser2 = new XercesDOMParser();
    parser2->setValidationScheme(XercesDOMParser::Val_Auto);
    parser2->setDoNamespaces(true);
    parser2->setDoSchema(false);
    parser2->setValidationSchemaFullChecking(false);
    parser2->setCreateEntityReferenceNodes(false);

        std::string xml("\
<test>\
      <b b1n='b1'> some b1 text\
    </b>\
    <b b2n='b2'>some b2 text\
    </b>\
    <a url='test.xml' name='aaaa'/>\
</test>\
");

 MemBufInputSource*memBufIS = new MemBufInputSource
 (
        (const XMLByte*)xml.c_str()
        , xml.length()
        , "somedoc"
        , false
 );




 try
    {
        parser->parse(*memBufIS);
	parser2->parse(*memBufIS);
    }
    catch (const OutOfMemoryException&)
    {
        cout<<"OutOfMemoryException"<<endl;
    }
    catch (const XMLException& e)
    {
        cout << "XMLException" << endl;
    }
    
    catch (...)
    {
        cout << "some unnown exception" << endl;
    }
    DOMNode*root = parser->getDocument()->getFirstChild();
    ((DOMDocument*)parser->getDocument())->setDocumentURI(0);
    DOMNode*root2 = parser2->getDocument();
    ((DOMDocument*)parser2->getDocument())->setDocumentURI(0);

	try{
//
//
// Here is the import of the doc2
//
//
		DOMNode* n=root2->getFirstChild();
		while(n)
		{
  			root->appendChild(((DOMDocument*)root2)->importNode(n,true));
  			n = n->getNextSibling();
		}

	}
	catch (const DOMException& toCatch) {
            char* message = XMLString::transcode(toCatch.msg);
            cout << "Exception message is: \n"
                 << message << "\n";
            XMLString::release(&message);
		return -1;
        }


    XercesConfiguration xercesConf;
    XQillaConfiguration *conf = &xercesConf;
    XQilla xqilla;
    int language = XQilla::XQUERY;
    language |= XQilla::UPDATE;
     Janitor<DynamicContext> contextGuard
        (xqilla.createContext((XQilla::Language)language, conf));
    DynamicContext *context = contextGuard.get();

    context->setXPath1CompatibilityMode(false);  
    MemBufFormatTarget*memtarget=0;


try{
    XQQuery *query = xqilla.parse(


        //X("declare revalidation skip;/test/b")


		X("declare revalidation skip;\
/test\
")
	

        ,contextGuard.release()
            );

	//DynamicContext *dynamic_context= new DynamicContext(query->createDynamicContext());
    Janitor<DynamicContext> dynamic_context(query->createDynamicContext());
    Node::Ptr node = xercesConf.createNode(root,dynamic_context.get());
    dynamic_context->setContextItem(node.get());
    dynamic_context->setContextPosition(1);
    dynamic_context->setContextSize(1);


    int mem=0;
	//int mem=1024;
   memtarget = new MemBufFormatTarget(mem,
                      dynamic_context->getMemoryManager());
   EventSerializer writer("UTF-8", "1.1", memtarget,  
            dynamic_context->getMemoryManager());
   writer.addNewlines(true);
   NSFixupFilter nsfilter(&writer, dynamic_context->getMemoryManager());
   query->execute(&nsfilter, dynamic_context.get());

    int count = memtarget->getLen()*sizeof(XMLByte);
    string result((char*)memtarget->getRawBuffer(),
                    count);
    cout << "length :" << count << endl;
        cout <<  result << endl;

   }
  catch(XQException &e) {}



	XMLPlatformUtils::Terminate();
    XQillaPlatformUtils::terminate();
return 0;
}

-------- Original-Nachricht --------
> Datum: Wed, 21 May 2008 12:39:15 +0200
> Von: Alberto Massari <am...@datadirect.com>
> An: c-users@xerces.apache.org
> Betreff: Re: How to append Document to another Document?

> Hi Affe,
> documents can only contain nodes created by the same document; so you 
> first need to first import the nodes in the target document
> 
> DOMNode* n=dom_root2->getFirstChild();
> while(n)
> {
>   dom_root1->appendChild(dom_root1->importNode(n));
>   n = n->getNextSibling();
> }
> 
> Alberto
> 
> wernwa@gmx.de wrote:
> > hi
> >
> > I am trying to append a DOMDocument to another DOMDocument:
> >
> >
> > DOMDocument *dom_root1 = parser1->getDocument();
> > DOMDocument *dom_root2 = parser2->getDocument();
> > 		
> > dom_root1->appendChild(dom_root2);
> >
> >
> > but I get the error:
> > A node is used in a different document than the one that created it
> >
> > greatings
> >   Affe
> >   
> 

-- 
Psssst! Schon vom neuen GMX MultiMessenger gehört?
Der kann`s mit allen: http://www.gmx.net/de/go/multimessenger

Re: How to append Document to another Document?

Posted by Alberto Massari <am...@datadirect.com>.
Hi Affe,
documents can only contain nodes created by the same document; so you 
first need to first import the nodes in the target document

DOMNode* n=dom_root2->getFirstChild();
while(n)
{
  dom_root1->appendChild(dom_root1->importNode(n));
  n = n->getNextSibling();
}

Alberto

wernwa@gmx.de wrote:
> hi
>
> I am trying to append a DOMDocument to another DOMDocument:
>
>
> DOMDocument *dom_root1 = parser1->getDocument();
> DOMDocument *dom_root2 = parser2->getDocument();
> 		
> dom_root1->appendChild(dom_root2);
>
>
> but I get the error:
> A node is used in a different document than the one that created it
>
> greatings
>   Affe
>