Skip to content

Commit 6a525ce

Browse files
author
Velli20
committed
Added function to find element and imp
roved tests
1 parent 9a9c7e3 commit 6a525ce

File tree

5 files changed

+388
-167
lines changed

5 files changed

+388
-167
lines changed

README.md

Lines changed: 62 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,95 +1,100 @@
11
# XML-parser
2-
Work in progress. Lightweight xml-parser with minimal system requirements.
2+
3+
Work in progress. Lightweight xml-parser for microcontrollers.
34

45

56
# Usage
67

7-
Example program:
8+
Example of parsing string:
89

910
```c
10-
static const PARSER_XML_NAME elements[]=
11-
{
12-
// Predefined element and attribute names.
13-
14-
{ "ELEM_type_1" },
15-
{ "ELEM_type_2" },
16-
{ "ELEM_type_3" },
11+
// List of element and attribute names in test string.
1712

18-
{ "testElementId" },
19-
{ "intAttribute" },
20-
{ "floatAttribute" },
21-
{ "stringAttribute" },
13+
static const PARSER_XML_NAME xml_names[]=
14+
{
15+
{ "element_type_1" },
16+
{ "element_type_2" },
17+
{ "element_type_3" },
18+
{ "element_type_4" },
19+
{ "element_type_5" },
20+
{ "element_type_6" },
21+
{ "element_type_7" },
22+
23+
{ "testElementId" },
24+
{ "intAttribute" },
25+
{ "floatAttribute" },
26+
{ "stringAttribute" },
2227
};
2328

24-
// Test string to parse
29+
// String to parse.
2530

26-
static const char test_1[]=
31+
static const char parse_test_string[]=
2732
{
28-
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
29-
"\n"
30-
"<ELEM_type_1 testElementId='0' intAttribute='20' floatAttribute='1.23' stringAttribute=\"TEST\">\n"
31-
"\n"
32-
" <!-- Single line comment -->\n"
33-
" <ELEM_type_2 testElementId='1'>\n"
34-
" <ELEM_type_3 testElementId='11'/>\n"
35-
" <ELEM_type_3 testElementId='12'/>\n"
36-
" <ELEM_type_3 testElementId='13'/>\n"
37-
"\n"
38-
" <!-- Single line comment -->\n"
39-
" <ELEM_type_3 testElementId='14'>\n"
40-
" <!-- Multiline\n"
41-
" comment -->\n"
42-
" </ELEM_type_3>\n"
43-
" </ELEM_type_2>\n"
44-
"\n"
45-
" <ELEM_type_2 testElementId='2'>Child element with string content</ELEM_type_2>\n"
46-
"</ELEM_type_1>\n"
47-
""
33+
"<element_type_1 testElementId=\"0\" intAttribute=\"20\" floatAttribute=\"1.230000\" stringAttribute=\"TEST\">\n"
34+
" <element_type_2 testElementId=\"1\">\n"
35+
" <element_type_3 testElementId=\"11\"/>\n"
36+
" <element_type_3 testElementId=\"12\"/>\n"
37+
" <element_type_3 testElementId=\"13\"/>\n"
38+
" <element_type_3 testElementId=\"14\"/>\n"
39+
" </element_type_2>\n"
40+
" <element_type_4>\n"
41+
" <element_type_5>\n"
42+
" <element_type_6 testElementId=\"15\"/>\n"
43+
" <element_type_6 testElementId=\"17\"/>\n"
44+
" </element_type_5>\n"
45+
" </element_type_4>\n"
46+
" <element_type_7>\n"
47+
" <element_type_8 testElementId=\"18\"/>\n"
48+
" </element_type_7>\n"
49+
"</element_type_1>\n"
4850
};
4951

50-
int main(void)
52+
static PARSER_ERROR test_parse(void)
5153
{
5254
PARSER_XML* xml;
53-
PARSER_CHAR out[1024];
54-
PARSER_INT bw;
55+
PARSER_CHAR buffer_out[1024];
56+
PARSER_INT buffer_lenght;
57+
PARSER_INT bytes_written;
5558
PARSER_ERROR error;
5659

5760
// Initialize xml-struct.
5861

5962
xml= parser_begin();
6063
if ( !xml )
61-
{
62-
printf("%s %d: XML-struct initialization failed.\n", __FUNCTION__, __LINE__);
6364
return(1);
64-
}
6565

66-
// Parse xml-string.
66+
// Parse string.
6767

68-
error= parser_parse_string(xml, test_1, elements, sizeof(elements)/sizeof(PARSER_XML_NAME));
68+
error= parser_parse_string(xml, parse_test_string, strlen(parse_test_string), xml_names, sizeof(xml_names)/sizeof(PARSER_XML_NAME));
6969
if ( error )
70-
{
71-
printf("%d %s: Error parsing xml-string.", __FUNCTION__, __LINE__);
72-
goto end;
73-
}
70+
return(error);
7471

7572
// End parsing.
7673

7774
error= parser_finalize(xml);
78-
if ( !error )
79-
{
80-
// Write parsed xml back to string.
75+
if ( error )
76+
return(error);
8177

82-
error= parser_write_xml_to_buffer(xml, elements, out, sizeof(out), &bw, 0);
83-
if ( !error )
84-
printf("Bytes written:%d Buffer content:\n%s", bw, out);
85-
}
78+
// Write parsed xml back to string.
79+
80+
error= parser_write_xml_to_buffer(xml, xml_names, buffer_out, sizeof(buffer_out), &bytes_written, 0);
81+
if ( error )
82+
return(error);
8683

87-
end:
84+
// Check that output buffer matches original string.
85+
86+
if ( strncmp(buffer_out, parse_test_string, sizeof(buffer_out)) )
87+
{
88+
printf("%s %d: Parse result error: input and output string does not match.\n", __FUNCTION__, __LINE__);
89+
printf("Original string:\n%s\n\n", parse_test_string);
90+
printf("Output buffer string:\n%s\n\n", buffer_out);
91+
}
8892

8993
// Free xml.
9094

91-
if ( xml )
92-
parser_free_xml(xml);
95+
error= parser_free_xml(xml);
96+
if ( error )
97+
return(error);
9398

9499
return(0);
95100
}

inc/xml_parser.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ PARSER_XML;
201201
#define PARSER_ASSERT(CONDITION) \
202202
if ( !(CONDITION) ) \
203203
{ \
204-
printf("Parser error %s %d: %s", \
204+
printf("Parser assert %s %d: %s", \
205205
__FUNCTION__, __LINE__, #CONDITION); \
206206
return(PARSER_RESULT_ERROR); \
207207
}
@@ -235,4 +235,13 @@ PARSER_ERROR parser_write_xml_to_buffer(const PARSER_XML* xml,
235235
PARSER_INT* bytes_written,
236236
PARSER_INT flags);
237237

238+
// parser_find_element
239+
240+
const PARSER_ELEMENT* parser_find_element(const PARSER_XML* xml,
241+
const PARSER_XML_NAME* xml_name_list,
242+
const PARSER_ELEMENT* offset,
243+
PARSER_INT max_depth,
244+
PARSER_INT xml_name_list_length,
245+
const PARSER_CHAR* element_name);
246+
238247
#endif

0 commit comments

Comments
 (0)