XMLHelpers.cxx

Go to the documentation of this file.
00001 #include "AtlfastAlgs/XMLHelpers.h"
00002 
00003 namespace Atlfast{
00004   
00005   // ============================================
00006   
00007   char* getText(DOMNode* node){
00008     if (node->getNodeType()==DOMNode::ELEMENT_NODE){
00009       DOMNodeList* children = node->getChildNodes();
00010       for(XMLSize_t cptr = 0; cptr < children->getLength();++cptr){
00011         
00012         DOMNode* cnode = children->item(cptr);
00013         
00014         if (cnode->getNodeType()==DOMNode::TEXT_NODE){
00015           DOMText* textNode = static_cast<DOMText*>(cnode);
00016           return XMLString::transcode(textNode->getData());
00017         }
00018       }
00019     }
00020     return 0;
00021   }
00022   
00023   std::vector<DOMNode*>  getAllChildTagsByName(DOMNode* node, std::string name) {
00024     DOMNodeList* children = node->getChildNodes();
00025     
00026     std::vector<DOMNode*> nodes;
00027     for(XMLSize_t ptr = 0; ptr < children->getLength(); ++ptr){
00028       DOMNode* node = children->item(ptr);
00029       char* nodeName = XMLString::transcode(node->getNodeName());
00030       if ( strcmp(nodeName, name.c_str()) == 0){
00031         nodes.push_back(node);
00032       }
00033       XMLString::release(&nodeName);
00034     }
00035     
00036     // std::cout<<"found "<<nodes.size()<<" for name "<<name<<std::endl;
00037     return nodes;
00038   }
00039   
00040   // ============================================
00041   
00042   DOMNode* getFirstChildTagByName(DOMNode* node, std::string name) {
00043     DOMNodeList* children = node->getChildNodes();
00044     
00045     for(XMLSize_t ptr = 0; ptr < children->getLength(); ++ptr){
00046       DOMNode* node = children->item(ptr);
00047       char* nodeName = XMLString::transcode(node->getNodeName());
00048       bool useThisNode = strcmp(nodeName, name.c_str()) == 0;
00049       XMLString::release(&nodeName);
00050       if ( useThisNode ) return node;
00051     }
00052     
00053     return 0;
00054   }
00055   
00056   // ============================================
00057 
00058   void getFirstValue(DOMNode* node, std::string tagname, std::string&  value){
00059     
00060     DOMNode* cnode = getFirstChildTagByName(node, tagname);
00061     if(cnode != 0){
00062       char *textPtr = getText(cnode);
00063       value = textPtr;
00064       delete [] textPtr;
00065     }else{
00066       std::cout<<"setFirstValue(): tagname "<<tagname
00067                <<" not found"<<std::endl;   
00068     }
00069   }  
00070   
00071   // ============================================
00072   
00073   void getFirstValue(DOMNode* node, std::string tagname, double& value){
00074     
00075     DOMNode* cnode = getFirstChildTagByName(node, tagname);
00076     if(cnode != 0){
00077       char *textPtr = getText(cnode);
00078       value = atof(textPtr);
00079       delete [] textPtr;
00080     }else{
00081       std::cout<<"setFirstValue(): tagname "<<tagname
00082                <<" not found"<<std::endl;   
00083     }
00084   }  
00085   
00086   // ============================================
00087   
00088   void getFirstValue(DOMNode* node, std::string tagname, int& value){
00089     
00090     DOMNode* cnode = getFirstChildTagByName(node,tagname);
00091     if(cnode != 0){
00092       char *textPtr = getText(cnode);
00093       value = atoi(textPtr);
00094       delete [] textPtr;
00095     }else{
00096       std::cout<<"setFirstValue(): tagname "<<tagname
00097                <<" not found"<<std::endl;   
00098     }
00099   }  
00100   
00101   // ============================================
00102   
00103   void getAllValues(DOMNode* node, std::string tagname, std::vector<double>& values){
00104     
00105     // Empty the vector first
00106     values.clear();
00107 
00108     DOMNode* cnode = getFirstChildTagByName(node,tagname);
00109     if(cnode != 0){
00110 
00111       char* xtmp = getText(cnode);
00112       std::string input_values(xtmp);
00113       xercesc::XMLString::release( &xtmp );
00114       
00115       std::stringstream ss(input_values,
00116                            std::stringstream::in | std::stringstream::out);
00117       
00118       double value = 0;
00119       while (!ss.eof()){
00120         ss >> value;
00121         values.push_back(value);
00122       }
00123     }else{
00124       std::cout<<"setFirstValue(): tagname "<<tagname
00125                <<" not found"<<std::endl;   
00126     }
00127   }  
00128     
00129   // ============================================
00130   
00131   DOMDocument* parseFileForDocument(const char* xmlFile) {
00132     
00133     std::cout<<"Starting parseFileForDocument: "<<xmlFile<<std::endl;
00134     
00135     try {
00136       XMLPlatformUtils::Initialize();
00137     }
00138     catch (const XMLException& toCatch) {
00139       char* message = XMLString::transcode(toCatch.getMessage());
00140       std::cout << "Error during initialization! :\n"
00141                 << message << "\n";
00142       XMLString::release(&message);
00143       return 0;
00144     }
00145     
00146     XercesDOMParser* parser = new XercesDOMParser();
00147     // parser->setValidationScheme(XercesDOMParser::Val_Always);    // optional.
00148     parser->setDoNamespaces(true);    // optional
00149     
00150     ErrorHandler* errHandler = (ErrorHandler*) new HandlerBase();
00151     parser->setErrorHandler(errHandler);
00152     
00153     std::cout<<"Parse - start\n";
00154     try {
00155       parser->parse(xmlFile);
00156       std::cout<<"Parse - end\n";
00157     }
00158     catch (const XMLException& toCatch) {
00159       char* message = XMLString::transcode(toCatch.getMessage());
00160       std::cout << "Exception message is: \n"
00161                 << message << "\n";
00162       XMLString::release(&message);
00163       return 0;
00164     }
00165     catch (const DOMException& toCatch) {
00166       char* message = XMLString::transcode(toCatch.msg);
00167       std::cout << "Exception message is: \n"
00168                 << message << "\n";
00169       XMLString::release(&message);
00170       return 0;
00171     }
00172     catch (...) {
00173       std::cout << "Unexpected Exception \n" ;
00174       return 0;
00175     }
00176     
00177     
00178     // Changing what happens here (SJHD 25/07/2006)
00179     // The ownership of the DOMDocument must transfer away from
00180     // the Parser at this point, so that the parser can be released
00181     // but the document's info can continue to be accessed. The
00182     // DOMDocument is then deleted at a later stage to release the
00183     // (huge amount of) associated memory.
00184 
00185     DOMDocument* dom = parser->adoptDocument();
00186     
00187     // Getting rid of error-handler and parser
00188     delete errHandler;
00189     delete parser;
00190     
00191     if (dom == 0){
00192       std::cout<<"Zero DOM pointer, bailing out...\n";
00193       return 0;
00194     }else return dom;
00195     
00196   }
00197   
00198   // ============================================
00199   
00200   std::vector<char*> getTagVals(DOMElement* docElement, const char* tagname){
00201     DOMNodeList* tags = 
00202       docElement->getElementsByTagName(XMLString::transcode(tagname));
00203     
00204     std::cout<<"Found "<<tags->getLength()<<" tags with name "<<tagname<<std::endl;
00205     std::vector<char*> tagVals;
00206     
00207     for(XMLSize_t ptr = 0; ptr < tags->getLength();++ptr){
00208       
00209       DOMNode* node = tags->item(ptr);
00210       std::vector<DOMText*> textNodes;
00211       if (node->getNodeType()==DOMNode::ELEMENT_NODE){
00212         DOMNodeList* children = node->getChildNodes();
00213         for(XMLSize_t cptr = 0; cptr < children->getLength();++cptr){
00214           
00215           DOMNode* cnode = children->item(cptr);
00216           
00217           if (cnode->getNodeType()==DOMNode::TEXT_NODE){
00218             textNodes.push_back(static_cast<DOMText*>(cnode));
00219           }
00220         }
00221         if (textNodes.size() != 1){
00222           std::cout<<"Error: found "<<textNodes.size()<<" text nodes"<<std::endl;
00223         }
00224         std::vector<DOMText*>::const_iterator itr = textNodes.begin();
00225         std::vector<DOMText*>::const_iterator end = textNodes.end();
00226         for(;itr!=end;++itr){
00227           tagVals.push_back(XMLString::transcode((*itr)->getData()));
00228         }
00229       } 
00230     }
00231     return tagVals;
00232   }
00233   
00234 } //namespace Atlfast

Generated on Mon Sep 24 14:19:12 2007 for AtlfastAlgs by  doxygen 1.5.1