arabica/examples/XPath/xgrep.cpp

87 lines
2.1 KiB
C++
Raw Normal View History

2005-08-09 12:30:42 +02:00
#ifdef _MSC_VER
#pragma warning(disable: 4786 4250 4503)
#endif
#include <string>
2007-09-05 00:55:47 +02:00
#include <SAX/helpers/CatchErrorHandler.hpp>
#include <DOM/SAX2DOM/SAX2DOM.hpp>
2007-09-13 23:21:55 +02:00
#include <DOM/io/Stream.hpp>
2005-08-09 12:30:42 +02:00
#include <XPath/XPath.hpp>
////////////////////////////////////////////////
int main(int argc, char* argv[])
{
if(argc < 3)
{
std::cout << "Usage : " << argv[0] << " xpath xmlfile ... " << std::endl;
return 0;
} // if(argc < 3)
2005-08-09 12:30:42 +02:00
2005-08-16 22:59:04 +02:00
Arabica::XPath::XPath<std::string> xpathParser;
2006-07-14 12:09:14 +02:00
Arabica::XPath::XPathExpressionPtr<std::string> xpath;
try {
//xpath = xpathParser.compile(argv[1]);
xpath = xpathParser.compile_expr(argv[1]);
}
catch(const std::runtime_error& error) {
std::cout << "XPath compilation error: " << error.what() << std::endl;
return 0;
}
2005-08-09 12:30:42 +02:00
2007-09-05 13:47:13 +02:00
Arabica::SAX2DOM::Parser<std::string> domParser;
2007-09-05 11:49:18 +02:00
Arabica::SAX::CatchErrorHandler<std::string> eh;
2005-08-09 12:30:42 +02:00
domParser.setErrorHandler(eh);
for(int i = 2; i < argc; ++i)
{
std::string file(argv[i]);
Arabica::SAX::InputSource<std::string> is;
2005-08-09 12:30:42 +02:00
is.setSystemId(file);
if(file != "-")
domParser.parse(is);
else
{
is.setSystemId("stdin");
is.setByteStream(std::cin);
domParser.parse(is);
} // if(file != "-")
if(!eh.errorsReported())
{
2007-09-05 13:47:13 +02:00
Arabica::DOM::Document<std::string> doc = domParser.getDocument();
2006-08-04 11:53:06 +02:00
// doc.normalize();
2005-11-09 22:13:22 +01:00
Arabica::XPath::XPathValuePtr<std::string> result;
result = xpath->evaluate(doc);
std::cout << file << std::endl;
switch(result->type())
2005-11-09 22:13:22 +01:00
{
case Arabica::XPath::NODE_SET:
2005-11-09 22:13:22 +01:00
{
const Arabica::XPath::NodeSet<std::string>& ns = result->asNodeSet();
for(unsigned int i = 0; i < ns.size(); ++i)
{
Arabica::DOM::Node<std::string> n = ns[i];
std::cout << n << std::endl;
}
}
break;
default:
std::cout << result->asString() << std::endl;
} // switch
2005-08-09 12:30:42 +02:00
}
else
{
std::cerr << eh.errors() << std::endl;
eh.reset();
} // if ...
} // for ...
return 0;
} // main
// end of file