469,939 Members | 2,345 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,939 developers. It's quick & easy.

Writing a DOMTree back to a file

HI everyone,

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
  1.     #include <xercesc/dom/deprecated/DOMParser.hpp>
  2.     #include <xercesc/util/PlatformUtils.hpp>
  3.     #include <xercesc/dom/DOM_Document.hpp>
  4.     #include <xercesc/dom/deprecated/DOM_DOMException.hpp>
  5.     #include <xercesc/dom/deprecated/DOM_Element.hpp>
  6.     #include <xercesc/dom/deprecated/DOM_Node.hpp>
  7.     #include <string>
  8.     #include <sstream>
  9.     #include <stdexcept>
  10.     #include <list>
  11.     #include <iostream>
  12.     #include <locale.h>
  13.  
  14.  
  15.     using namespace std;
  16.  
  17.     XERCES_CPP_NAMESPACE_USE
  18.  
  19.  
  20.     int main(int argc, char* argv[]){
  21.  
  22.         setlocale(LC_ALL,NULL);
  23.         setlocale(LC_NUMERIC,"C");
  24.  
  25.         cout << "Starting program \n";
  26.         cout << "Initializing the DOM ... ";
  27.  
  28.         try{
  29.             //Initialize Xerces
  30.             XMLPlatformUtils::Initialize();
  31.             cout << " ok \n";
  32.         }
  33.         catch (const XMLException& toCatch) {
  34.             char* message = XMLString::transcode(toCatch.getMessage());
  35.             cout << " failed \n";
  36.             cout << "Error during initialization! :\n"
  37.                  << message << "\n";
  38.             XMLString::release(&message);
  39.             return 1;
  40.         }
  41.  
  42.         //Create parser
  43.         DOMParser oParser;
  44.  
  45.         cout << "Parsing the XML file ... ";
  46.  
  47.         try{
  48.             //Create the DOM treen and write it to memory
  49.             oParser.parse("test2.xml");
  50.             cout << " ok \n";
  51.         }
  52.         catch (const XMLException& toCatch) {
  53.             cout << " failed \n";
  54.             cout << "Exception message is: \n"
  55.                  << toCatch.getMessage()<< "\n";
  56.             return -1;
  57.         }
  58.         catch (const DOM_DOMException&) {
  59.             cout << " failed \n";
  60.             cout << "Error parsing file \n";
  61.             return -1;
  62.         }
  63.         catch (...) {
  64.             cout << " failed \n";
  65.             cout << "Unexpected Exception \n";
  66.             return -1;
  67.         }
  68.  
  69.         //Get the first Element in the XML file
  70.         DOM_Document oDoc=oParser.getDocument();
  71.  
  72.         //access the tree
  73.         if(oDoc!=0){
  74.             DOM_Element oRoot=oDoc.getDocumentElement();
  75.  
  76.             if(oRoot!=0){
  77.                 DOM_Node oNode=oRoot.getFirstChild();
  78.  
  79.                 while(oNode!=0){
  80.                     if(oNode.getNodeType()==DOM_Node::ELEMENT_NODE){
  81.                         DOM_Element oNodeEle=(DOM_Element&)oNode;
  82.  
  83.                         if(oNodeEle.getNodeName().equals(DOMString("Port"))){
  84.                             DOMString oAttrVal=oNodeEle.getAttribute(DOMString("nr"));
  85.  
  86.                             if(oAttrVal.length()>0){
  87.                                 char* pcLocal=oAttrVal.transcode();
  88.                                 int nPort=atoi(pcLocal);
  89.  
  90.                                 cout << "Port: " << nPort << endl;
  91.  
  92.                                 //Clear variable
  93.                                 delete[]pcLocal;
  94.                             };
  95.                         }
  96.  
  97.                         else if(oNodeEle.getNodeName().equals(DOMString("Data"))){
  98.                             DOMString oAttrVal=oNodeEle.getAttribute(DOMString("path"));
  99.  
  100.                             if(oAttrVal.length()>0){
  101.                                 char* pcLocal=oAttrVal.transcode();
  102.  
  103.                                 cout << "Data: " << pcLocal <<endl;
  104.  
  105.                                 //clear variable
  106.                                 delete[]pcLocal;
  107.                             };
  108.                         };
  109.  
  110.                     };
  111.  
  112.                     //Go to next entry
  113.                     oNode=oNode.getNextSibling();
  114.  
  115.                 };
  116.  
  117.             }else{
  118.                 cout << "Root node not found" << endl;
  119.             };
  120.  
  121.         }else{
  122.             cout << "Document not found" << endl;
  123.         };
  124.  
  125.         cout << "Programm finished" << endl;
  126.  
  127.         return 0;
  128.  
  129.     }
  130.  
This is my code till now I now want to add a routine that writes the whole tree back to a file.... however I don't know where to start with that and how. I already checked the Xerces sight and other helps but I can't get anywhere somehow.

Can anyone help me?

Thanks in advance.
May 30 '07 #1
4 1585
dorinbogdan
839 Expert 512MB
Welcome to TheScripts TSDN...

I posted a link to your question in the C/C++ forum too.

Dorin.
May 30 '07 #2
Banfa
9,065 Expert Mod 8TB
Have you read this about the DOMWriter?
May 30 '07 #3
Have you read this about the DOMWriter?

Yes sadly I have to admit that I did but for some reason I can't adated it or get it to work
May 30 '07 #4
Banfa
9,065 Expert Mod 8TB
Yes sadly I have to admit that I did but for some reason I can't adated it or get it to work
Perhaps you should stop using the depricated classes, like DOMParser and use the latest classes, like XercesDOMParser or DOMBuilder. These may work better with DOMWriter since they are part of the same API release level.
May 30 '07 #5

Post your reply

Sign in to post your reply or Sign up for a free account.

By using this site, you agree to our Privacy Policy and Terms of Use.