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>
- //For backup test start
- #include <xercesc/dom/DOM.hpp>
- #include <xercesc/util/XMLString.hpp>
- #include <xercesc/dom/DOMImplementation.hpp>
- #include <xercesc/dom/DOMImplementationLS.hpp>
- #include <xercesc/dom/DOMImplementationRegistry.hpp>
- #include <xercesc/framework/StdOutFormatTarget.hpp>
- //End
- #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();
- cout << "oRoot= " << &oRoot <<endl;
- 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;
- //Writing to file try start
- const XMLCh gLS[]={ chLatin_L, chLatin_S, chNull};
- DOMImplementation* impl= DOMImplementationRegistry::getDOMImplementation(gLS);
- DOMWriter* theSerializer=((DOMImplementationLS*)impl)->createDOMWriter();
- if(theSerializer->canSetFeature(XMLUni::fgDOMXMLDeclaration,true))
- theSerializer->setFeature(XMLUni::fgDOMXMLDeclaration,true);
- DOMDocument* doc;
- doc = impl->createDocument(0,XMLString::transcode("Config"),0);
- doc->setEncoding(XMLString::transcode("UTF-8"));
- doc->setVersion(XMLString::transcode("1.0"));
- DOMNode* node = (DOMDocument*)doc->getDocumentElement();
- XMLFormatTarget *myFormTarget;
- myFormTarget = new StdOutFormatTarget();
- try{
- cout << endl;
- theSerializer->writeNode(myFormTarget, *doc);
- cout << endl << endl;
- }
- catch(...){
- cout << "Fehler am Ende!" << endl;
- }
- theSerializer->release();
- //Writing to file try end
- return 0;
- }
and all I get as output is:
<?xml version="1.0" encoding="iso-8859-1"?><Config />
but the file looks like:
Expand|Select|Wrap|Line Numbers
- <?xml version="1.0" encoding="iso-8859-1"?>
- <Config>
- <Port nr="50000" />
- <Data path="."/>
- </Config>
What am I doing wrong?? Any idea??
Thanks in advance