Main Page   Namespace List   Class Hierarchy   Compound List   File List   Compound Members  

xpath_tokens.cpp

00001 #include <iostream>
00002 #include <list>
00003 
00004 #include "judo.hpp"
00005 #include "XPath.h"
00006 
00007 using namespace std;
00008 using namespace judo;
00009 using namespace judo::XPath;
00010 
00011 int main(int argc, char **argv)
00012 {
00013     judo::Element doc("doc");
00014     judo::Element* elem = new judo::Element("test");
00015     elem->putAttrib("attr", "valu");
00016     elem->putAttrib("attr2", "valu2");
00017     elem->addElement("foo","bar1");
00018     doc.appendChild(elem);
00019     elem = new judo::Element("test2");
00020     judo::Element* child = new judo::Element("child");
00021     elem->appendChild(child);
00022     doc.appendChild(elem);
00023     elem = new judo::Element("test");
00024     doc.appendChild(elem);
00025     elem = new judo::Element("test2");
00026     elem->putAttrib("key", "value");
00027     child = new judo::Element("child");
00028     elem->appendChild(child);
00029     doc.appendChild(elem);
00030     elem = new judo::Element("test");
00031     elem->putAttrib("attr", "valu-test");
00032     elem->putAttrib("attr2", "valu3");
00033     elem->putAttrib("attr3", "something");
00034     elem->addElement("foo","bar2");
00035     elem->addElement("foo","bar3");
00036     elem->addElement("sub", "test-child");
00037     doc.appendChild(elem);
00038     elem = doc.addElement("test2");
00039     elem->putAttrib("attr", "valu-test2");
00040 
00041 
00042     if (argc >= 3)
00043         cout << doc.toString() << endl;
00044 
00045     try
00046     {
00047         XPath::Query query = XPath::Query(argv[1]);
00048         XPath::Value* result = query.execute(&doc);
00049         if (argc >= 3)
00050         {
00051             if (!result->check())
00052                 cout << "No match" << endl;
00053             else
00054             {
00055                 cout << "Yay, it matched" << endl;
00056                 XPath::Value::AttribMap attribs = result->getAttribs();
00057                 XPath::Value::ValueList values = result->getValues();
00058                 XPath::Value::ElemList elems = result->getList();
00059                 cout << "And the results are: " << endl;
00060 
00061                 if (!attribs.empty())
00062                 {
00063                     XPath::Value::AttribMap::iterator it = attribs.begin();
00064                     while(it != attribs.end())
00065                     {
00066                         cout << "attrib: " << it->first << " = '" << it->second << "'" << endl;
00067                         it++;
00068                     }
00069 
00070 
00071                 }
00072                 else if (!values.empty())
00073                 {
00074                     XPath::Value::ValueList::iterator it = values.begin();
00075                     while( it != values.end() )
00076                     {
00077                         std::string val = *it;
00078                         cout << "value: " << val << endl;
00079                         it++;
00080                     }
00081                 }
00082                 else
00083                 {
00084                     XPath::Value::ElemList::iterator it = elems.begin();
00085                     while( it != elems.end() )
00086                     {
00087                         judo::Element* elem = *it;
00088                         cout << "elem: " << elem->toString() << endl;
00089                         it++;
00090                     }
00091                 }
00092             }
00093         }
00094     }
00095     catch(XPath::Query::Invalid)
00096     {
00097         cout << "Unable to create query" << endl;
00098     };
00099 }
00100 

Generated on Thu Jul 24 13:31:51 2003 for jabberoo by doxygen1.3-rc3