4 * @brief Implementation of xml_t.
9 * Copyright (C) 2007 Martin Willi
10 * Hochschule fuer Technik Rapperswil
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the
14 * Free Software Foundation; either version 2 of the License, or (at your
15 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
19 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
25 #include <libxml/parser.h>
26 #include <libxml/tree.h>
29 typedef struct private_xml_t private_xml_t
;
34 struct private_xml_t
{
42 * root node of this xml (part)
47 * document, only for root xml_t
57 * number of enumerator instances
63 * child element enumerator
66 /** enumerator interface */
68 /** current child context (returned to enumerate() caller) */
70 /** currently processing node */
75 * Implementation of xml_t.children().enumerate().
77 static bool child_enumerate(child_enum_t
*e
, private_xml_t
**child
,
78 char **name
, char **value
)
80 while (e
->node
&& e
->node
->type
!= XML_ELEMENT_NODE
)
82 e
->node
= e
->node
->next
;
88 text
= e
->node
->children
;
91 while (text
&& text
->type
!= XML_TEXT_NODE
)
97 *value
= text
->content
;
99 *name
= (char*)e
->node
->name
;
101 e
->child
.node
= e
->node
->children
;
102 e
->node
= e
->node
->next
;
109 * Implementation of xml_t.get_attribute.
111 static char* get_attribute(private_xml_t
*this, char *name
)
117 * destroy enumerator, and complete tree if this was the last enumerator
119 static void child_destroy(child_enum_t
*this)
121 if (--this->child
.root
->enums
== 0)
123 xmlFreeDoc(this->child
.root
->doc
);
124 free(this->child
.root
);
130 * Implementation of xml_t.children.
132 static enumerator_t
* children(private_xml_t
*this)
134 child_enum_t
*ce
= malloc_thing(child_enum_t
);
135 ce
->e
.enumerate
= (void*)child_enumerate
;
136 ce
->e
.destroy
= (void*)child_destroy
;
137 ce
->node
= this->node
;
138 ce
->child
.public.children
= (void*)children
;
139 ce
->child
.public.get_attribute
= (void*)get_attribute
;
140 ce
->child
.node
= NULL
;
141 ce
->child
.doc
= this->doc
;
142 ce
->child
.root
= this->root
;
150 xml_t
*xml_create(char *xml
)
152 private_xml_t
*this = malloc_thing(private_xml_t
);
154 this->public.get_attribute
= (char*(*)(xml_t
*,char*))get_attribute
;
155 this->public.children
= (enumerator_t
*(*)(xml_t
*))children
;
157 this->doc
= xmlReadMemory(xml
, strlen(xml
), NULL
, NULL
, 0);
158 if (this->doc
== NULL
)
163 this->node
= xmlDocGetRootElement(this->doc
);
167 return &this->public;