LLC2_API
xml_lookup.cpp
Go to the documentation of this file.
00001 /* ------------------------------------------------------------------------- */
00002 /* file xml_lookup.cpp                                                       */
00003 /* ------------------------------------------------------------------------- */
00004 /*   Copyright (C) 2011 Peter Milne, D-TACQ Solutions Ltd
00005  *                      <Peter dot Milne at D hyphen TACQ dot com>
00006  *  Created on: Sep 25, 2011
00007  *      Author: pgm
00008 
00009     http://www.d-tacq.com
00010 
00011     This program is free software; you can redistribute it and/or modify
00012     it under the terms of Version 2 of the GNU General Public License
00013     as published by the Free Software Foundation;
00014 
00015     This program is distributed in the hope that it will be useful,
00016     but WITHOUT ANY WARRANTY; without even the implied warranty of
00017     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018     GNU General Public License for more details.
00019 
00020     You should have received a copy of the GNU General Public License
00021     along with this program; if not, write to the Free Software
00022     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.                */
00023 /* ------------------------------------------------------------------------- */
00024 
00025 #include <stdio.h>
00026 #include <string.h>
00027 #include "xmlParser.h"
00028 
00029 #define MAXPATH 20      /* HUGE */
00030 #define DELIM   "/"
00031 
00032 char **path_elems;
00033 
00034 
00035 int xml_lookup(XMLNode current, char** path, int nelems)
00036 {
00037         if (nelems == 1){
00038                 if (path[0][0] == '@'){
00039                         const char* value = current.getAttribute(&path[0][1]);
00040                         printf("value=%s\n", value);
00041                         return 0;
00042                 }else{
00043                         XMLNode leaf = current.getChildNode(path[0]);
00044                         printf("%s\n", leaf.getText());
00045                 }
00046         }else{
00047                 int nc = current.nChildNode(path[0]);
00048                 for (int ic = 0; ic != nc; ++ic){
00049                         xml_lookup(current.getChildNode(path[0],ic), &path[1], nelems-1);
00050                 }       
00051         }
00052 }
00053 int xml_lookup(const char *doc, int nelems)
00054 {
00055         int ii;
00056         int ipath = 0;
00057         XMLNode root = XMLNode::openFileHelper(doc, path_elems[ipath]);
00058         XMLNode current = root;
00059         int nc;
00060 
00061         return xml_lookup(current, &path_elems[1], nelems -1);
00062 }
00063 
00064 int parse_path(const char* path)
00065 {
00066         char *buf = new char[strlen(path)+1];
00067         strcpy(buf, path);
00068         int nelems = 0;
00069         
00070         path_elems = new char* [MAXPATH];
00071         for (char* tok = buf; (tok = strtok(tok, DELIM)); tok = 0, nelems++){
00072                 path_elems[nelems] = tok;
00073         }
00074         return nelems;
00075 }
00076 
00077 int main(int argc, char* argv[])
00078 {
00079         if (argc != 3){
00080                 fprintf(stderr, "USAGE: xml_lookup doc path\n");
00081                 return -1;
00082         }
00083 
00084         
00085         return xml_lookup(argv[1], parse_path(argv[2]));
00086 }