I am new with Xerces-C but I managed to get a DOMTree runing and to extracte the data to the screen. However I need to write the Tree back to a file.... and this is where I am stuck.
Expand|Select|Wrap|Line Numbers
- #include <xercesc/dom/deprecated/DOMParser.hpp>
- #include <xercesc/util/PlatformUtils.hpp>
- #include <xercesc/dom/DOM_Document.hpp>
- #include <xercesc/dom/deprecated/DOM_DOMException.hpp>
- #include <xercesc/dom/deprecated/DOM_Element.hpp>
- #include <xercesc/dom/deprecated/DOM_Node.hpp>
- #include <string>
- #include <sstream>
- #include <stdexcept>
- #include <list>
- #include <iostream>
- #include <locale.h>
- using namespace std;
- XERCES_CPP_NAMESPACE_USE
- int main(int argc, char* argv[]){
- setlocale(LC_ALL,NULL);
- setlocale(LC_NUMERIC,"C");
- cout << "Starting program \n";
- cout << "Initializing the DOM ... ";
- try{
- //Initialize Xerces
- XMLPlatformUtils::Initialize();
- cout << " ok \n";
- }
- catch (const XMLException& toCatch) {
- char* message = XMLString::transcode(toCatch.getMessage());
- cout << " failed \n";
- cout << "Error during initialization! :\n"
- << message << "\n";
- XMLString::release(&message);
- return 1;
- }
- //Create parser
- DOMParser oParser;
- cout << "Parsing the XML file ... ";
- try{
- //Create the DOM treen and write it to memory
- oParser.parse("test2.xml");
- cout << " ok \n";
- }
- catch (const XMLException& toCatch) {
- cout << " failed \n";
- cout << "Exception message is: \n"
- << toCatch.getMessage()<< "\n";
- return -1;
- }
- catch (const DOM_DOMException&) {
- cout << " failed \n";
- cout << "Error parsing file \n";
- return -1;
- }
- catch (...) {
- cout << " failed \n";
- cout << "Unexpected Exception \n";
- return -1;
- }
- //Get the first Element in the XML file
- DOM_Document oDoc=oParser.getDocument();
- //access the tree
- if(oDoc!=0){
- DOM_Element oRoot=oDoc.getDocumentElement();
- if(oRoot!=0){
- DOM_Node oNode=oRoot.getFirstChild();
- while(oNode!=0){
- if(oNode.getNodeType()==DOM_Node::ELEMENT_NODE){
- DOM_Element oNodeEle=(DOM_Element&)oNode;
- if(oNodeEle.getNodeName().equals(DOMString("Port"))){
- DOMString oAttrVal=oNodeEle.getAttribute(DOMString("nr"));
- if(oAttrVal.length()>0){
- char* pcLocal=oAttrVal.transcode();
- int nPort=atoi(pcLocal);
- cout << "Port: " << nPort << endl;
- //Clear variable
- delete[]pcLocal;
- };
- }
- else if(oNodeEle.getNodeName().equals(DOMString("Data"))){
- DOMString oAttrVal=oNodeEle.getAttribute(DOMString("path"));
- if(oAttrVal.length()>0){
- char* pcLocal=oAttrVal.transcode();
- cout << "Data: " << pcLocal <<endl;
- //clear variable
- delete[]pcLocal;
- };
- };
- };
- //Go to next entry
- oNode=oNode.getNextSibling();
- };
- }else{
- cout << "Root node not found" << endl;
- };
- }else{
- cout << "Document not found" << endl;
- };
- cout << "Programm finished" << endl;
- return 0;
- }
Can anyone help me?
Thanks in advance.