473,757 Members | 10,736 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Writing a DOMTree back to a file

4 New Member
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 1945
dorinbogdan
839 Recognized Expert Contributor
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 Recognized Expert Moderator Expert
Have you read this about the DOMWriter?
May 30 '07 #3
Arengin
4 New Member
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 Recognized Expert Moderator Expert
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
1291
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 accomplish the above task by reading the lines of the entire file into a list, preforming some function to that list and then writing the list back out. I want to write a generic function/module that will free me from repeatedly typing the same thing,...
16
7188
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 unchanged). How can I do that in Python? Claudio Grondi
6
5272
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
9298
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10072
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9885
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9737
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8737
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7286
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6562
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5329
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3829
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.