A lightweight XML parser for use with Gunslinger.
At the moment, this library is capable of parsing the following document:
<?xml version="1.0" encoding="UTF-8"?>
<test some_attrib="I'm an attribute!" some_entities="' & " < >" some_number1 = "384.7" some_bool="true">
<test_child>Hello, world!</test_child>
<other_test_child some_bool="false">
<other_test_child_child>Hello there! ' & " < ></other_test_child_child>
</other_test_child>
<!-- Hello, I'm a comment. -->
<some_thing thing="Haha yes" thing1 = "33"/>
<!-- I'm another comment. -->
</test>
It supports the following entities:
Character | Entity |
---|---|
& | & |
' | ' |
" | " |
< | < |
> | > |
Before use, make sure you have Gunslinger set up, including adding gs.h
, from the
Gunslinger repository to your include path.
Then, #define GS_XML_IMPL
in one C or C++ source file before you #include gs_xml.h
.
// To parse from memory: gs_xml_parse(const char* source)
gs_xml_document_t* doc = gs_xml_parse_file("./test.xml");
if (!doc)
{
printf("XML Parse Error: %s\n", gs_xml_get_error());
return;
}
gs_xml_free(doc);
A more complete example can be found in example.
// To find an element that isn't a child
gs_xml_node_t* node = gs_xml_find_node(doc, "test_node");
// To find a child
gs_xml_node_t* child = gs_xml_find_node_child(node, "test_child");
// To find an attribute
gs_xml_attribute_t* attrib = gs_xml_find_attribute(node, "test_attrib");
printf("%s\n", node->name);
printf("%s\n", attrib->name);
printf("%g\n", number_attrib->value.number);
printf("%s\n", string_attrib->value.string);
printf("%s\n", boolean_attrib->value.boolean ? "true" : "false");