00001
00002
00003
00004
00005
00006
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "nodeparser.h"
00021
00022 using std::string;
00023 using std::vector;
00024
00025 NodeParser::NodeParser(const xmlpp::Node::NodeList& list): xmlpp::Node::NodeList(list)
00026 {
00027 }
00028
00029 NodeParser::NodeParser(const xmlpp::Node* node)
00030 {
00031 push_back(const_cast<xmlpp::Node*>(node));
00032 }
00033
00034 NodeParser::NodeParser(const xmlpp::DomParser& parser)
00035 {
00036 xmlpp::Node* node = parser.get_document()->get_root_node();
00037 push_back(const_cast<xmlpp::Node*>(node));
00038 }
00039
00040 NodeParser NodeParser::Path(const xmlpp::Node* node,const std::string& path)
00041 {
00042
00043
00044 NodeParser result;
00045
00046
00047 std::string key = path;
00048 std::string remainder;
00049 std::string::size_type token_pos = path.find('/');
00050 if ( token_pos != std::string::npos )
00051 {
00052 key = path.substr(0, token_pos );
00053 remainder = path.substr( token_pos + 1 );
00054 }
00055
00056
00057 xmlpp::Node::NodeList list = node->get_children();
00058 for(xmlpp::Node::NodeList::iterator iter = list.begin(); iter != list.end(); ++iter)
00059 {
00060 if ( (*iter)->get_name() == key )
00061 {
00062
00063 if ( remainder.length() )
00064 {
00065 NodeParser remain_list = NodeParser(*iter).Path(remainder);
00066 result.splice(result.end(),remain_list);
00067 }
00068
00069
00070 else
00071 result.push_back(*iter);
00072 }
00073 }
00074
00075 return result;
00076 }
00077
00078 NodeParser NodeParser::Path(const std::string& path) const
00079 {
00080
00081 NodeParser result;
00082
00083 for(const_iterator iter = begin(); iter != end(); ++iter)
00084 {
00085 NodeParser iter_list = Path(*iter,path);
00086 result.splice(result.end(),iter_list);
00087 }
00088
00089 return result;
00090 }
00091
00092 NodeParser NodeParser::Select(const std::string& key, const std::string& value) const
00093 {
00094
00095 NodeParser result;
00096 for(const_iterator iter = begin(); iter != end(); ++iter)
00097 {
00098 xmlpp::Node::NodeList list = (*iter)->get_children();
00099 for(xmlpp::Node::NodeList::const_iterator iter3 = list.begin(); iter3 != list.end(); ++iter3)
00100 {
00101 if ( (*iter3)->get_name() == key )
00102 {
00103 xmlpp::Node::NodeList list = (*iter3)->get_children();
00104 for(xmlpp::Node::NodeList::const_iterator iter4 = list.begin(); iter4 != list.end(); ++iter4)
00105 {
00106 const xmlpp::TextNode* nodeText = dynamic_cast<const xmlpp::TextNode*>(*iter4);
00107 if ( nodeText && nodeText->get_content() == value )
00108 result.push_back(*iter);
00109 break;
00110 }
00111 }
00112 }
00113 }
00114 return result;
00115 }
00116
00117 vector<string> NodeParser::Text(void) const
00118 {
00119 vector<string> result;
00120
00121
00122 for(xmlpp::Node::NodeList::const_iterator iter = begin(); iter != end(); ++iter)
00123 {
00124
00125 xmlpp::Node::NodeList list = (*iter)->get_children();
00126 for(xmlpp::Node::NodeList::const_iterator iter2 = list.begin(); iter2 != list.end(); ++iter2)
00127 {
00128 const xmlpp::TextNode* nodeText = dynamic_cast<const xmlpp::TextNode*>(*iter2);
00129 if ( nodeText )
00130 {
00131 result.push_back(nodeText->get_content());
00132 }
00133 }
00134 }
00135 if ( result.empty() )
00136 result.push_back(string());
00137 return result;
00138 }
00139
00140