2 * Copyright (C) 2007 Martin Willi
3 * Hochschule fuer Technik Rapperswil
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 #include <libxml/parser.h>
19 #include <libxml/tree.h>
22 typedef struct private_xml_t private_xml_t
;
27 struct private_xml_t
{
35 * root node of this xml (part)
40 * document, only for root xml_t
50 * number of enumerator instances
56 * child element enumerator
59 /** enumerator interface */
61 /** current child context (returned to enumerate() caller) */
63 /** currently processing node */
68 * Implementation of xml_t.children().enumerate().
70 static bool child_enumerate(child_enum_t
*e
, private_xml_t
**child
,
71 char **name
, char **value
)
73 while (e
->node
&& e
->node
->type
!= XML_ELEMENT_NODE
)
75 e
->node
= e
->node
->next
;
81 text
= e
->node
->children
;
84 while (text
&& text
->type
!= XML_TEXT_NODE
)
90 *value
= text
->content
;
92 *name
= (char*)e
->node
->name
;
94 e
->child
.node
= e
->node
->children
;
95 e
->node
= e
->node
->next
;
102 * Implementation of xml_t.get_attribute.
104 static char* get_attribute(private_xml_t
*this, char *name
)
110 * destroy enumerator, and complete tree if this was the last enumerator
112 static void child_destroy(child_enum_t
*this)
114 if (--this->child
.root
->enums
== 0)
116 xmlFreeDoc(this->child
.root
->doc
);
117 free(this->child
.root
);
123 * Implementation of xml_t.children.
125 static enumerator_t
* children(private_xml_t
*this)
127 child_enum_t
*ce
= malloc_thing(child_enum_t
);
128 ce
->e
.enumerate
= (void*)child_enumerate
;
129 ce
->e
.destroy
= (void*)child_destroy
;
130 ce
->node
= this->node
;
131 ce
->child
.public.children
= (void*)children
;
132 ce
->child
.public.get_attribute
= (void*)get_attribute
;
133 ce
->child
.node
= NULL
;
134 ce
->child
.doc
= this->doc
;
135 ce
->child
.root
= this->root
;
143 xml_t
*xml_create(char *xml
)
145 private_xml_t
*this = malloc_thing(private_xml_t
);
147 this->public.get_attribute
= (char*(*)(xml_t
*,char*))get_attribute
;
148 this->public.children
= (enumerator_t
*(*)(xml_t
*))children
;
150 this->doc
= xmlReadMemory(xml
, strlen(xml
), NULL
, NULL
, 0);
151 if (this->doc
== NULL
)
156 this->node
= xmlDocGetRootElement(this->doc
);
160 return &this->public;