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 "jerome.mariette" <je...@mbari.org> on 2007/07/12 00:59:09 UTC

DOMWriter

Hi everybody,
I'm new in xerces and I get some trouble just to write a basic XML file !!!
I try to create a DOM document from scratch and then to write it into a file
on the disk, here is the code:

	int errorCode = 0;
	if(!appendEventsXML && impl != 0) {
		try {  
			
			//XMLFormatTarget *myFormTarget = new
LocalFileFormatTarget(X("res.xml"));
			DOMWriter *theSerializer =
((DOMImplementationLS*)impl)->createDOMWriter();
			
			DOMDocument* doc = impl->createDocument(
							   0,                    // root element namespace URI.
	                           X("company"),         // root element name
	                           0);                   // document type object
(DTD).
	                           
	                           
			DOMElement* rootElem = doc->getDocumentElement();
	
			DOMElement*  prodElem = doc->createElement(X("product"));
			rootElem->appendChild(prodElem);
			
			DOMText*    prodDataVal = doc->createTextNode(X("Xerces-C"));
			prodElem->appendChild(prodDataVal);
			
			DOMElement*  catElem = doc->createElement(X("category"));
			rootElem->appendChild(catElem);
			
			catElem->setAttribute(X("idea"), X("great"));
			
			DOMText*    catDataVal = doc->createTextNode(X("XML Parsing Tools"));
			catElem->appendChild(catDataVal);
			
			DOMElement*  devByElem = doc->createElement(X("developedBy"));
			rootElem->appendChild(devByElem);
			
			DOMText*    devByDataVal = doc->createTextNode(X("Apache Software
Foundation"));
			devByElem->appendChild(devByDataVal);                  
	
			//
			// Now count the number of elements in the above DOM tree.
			//
	
			unsigned int elementCount =
doc->getElementsByTagName(X("*"))->getLength();
			XERCES_STD_QUALIFIER cout << "The tree just created contains: " <<
elementCount
			<< " elements." << XERCES_STD_QUALIFIER endl;

			XMLFormatTarget *myFormatTarget = new
LocalFileFormatTarget("myXMLFile.xml");
			theSerializer->writeNode(myFormatTarget, *prodElem);
			
			
		}
		catch (const OutOfMemoryException&)
		{
			XERCES_STD_QUALIFIER cerr << "OutOfMemoryException" <<
XERCES_STD_QUALIFIER endl;
			errorCode = 5;
		}
		catch (const DOMException& e)
		{
			XERCES_STD_QUALIFIER cerr << "DOMException code is:  " << e.code <<
XERCES_STD_QUALIFIER endl;
			errorCode = 2;
		}
		catch (...)
		{
			XERCES_STD_QUALIFIER cerr << "An error occurred creating the document" <<
XERCES_STD_QUALIFIER endl;
			errorCode = 3;
		}
	} else {
		XERCES_STD_QUALIFIER cerr << "Requested implementation is not supported"
<< XERCES_STD_QUALIFIER endl;
		errorCode = 4;
	}

What am I doing wrong ?? the file is created but nothing is inside !!!
thanks for your help
-- 
View this message in context: http://www.nabble.com/DOMWriter-tf4065159.html#a11550772
Sent from the Xerces - C - Dev mailing list archive at Nabble.com.


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


Re: DOMWriter

Posted by Alberto Massari <am...@datadirect.com>.
At 15.44 13/07/2007 -0700, jerome.mariette wrote:


>Ok so sounds like I absolutly have to delete my XMLFormatTarget if I want
>the XML writen !!
>Is that normal ??

Well, it's good housekeeping to delete the objects you created; apart 
for that, if you still want to leak that object, you can call flush() 
to have the cache written to the disk.

Alberto






>jerome.mariette wrote:
> >
> > Hi everybody,
> > I'm new in xerces and I get some trouble just to write a basic XML file
> > !!! I try to create a DOM document from scratch and then to write it into
> > a file on the disk, here is the code:
> >
> >       int errorCode = 0;
> >       if(!appendEventsXML && impl != 0) {
> >               try {
> >
> >                       //XMLFormatTarget *myFormTarget = new
> > LocalFileFormatTarget(X("res.xml"));
> >                       DOMWriter *theSerializer =
> > ((DOMImplementationLS*)impl)->createDOMWriter();
> >
> >                       DOMDocument* doc = impl->createDocument(
> >                                                          0, 
>               // root element namespace URI.
> >                                  X("company"),         // root element name
> >                                  0);                   // 
> document type object
> > (DTD).
> >
> >
> >                       DOMElement* rootElem = doc->getDocumentElement();
> >
> >                       DOMElement*  prodElem = 
> doc->createElement(X("product"));
> >                       rootElem->appendChild(prodElem);
> >
> >                       DOMText*    prodDataVal = 
> doc->createTextNode(X("Xerces-C"));
> >                       prodElem->appendChild(prodDataVal);
> >
> >                       DOMElement*  catElem = 
> doc->createElement(X("category"));
> >                       rootElem->appendChild(catElem);
> >
> >                       catElem->setAttribute(X("idea"), X("great"));
> >
> >                       DOMText*    catDataVal = 
> doc->createTextNode(X("XML Parsing Tools"));
> >                       catElem->appendChild(catDataVal);
> >
> >                       DOMElement*  devByElem = 
> doc->createElement(X("developedBy"));
> >                       rootElem->appendChild(devByElem);
> >
> >                       DOMText*    devByDataVal = 
> doc->createTextNode(X("Apache Software
> > Foundation"));
> >                       devByElem->appendChild(devByDataVal); 
>
> >
> >                       //
> >                       // Now count the number of elements in the 
> above DOM tree.
> >                       //
> >
> >                       unsigned int elementCount =
> > doc->getElementsByTagName(X("*"))->getLength();
> >                       XERCES_STD_QUALIFIER cout << "The tree just 
> created contains: " <<
> > elementCount
> >                       << " elements." << XERCES_STD_QUALIFIER endl;
> >
> >                       XMLFormatTarget *myFormatTarget = new
> > LocalFileFormatTarget("myXMLFile.xml");
> >                       theSerializer->writeNode(myFormatTarget, *prodElem);
> >
> >
> >               }
> >               catch (const OutOfMemoryException&)
> >               {
> >                       XERCES_STD_QUALIFIER cerr << 
> "OutOfMemoryException" <<
> > XERCES_STD_QUALIFIER endl;
> >                       errorCode = 5;
> >               }
> >               catch (const DOMException& e)
> >               {
> >                       XERCES_STD_QUALIFIER cerr << "DOMException 
> code is:  " << e.code <<
> > XERCES_STD_QUALIFIER endl;
> >                       errorCode = 2;
> >               }
> >               catch (...)
> >               {
> >                       XERCES_STD_QUALIFIER cerr << "An error 
> occurred creating the document"
> > << XERCES_STD_QUALIFIER endl;
> >                       errorCode = 3;
> >               }
> >       } else {
> >               XERCES_STD_QUALIFIER cerr << "Requested 
> implementation is not supported"
> > << XERCES_STD_QUALIFIER endl;
> >               errorCode = 4;
> >       }
> >
> > What am I doing wrong ?? the file is created but nothing is inside !!!
> > thanks for your help
> >
>
>--
>View this message in context: 
>http://www.nabble.com/DOMWriter-tf4065159.html#a11588797
>Sent from the Xerces - C - Dev mailing list archive at Nabble.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


Re: DOMWriter

Posted by "jerome.mariette" <je...@mbari.org>.

Ok so sounds like I absolutly have to delete my XMLFormatTarget if I want
the XML writen !! 
Is that normal ?? 






jerome.mariette wrote:
> 
> Hi everybody,
> I'm new in xerces and I get some trouble just to write a basic XML file
> !!! I try to create a DOM document from scratch and then to write it into
> a file on the disk, here is the code:
> 
> 	int errorCode = 0;
> 	if(!appendEventsXML && impl != 0) {
> 		try {  
> 			
> 			//XMLFormatTarget *myFormTarget = new
> LocalFileFormatTarget(X("res.xml"));
> 			DOMWriter *theSerializer =
> ((DOMImplementationLS*)impl)->createDOMWriter();
> 			
> 			DOMDocument* doc = impl->createDocument(
> 							   0,                    // root element namespace URI.
> 	                           X("company"),         // root element name
> 	                           0);                   // document type object
> (DTD).
> 	                           
> 	                           
> 			DOMElement* rootElem = doc->getDocumentElement();
> 	
> 			DOMElement*  prodElem = doc->createElement(X("product"));
> 			rootElem->appendChild(prodElem);
> 			
> 			DOMText*    prodDataVal = doc->createTextNode(X("Xerces-C"));
> 			prodElem->appendChild(prodDataVal);
> 			
> 			DOMElement*  catElem = doc->createElement(X("category"));
> 			rootElem->appendChild(catElem);
> 			
> 			catElem->setAttribute(X("idea"), X("great"));
> 			
> 			DOMText*    catDataVal = doc->createTextNode(X("XML Parsing Tools"));
> 			catElem->appendChild(catDataVal);
> 			
> 			DOMElement*  devByElem = doc->createElement(X("developedBy"));
> 			rootElem->appendChild(devByElem);
> 			
> 			DOMText*    devByDataVal = doc->createTextNode(X("Apache Software
> Foundation"));
> 			devByElem->appendChild(devByDataVal);                  
> 	
> 			//
> 			// Now count the number of elements in the above DOM tree.
> 			//
> 	
> 			unsigned int elementCount =
> doc->getElementsByTagName(X("*"))->getLength();
> 			XERCES_STD_QUALIFIER cout << "The tree just created contains: " <<
> elementCount
> 			<< " elements." << XERCES_STD_QUALIFIER endl;
> 
> 			XMLFormatTarget *myFormatTarget = new
> LocalFileFormatTarget("myXMLFile.xml");
> 			theSerializer->writeNode(myFormatTarget, *prodElem);
> 			
> 			
> 		}
> 		catch (const OutOfMemoryException&)
> 		{
> 			XERCES_STD_QUALIFIER cerr << "OutOfMemoryException" <<
> XERCES_STD_QUALIFIER endl;
> 			errorCode = 5;
> 		}
> 		catch (const DOMException& e)
> 		{
> 			XERCES_STD_QUALIFIER cerr << "DOMException code is:  " << e.code <<
> XERCES_STD_QUALIFIER endl;
> 			errorCode = 2;
> 		}
> 		catch (...)
> 		{
> 			XERCES_STD_QUALIFIER cerr << "An error occurred creating the document"
> << XERCES_STD_QUALIFIER endl;
> 			errorCode = 3;
> 		}
> 	} else {
> 		XERCES_STD_QUALIFIER cerr << "Requested implementation is not supported"
> << XERCES_STD_QUALIFIER endl;
> 		errorCode = 4;
> 	}
> 
> What am I doing wrong ?? the file is created but nothing is inside !!!
> thanks for your help
> 

-- 
View this message in context: http://www.nabble.com/DOMWriter-tf4065159.html#a11588797
Sent from the Xerces - C - Dev mailing list archive at Nabble.com.


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


Re: DOMWriter

Posted by Ali_baba <su...@yahoo.com>.
Add these two line at the end of the  code
theSerializer->writeNode(myFormTarget, *xmlDoc);
delete theSerializer;
delete myFormTarget;


-- 
View this message in context: http://www.nabble.com/DOMWriter-tf4065159.html#a12032178
Sent from the Xerces - C - Dev mailing list archive at Nabble.com.


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