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 George Kassapoglou <gk...@intranet.gr> on 2003/06/26 10:24:50 UTC

Using DOMWriter and DOMParser

Dear friends,

I 'm new in Xerces so don't count on me very much.

DOMParser parses an xml file and creates an xml tree.
Then you can get a reference on the tree and change it
(add new elements, erase some etc.) with DOMDocument and
write it to an xml file with DOMWriter.
Example:
XercesDOMParser* parser = new XercesDOMParser();
.
//some flags eg. parser->setDoSchema(true);
.

try{
	parser->parse(your_xml_file);
}
catch(const XMLException& toCatch)
{
	char* message = XMLString::transcode(toCatch.getMessage());
	cout << "Exception message is : \n"
	     << message << "\n";
	XMLString::release(&message);
	return -1;
}

DOMDocument* doc = parser->getDocument();
DOMNode* root = doc->getDocumentElement();

.
//Manipulate the tree
.

DOMImplementation* impl =
DOMImplementationRegistry::getDOMImplementation(0);
DOMWriter* writer = ((DOMImplementationLS*)impl)->createDOMWriter();
LocalFileFormatTarget myFormTarget(your_file_name);

//And after all that, you can write your xml tree in a file
try
{
	writer->writeNode(&myFormTarget , *root);
}
catch(...) { cout << "Unexpected error.\n"; }
writer->release();

delete parser;


//Finish


I use it and it works.


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