1 /* Copyright (C) 2000-2004 Thomas Bopp, Thorsten Hampel, Ludger Merkens
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 * $Id: xml_parser.pike,v 1.1 2008/03/31 13:39:57 exodusd Exp $
21 //! this is a structure to make accessing Parser.XML.Node(s) easier.
30 string node_to_str(object n);
38 mapping pi = ([ ]); // processing instructions;
44 * Parse given data using the Parser.XML.Tree module.
46 * @param string data - the xml data to parse.
47 * @return NodeXML structure described by its root-node.
49 NodeXML parse_data(string data)
55 * Converts a node of an XML Tree to a string.
57 * @param NodeXML ann - the node to convert.
58 * @return string representation of the Node and it children recursively.
61 string node_to_str(NodeXML ann)
67 foreach(indices(ann->attributes), attr) {
68 if ( attr != ann->name )
69 res += " " + attr + "=\""+ann->attributes[attr]+"\"";
72 foreach(ann->children, NodeXML child) {
73 res += "<"+child->name;
74 foreach(indices(child->attributes), attr) {
75 if ( attr != child->name )
76 res += " " + attr + "=\""+child->attributes[attr]+"\"";
78 res += ">" + child->data + children_to_str(child->children)+
79 "</"+child->name+">\n";
81 res += "</"+ann->name+">\n";
86 * Some conversion function I forgot where it is used at all.
88 * @param array annotations - an array of annotations to convert
89 * @return a string representation of the annotations.
91 string children_to_str(array annotations)
94 if ( !arrayp(annotations) )
97 foreach(annotations, NodeXML ann) {
98 res += node_to_str(ann);
104 * Convert some annotations to a string representation by using the
105 * children_to_str function. Remember annotations can be annotated again!
107 * @param array annotations - the annotations to convert.
108 * @return string representation of the annotations.
109 * @see children_to_str
111 string convert_annotations(array annotations)
114 foreach(annotations, NodeXML ann) {
115 res += children_to_str(ann->children);
121 * Display the structure of a XML Tree given by NodeXML node.
123 * @param NodeXML node - the node, for example the root-node of the tree.
124 * @return just writes the structure to stderr.
126 void display_structure(NodeXML node, void|int depth)
128 for ( int i = 0 ; i < depth; i++ )
130 werror(node->name+":"+node->data+"\n");
131 foreach(node->children, NodeXML n) {
132 display_structure(n, depth+1);
137 * Create a mapping from an XML Tree.
139 * @param NodeXML n - the root-node to transform to a mapping.
140 * @return converted mapping.
142 mapping xmlMap(NodeXML n)
145 foreach ( n->children, NodeXML children) {
146 if ( children->name == "member" ) {
148 foreach(children->children, object o) {
150 if ( o->name == "key" )
151 key = unserialize(o->children[0]);
152 else if ( o->name == "value" )
153 value = unserialize(o->children[0]);
162 * Create an array with the childrens of the given Node.
164 * @param NodeXML n - the current node to unserialize.
165 * @return Array with unserialized childrens.
167 array xmlArray(NodeXML n)
170 foreach ( n->children, NodeXML children) {
171 res += ({ unserialize(children) });
177 * Create some data structure from an XML Tree.
179 * @param NodeXML n - the root-node of the XML Tree to unserialize.
180 * @return some data structure describing the tree.
182 mixed unserialize(NodeXML n)
195 return (float)n->data;
206 string xml = "<?xml version='1.0'?><a>1<b>2</b>3<c a='1'>4</c></a>";
207 object node = parse_data(xml);
208 object n = node->get_node("/a/b");
209 if ( !objectp(n) || n->data != "2" )
210 error("Failed to resolve xpath expression.");
211 n->replace_node("<huh/>");
212 if ( node->get_xml()!= "<a >13<huh ></huh>\n<c a='1' >4</c>\n</a>\n" )
213 error("replacing of <b/> didnt work !\nResult is:"+node->get_xml());
218 mixed err = catch(parse_data(xml));
220 error("Wrong xml code does not throw error.\n");
222 xml = "<a><b test=1/></a>";
223 err = catch(parse_data(xml));
225 error("Wrong xml code does not throw error.\n");