2 * Copyright (C) 2007 Martin Willi
4 * Copyright (C) secunet Security Networks AG
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21 #include <libxml/parser.h>
22 #include <libxml/tree.h>
25 typedef struct private_xml_t private_xml_t
;
30 struct private_xml_t
{
38 * root node of this xml (part)
43 * document, only for root xml_t
53 * number of enumerator instances
59 * child element enumerator
62 /** enumerator interface */
64 /** current child context (returned to enumerate() caller) */
66 /** currently processing node */
70 METHOD(enumerator_t
, child_enumerate
, bool,
71 child_enum_t
*e
, va_list args
)
73 private_xml_t
**child
;
76 VA_ARGS_VGET(args
, child
, name
, value
);
78 while (e
->node
&& e
->node
->type
!= XML_ELEMENT_NODE
)
80 e
->node
= e
->node
->next
;
86 text
= e
->node
->children
;
89 while (text
&& text
->type
!= XML_TEXT_NODE
)
95 *value
= text
->content
;
97 *name
= (char*)e
->node
->name
;
99 e
->child
.node
= e
->node
->children
;
100 e
->node
= e
->node
->next
;
106 METHOD(xml_t
, get_attribute
, char*,
107 private_xml_t
*this, char *name
)
112 METHOD(enumerator_t
, child_destroy
, void,
115 if (--this->child
.root
->enums
== 0)
117 xmlFreeDoc(this->child
.root
->doc
);
118 free(this->child
.root
);
123 METHOD(xml_t
, children
, enumerator_t
*,
129 .enumerate
= enumerator_enumerate_default
,
130 .venumerate
= _child_enumerate
,
131 .destroy
= _child_destroy
,
135 .get_attribute
= _get_attribute
,
136 .children
= _children
,
150 xml_t
*xml_create(char *xml
)
156 .get_attribute
= _get_attribute
,
157 .children
= _children
,
159 .doc
= xmlReadMemory(xml
, strlen(xml
), NULL
, NULL
, 0),
168 this->node
= xmlDocGetRootElement(this->doc
);
171 return &this->public;