1 #include <sngxml/xpath/XPathEvaluate.hpp>
2 #include <sngxml/xpath/InitDone.hpp>
3 #include <sngxml/dom/Parser.hpp>
4 #include <sngxml/dom/Element.hpp>
5 #include <soulng/util/CodeFormatter.hpp>
6 #include <soulng/util/InitDone.hpp>
7 #include <soulng/util/Path.hpp>
8 #include <soulng/util/Unicode.hpp>
9 #include <iostream>
10 #include <stdexcept>
11 #include <stdlib.h>
12
13 using namespace soulng::util;
14 using namespace soulng::unicode;
15
16 std::string GetSoulNGRootDir()
17 {
18 const char* soulngRootEnv = getenv("SOULNG_ROOT");
19 std::string soulngRootDir;
20 if (soulngRootEnv)
21 {
22 soulngRootDir = soulngRootEnv;
23 }
24 if (soulngRootDir.empty())
25 {
26 throw std::runtime_error("please set SOULNG_ROOT environment variable to point to /path/to/soulng directory");
27 }
28 return soulngRootDir;
29 }
30
31 struct Initializer
32 {
33 Initializer()
34 {
35 soulng::util::Init();
36 sngxml::xpath::Init();
37 }
38 ~Initializer()
39 {
40 sngxml::xpath::Done();
41 soulng::util::Done();
42 }
43 };
44
45
46
47
48
49
50
51
52 int main()
53 {
54 Initializer initializer;
55 try
56 {
57 std::unique_ptr<sngxml::dom::Document> xmlDoc = sngxml::dom::ReadDocument(GetFullPath(Path::Combine(GetSoulNGRootDir(), "examples/sngxml/foo.xml")));
58 std::unique_ptr<sngxml::xpath::XPathObject> result = sngxml::xpath::Evaluate(U"/doc/foo", xmlDoc.get());
59 if (result)
60 {
61 if (result->Type() == sngxml::xpath::XPathObjectType::nodeSet)
62 {
63 sngxml::xpath::XPathNodeSet* nodeSet = static_cast<sngxml::xpath::XPathNodeSet*>(result.get());
64 for (int i = 0; i < nodeSet->Length(); ++i)
65 {
66 sngxml::dom::Node* node = (*nodeSet)[i];
67 if (node->GetNodeType() == sngxml::dom::NodeType::elementNode)
68 {
69 sngxml::dom::Element* element = static_cast<sngxml::dom::Element*>(node);
70 soulng::util::WriteUtf8(std::cout, ToUtf8(element->Name()));
71 std::cout << std::endl;
72 }
73 }
74 }
75 }
76 }
77 catch (const std::exception& ex;)
78 {
79 std::cerr << ex.what() << std::endl;
80 return 1;
81 }
82 return 0;
83 }