//XMLDemo.cpp - command line demo program to process XML Bible Files
// By Jim Skon, MVNU, April 2011
#include <string>
#include <iostream>
#include <sstream>

#define XML_USE_STL
#include "xmlParser.h"

using namespace std;

/* Template to allow conversion of other types to strings */
template <class T>
inline std::string to_string (const T& t)
{
  std::stringstream ss;
  ss << t;
  return ss.str();
}

main (int argc, char ** argv) {

  string bible_file;

  if (argc != 4) {
    cout << "Usage: " << argv[0] << " Book Chapter Verse" << endl;
    exit(2);
  }
  int book = atoi(argv[1]);
  int chapter = atoi(argv[2]);
  int verse = atoi(argv[3]);
  
  if (book < 1 || book > 66) {
    cout << "Bad book" << endl;
    exit(2);
  }
  bible_file = "Strongs/kjv_by_book/" + to_string<int>(book)  + ".xml";
  cout << "file: " << bible_file << endl;

  if (chapter < 1) {
    cout << "Bad chapter" << endl;
    exit(2);
  }

  if (verse < 1) {
    cout << "Bad verse" << endl;
    exit(2);
  }

  cout << "Reference: " << book << ":" << chapter << ":" << verse << endl;

  // this opens and parses the correct XML file:
  XMLNode xMainNode=XMLNode::openFileHelper(bible_file.c_str(),"bible");

  // this prints the book name:
  XMLNode bookNode=xMainNode.getChildNode("book");
  cout << "Book Name is:" << bookNode.getAttribute("name") << endl;

  XMLNode chNode=bookNode.getChildNode("chapter",chapter-1);
  cout << "Chapter is:" << chNode.getAttribute("number") << endl;

  XMLNode verNode=chNode.getChildNode("verse",verse-1);
  cout << "Verse is:" << verNode.getAttribute("number") << endl;

  int i = 0, itext = 0, ichild = 0;
  string nodeName;
  
  /* Traverse the verse structure */
   while (i < verNode.nElement()) {
    switch (verNode.enumContents(i++).etype) {

      /* an eNodeChild means an embedded node, probably a strongs entry */
    case eNodeChild:
      /* Print the node's Bible text if it is there */
      if (verNode.getChildNode(ichild).nText() > 0)
	cout << verNode.getChildNode(ichild).getText() << " ";
      /* Get the Strong's references associated with this node */
      nodeName =  verNode.getChildNode(ichild).getName();
      /* If the node is strongs, print the text, and the number */
      if (nodeName == "strongs") {
	if (verNode.getChildNode(ichild).getAttribute("hebrew") != NULL) {
	  cout <<  "(H:" << verNode.getChildNode(ichild).getAttribute("hebrew") << ") ";
	} else	if (verNode.getChildNode(ichild).getAttribute("greek") != NULL) {
	  cout <<  "(G:" << verNode.getChildNode(ichild).getAttribute("greek") << ") ";
	}
      } else {
	/* Common node is "em", for emphasis */
	//cout << "Node = " << nodeName << endl;
      }
      ichild++;
      break;

    case eNodeAttribute: 
      //cout << "Attr ";
      break;

    case eNodeText:
      /* Print out the verse text */
      cout << verNode.getText(itext++) << " ";
      break;

    case eNodeClear:
      cout << "Clear ";
      break;

    case eNodeNULL:
      cout << "Null ";
      break;
    }
  }
  cout << endl;
}

-- JimSkon - 2011-04-25

Topic revision: r1 - 2011-04-25 - JimSkon
 
This site is powered by the TWiki collaboration platformCopyright &© by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback