1 #include <iostream>
2 #include <libxml/xmlmemory.h>
3 #include <libxml/parser.h>
4 
5 #include "util/exception.h"
6 #include "xml.h"
7 #include "apicharlist.h"
8 
9 void
parse_xml(void)10 ApiCharacterList::parse_xml (void)
11 {
12   this->chars.clear();
13 
14   std::cout << "Parsing XML: Characters.xml ..." << std::endl;
15   XmlDocumentPtr xml = XmlDocument::create
16       (&this->http_data->data[0], this->http_data->data.size());
17   xmlNodePtr root = xml->get_root_element();
18   this->parse_eveapi_tag(root);
19 }
20 
21 /* ---------------------------------------------------------------- */
22 
23 void
parse_eveapi_tag(xmlNodePtr node)24 ApiCharacterList::parse_eveapi_tag (xmlNodePtr node)
25 {
26   if (node->type != XML_ELEMENT_NODE
27       || xmlStrcmp(node->name, (xmlChar const*)"eveapi"))
28     throw Exception("Invalid XML root. Expecting <eveapi> node.");
29 
30   for (node = node->children; node != 0; node = node->next)
31   {
32     /* Let the base class know of some fields. */
33     this->check_node(node);
34 
35     if (node->type == XML_ELEMENT_NODE
36         && !xmlStrcmp(node->name, (xmlChar const*)"result"))
37     {
38       //std::cout << "Found <result> tag" << std::endl;
39       this->parse_result_tag(node->children);
40     }
41   }
42 }
43 
44 /* ---------------------------------------------------------------- */
45 
46 void
parse_result_tag(xmlNodePtr node)47 ApiCharacterList::parse_result_tag (xmlNodePtr node)
48 {
49   for (; node != 0; node = node->next)
50   {
51     if (node->type != XML_ELEMENT_NODE)
52       continue;
53 
54     if (!xmlStrcmp(node->name, (xmlChar const*)"rowset"))
55     {
56       std::string name = this->get_property(node, "name");
57       if (name == "characters")
58         this->parse_characters(node->children);
59     }
60   }
61 }
62 
63 /* ---------------------------------------------------------------- */
64 
65 void
parse_characters(xmlNodePtr node)66 ApiCharacterList::parse_characters (xmlNodePtr node)
67 {
68   for (; node != 0; node = node->next)
69   {
70     if (node->type != XML_ELEMENT_NODE)
71       continue;
72 
73     if (!xmlStrcmp(node->name, (xmlChar const*)"row"))
74     {
75       try
76       {
77         ApiCharListEntry entry;
78         entry.name = this->get_property(node, "name");
79         entry.char_id = this->get_property(node, "characterID");
80         entry.corp = this->get_property(node, "corporationName");
81         entry.corp_id = this->get_property(node, "corporationID");
82         this->chars.push_back(entry);
83       }
84       catch (Exception& e)
85       {
86         throw Exception(e + " in element \"row\"");
87       }
88     }
89   }
90 }
91