473,406 Members | 2,369 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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 1928
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

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

Similar topics

6
by: cdecarlo | last post by:
Hello, I've often found that I am writing little scripts at the interpretor to read a text file, perform some conversion, and then write the converted data back out to a file. I normally...
16
by: Claudio Grondi | last post by:
I have a 250 Gbyte file (occupies the whole hard drive space) and want to change only eight bytes in this file at a given offset of appr. 200 Gbyte (all other data in that file should remain...
6
by: arne.muller | last post by:
Hello, I've come across some problems reading strucutres from binary files. Basically I've some strutures typedef struct { int i; double x; int n; double *mz;
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.