00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00020 #include<ImLib3D/XMLTools.hpp>
00021 #include<ImLib3D/CppTools.hpp>
00022
00023 string
00024 GetAttribute(xmlpp::Element *node0,string attribute,bool fail)
00025 {
00026 xmlpp::Element *node=dynamic_cast<xmlpp::Element*>(node0);
00027 if(!node && !fail){return "";}
00028 if(!node && fail){ThrowError("GetAttribute: NULL node");}
00029 if(node->get_attribute(attribute))
00030 {
00031 return node->get_attribute(attribute)->get_value();
00032 }
00033 else
00034 {
00035 if(fail){ThrowError("did not find attribute \"%s\" for node \"%s\"",attribute,string(node->get_name()));}
00036 return "";
00037 }
00038 }
00039
00040 xmlpp::Element *
00041 Child(xmlpp::Element *parent,string name,bool fail)
00042 {
00043 if(!parent && !fail){return NULL;}
00044 if(!parent && fail){ThrowError("Child: NULL parent");}
00045 vector<xmlpp::Element *> nodeList=ChildElements(parent,name);
00046 if(nodeList.size()==0)
00047 {
00048 if(fail){ThrowError("Child: did not find child \"%s\" for parent \"%s\"",name,(string)parent->get_name());}
00049 return NULL;
00050 }
00051 else
00052 {
00053 return *(nodeList.begin());
00054 }
00055 }
00056
00057 string
00058 Spaces(int nspaces)
00059 {
00060 string res;
00061 for(int i=0;i<nspaces;i++){res+=" ";}
00062 return res;
00063 }
00064
00065 void
00066 RecursiveShowXMLElement(xmlpp::Element *node,int depth)
00067 {
00068 std::cout << std::endl;
00069
00070 const xmlpp::ContentNode* nodeContent = dynamic_cast<const xmlpp::ContentNode*>(node);
00071 const xmlpp::TextNode* nodeText = dynamic_cast<const xmlpp::TextNode*>(node);
00072 const xmlpp::CommentNode* nodeComment = dynamic_cast<const xmlpp::CommentNode*>(node);
00073
00074 if(nodeText && nodeText->is_white_space())
00075 return;
00076
00077 Glib::ustring nodename = node->get_name();
00078
00079 if(!nodeText && !nodeComment && !nodename.empty())
00080 {
00081 cout << Spaces(4*depth);
00082 std::cout << "Node name = " << node << " " << node->get_name() << std::endl;
00083 std::cout << "Node name = " << nodename << std::endl;
00084 }
00085 else if(nodeText)
00086 {
00087 cout << Spaces(4*depth);
00088 std::cout << "Text Node" << std::endl;
00089 }
00090
00091
00092 if(nodeText)
00093 {
00094 cout << Spaces(4*depth);
00095 std::cout << "text = \"" << nodeText->get_content() << "\"" << std::endl;
00096 }
00097 else if(nodeComment)
00098 {
00099 cout << Spaces(4*depth);
00100 std::cout << "comment = " << nodeComment->get_content() << std::endl;
00101 }
00102 else if(nodeContent)
00103 {
00104 cout << Spaces(4*depth);
00105 std::cout << "content = " << nodeContent->get_content() << std::endl;
00106 }
00107 else if(const xmlpp::Element* nodeElement = dynamic_cast<const xmlpp::Element*>(node))
00108 {
00109
00110
00111
00112 cout << Spaces(4*depth);
00113 std::cout << " line = " << node->get_line() << std::endl;
00114
00115
00116 const xmlpp::Element::AttributeList& attributes = nodeElement->get_attributes();
00117 for(xmlpp::Element::AttributeList::const_iterator iter = attributes.begin(); iter != attributes.end(); ++iter)
00118 {
00119 const xmlpp::Attribute* attribute = *iter;
00120 cout << Spaces(4*depth);
00121 std::cout << " Attribute " << attribute->get_name() << " = " << attribute->get_value() << std::endl;
00122 }
00123
00124 const xmlpp::Attribute* attribute = nodeElement->get_attribute("title");
00125 if(attribute)
00126 {
00127 std::cout << "title found: =" << attribute->get_value() << std::endl;
00128
00129 }
00130 }
00131
00132 if(!nodeContent)
00133 {
00134
00135 vector<xmlpp::Element *> list=ChildElements(node);
00136 for(vector<xmlpp::Element *>::iterator iter = list.begin(); iter != list.end(); ++iter)
00137 {
00138 RecursiveShowXMLElement(*iter, depth + 1);
00139 }
00140 }
00141 }
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179 void
00180 ReadText(xmlpp::Element *node,string textNodeName,string &res,bool fail)
00181 {
00182 xmlpp::Element *child=Child(node,textNodeName,false);
00183 if(!child)
00184 {
00185 if(fail){ThrowError("ReadText: could not find child node:\"%s\"",textNodeName);}
00186 res="";
00187 return ;
00188 }
00189 res="";
00190 xmlpp::TextNode *text=child->get_child_text();
00191 if(text){res=text->get_content();}
00192 return;
00193 }