

We are going to parse an XML sample file and print the data to stdout.

Like all examples we start by including Eina\+: 
\begin{DoxyCodeInclude}{0}
\DoxyCodeLine{\textcolor{preprocessor}{\#include\ <\mbox{\hyperlink{Eina_8h}{Eina.h}}>}}

\end{DoxyCodeInclude}
 We declare 2 booleans to keep track of tags\+: 
\begin{DoxyCodeInclude}{0}
\DoxyCodeLine{\mbox{\hyperlink{group__Eina__Types__Group_ga3fe0caf72e93b1bab1ca8ee3ccf3f226}{Eina\_Bool}}\ tag\_login\ \ \ =\ \mbox{\hyperlink{group__Eina__Types__Group_ga1feb115f8e9913e806e090d9bd5a7301}{EINA\_FALSE}};}
\DoxyCodeLine{\mbox{\hyperlink{group__Eina__Types__Group_ga3fe0caf72e93b1bab1ca8ee3ccf3f226}{Eina\_Bool}}\ tag\_message\ =\ \mbox{\hyperlink{group__Eina__Types__Group_ga1feb115f8e9913e806e090d9bd5a7301}{EINA\_FALSE}};}

\end{DoxyCodeInclude}
 Here we declare some variables and initialize eina\+: 
\begin{DoxyCodeInclude}{0}
\DoxyCodeLine{}
\DoxyCodeLine{\textcolor{keywordtype}{int}}
\DoxyCodeLine{main(\textcolor{keywordtype}{void})}
\DoxyCodeLine{\{}
\DoxyCodeLine{\ \ \ FILE\ *file;}
\DoxyCodeLine{\ \ \ \textcolor{keywordtype}{long}\ size;}
\DoxyCodeLine{\ \ \ \textcolor{keywordtype}{char}\ *buffer;}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{struct__Eina__Array}{Eina\_Array}}\ *array;}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{group__Eina__Main__Group_ga81b8137665b8a4d2cb38ca50017d4c74}{eina\_init}}();}

\end{DoxyCodeInclude}
 We fill buffer with the XML data from chat.\+xml\+: 
\begin{DoxyCodeInclude}{0}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \textcolor{keywordflow}{if}\ ((file\ =\ fopen(\textcolor{stringliteral}{"{}chat.xml"{}},\ \textcolor{stringliteral}{"{}rb"{}})))}
\DoxyCodeLine{\ \ \ \ \ \{}
\DoxyCodeLine{\ \ \ \ \ \ \ \ fseek(file,\ 0,\ SEEK\_END);}
\DoxyCodeLine{\ \ \ \ \ \ \ \ size\ =\ ftell(file);}
\DoxyCodeLine{\ \ \ \ \ \ \ \ fseek(file,\ 0,\ SEEK\_SET);}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ (size\ <\ 0)}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \{}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \mbox{\hyperlink{group__Eina__Log__Group_ga2e33caeeeb6051526c01b7fea4d6c91b}{EINA\_LOG\_ERR}}(\textcolor{stringliteral}{"{}Can't\ read\ chat.xml"{}});}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{return}\ 0;}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ ((buffer\ =\ malloc(size)))}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \{}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ (fread(buffer,\ 1,\ size,\ file)\ !=\ (\textcolor{keywordtype}{unsigned}\ \textcolor{keywordtype}{long})size)}

\end{DoxyCodeInclude}
 We will use an Eina\+\_\+\+Array to store the data\+: 
\begin{DoxyCodeInclude}{0}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ array\ =\ \mbox{\hyperlink{group__Eina__Array__Group_gaf4b5844225b5359a617744210a5ed6ff}{eina\_array\_new}}(10);}

\end{DoxyCodeInclude}
 Here we call \mbox{\hyperlink{group__Eina__Simple__XML__Group_ga2a0393367425ea29ef27d27796567380}{eina\+\_\+simple\+\_\+xml\+\_\+parse()}}. We pass the buffer with data, its size, we ask to strip leading and trailing whitespace, we give the callback function and the array to store the formatted data\+: 
\begin{DoxyCodeInclude}{0}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \mbox{\hyperlink{group__Eina__Simple__XML__Group_ga2a0393367425ea29ef27d27796567380}{eina\_simple\_xml\_parse}}(buffer,\ size,\ \mbox{\hyperlink{group__Eina__Types__Group_ga05c12dacc8b4058994df842b41be85fc}{EINA\_TRUE}},}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \_xml\_tag\_cb,\ array);}

\end{DoxyCodeInclude}
 This will loop over the array and print the data using \+\_\+print callback\+: 
\begin{DoxyCodeInclude}{0}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \mbox{\hyperlink{group__Eina__Array__Group_ga8de2d191a0fffceb5bdc6376540b8db1}{eina\_array\_foreach}}(array,\ \_print,\ NULL);}

\end{DoxyCodeInclude}
 This is the main XML parser callback, it will check for known tags and get the corresponding values\+: 
\begin{DoxyCodeInclude}{0}
\DoxyCodeLine{\textcolor{keyword}{static}\ \mbox{\hyperlink{group__Eina__Types__Group_ga3fe0caf72e93b1bab1ca8ee3ccf3f226}{Eina\_Bool}}}
\DoxyCodeLine{\_xml\_tag\_cb(\textcolor{keywordtype}{void}\ *data,\ Eina\_Simple\_XML\_Type\ type,\ \textcolor{keyword}{const}\ \textcolor{keywordtype}{char}\ *content,}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keywordtype}{unsigned}\ offset\ \mbox{\hyperlink{group__Eina__Types__Group_ga82d751be730f849719ca292f9558be87}{EINA\_UNUSED}},\ \textcolor{keywordtype}{unsigned}\ length)}
\DoxyCodeLine{\{}
\DoxyCodeLine{\ \ \ \textcolor{keywordtype}{char}\ buffer[length+1];}
\DoxyCodeLine{\ \ \ \mbox{\hyperlink{struct__Eina__Array}{Eina\_Array}}\ *array\ =\ data;}
\DoxyCodeLine{\ \ \ \textcolor{keywordtype}{char}\ str[512]\ =\ \{\textcolor{charliteral}{'\(\backslash\)0'}\};}

\end{DoxyCodeInclude}
 We first check for opening tag\+: 
\begin{DoxyCodeInclude}{0}
\DoxyCodeLine{\ \ \ \textcolor{keywordflow}{if}\ (type\ ==\ \mbox{\hyperlink{group__Eina__Simple__XML__Group_ggad43c24562b801f797d8f7cb8b3fe2d4ca5be26ebfdfdefbc411adde618c4c2dd4}{EINA\_SIMPLE\_XML\_OPEN}})}

\end{DoxyCodeInclude}
 If we know the tag should have attributes, then we find them using \mbox{\hyperlink{group__Eina__Simple__XML__Group_ga3af39eab6fc32f7ccd9278d2c7b7314a}{eina\+\_\+simple\+\_\+xml\+\_\+tag\+\_\+attributes\+\_\+find()}} and give them to another parsing function using \mbox{\hyperlink{group__Eina__Simple__XML__Group_ga59ca63d8488d4af119983d6024b6da2b}{eina\+\_\+simple\+\_\+xml\+\_\+attributes\+\_\+parse()}}\+: 
\begin{DoxyCodeInclude}{0}
\DoxyCodeLine{\ \ \ \ \ \{}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}(!strncmp(\textcolor{stringliteral}{"{}post"{}},\ content,\ strlen(\textcolor{stringliteral}{"{}post"{}})))}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \{}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \textcolor{keyword}{const}\ \textcolor{keywordtype}{char}\ *tags\ =\ \mbox{\hyperlink{group__Eina__Simple__XML__Group_ga3af39eab6fc32f7ccd9278d2c7b7314a}{eina\_simple\_xml\_tag\_attributes\_find}}(content,}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ length);}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \mbox{\hyperlink{group__Eina__Simple__XML__Group_ga59ca63d8488d4af119983d6024b6da2b}{eina\_simple\_xml\_attributes\_parse}}(tags,\ length\ -\/\ (tags\ -\/\ content),}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \_xml\_attr\_cb,\ str);}

\end{DoxyCodeInclude}
 We check for other known tags\+: 
\begin{DoxyCodeInclude}{0}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \textcolor{keywordflow}{else}\ \textcolor{keywordflow}{if}\ (!strncmp(\textcolor{stringliteral}{"{}login>"{}},\ content,\ strlen(\textcolor{stringliteral}{"{}login>"{}})))}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \{}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ tag\_login\ =\ \mbox{\hyperlink{group__Eina__Types__Group_ga05c12dacc8b4058994df842b41be85fc}{EINA\_TRUE}};}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \textcolor{keywordflow}{else}\ \textcolor{keywordflow}{if}\ (!strncmp(\textcolor{stringliteral}{"{}message>"{}},\ content,\ strlen(\textcolor{stringliteral}{"{}message>"{}})))}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \{}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ tag\_message\ =\ \mbox{\hyperlink{group__Eina__Types__Group_ga05c12dacc8b4058994df842b41be85fc}{EINA\_TRUE}};}

\end{DoxyCodeInclude}
 We then check data for corresponding tag\+: 
\begin{DoxyCodeInclude}{0}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{\ \ \ \ \ \}}
\DoxyCodeLine{\ \ \ \textcolor{keywordflow}{else}\ \textcolor{keywordflow}{if}\ (type\ ==\ \mbox{\hyperlink{group__Eina__Simple__XML__Group_ggad43c24562b801f797d8f7cb8b3fe2d4caea1f4a70cdfcfba5c19ce1abe363c688}{EINA\_SIMPLE\_XML\_DATA}})}
\DoxyCodeLine{\ \ \ \ \ \{}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ (tag\_login\ ==\ \mbox{\hyperlink{group__Eina__Types__Group_ga05c12dacc8b4058994df842b41be85fc}{EINA\_TRUE}})}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \{}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \mbox{\hyperlink{group__Eina__String__Group_gae28caa0c42c4e76ae584f22bd3615723}{eina\_strlcpy}}(buffer,\ content,\ \textcolor{keyword}{sizeof}(buffer));}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \mbox{\hyperlink{group__Eina__String__Group_ga9d340cfcfd1259f3ef76d314a974b0e0}{eina\_strlcat}}(str,\ \textcolor{stringliteral}{"{}<"{}},\ 1);}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \mbox{\hyperlink{group__Eina__String__Group_ga9d340cfcfd1259f3ef76d314a974b0e0}{eina\_strlcat}}(str,\ buffer,\ \textcolor{keyword}{sizeof}(str));}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \mbox{\hyperlink{group__Eina__String__Group_ga9d340cfcfd1259f3ef76d314a974b0e0}{eina\_strlcat}}(str,\ \textcolor{stringliteral}{"{}>\ "{}},\ 2);}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ tag\_login\ =\ \mbox{\hyperlink{group__Eina__Types__Group_ga1feb115f8e9913e806e090d9bd5a7301}{EINA\_FALSE}};}

\end{DoxyCodeInclude}
 We are doing the formatting in same time and put all the \texorpdfstring{$<$}{<}post\texorpdfstring{$>$}{>} children in str. 
\begin{DoxyCodeInclude}{0}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \textcolor{keywordflow}{else}\ \textcolor{keywordflow}{if}\ (tag\_message\ ==\ \mbox{\hyperlink{group__Eina__Types__Group_ga05c12dacc8b4058994df842b41be85fc}{EINA\_TRUE}})}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \{}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \mbox{\hyperlink{group__Eina__String__Group_gae28caa0c42c4e76ae584f22bd3615723}{eina\_strlcpy}}(buffer,\ content,\ \textcolor{keyword}{sizeof}(buffer));}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \mbox{\hyperlink{group__Eina__String__Group_ga9d340cfcfd1259f3ef76d314a974b0e0}{eina\_strlcat}}(str,\ buffer,\ \textcolor{keyword}{sizeof}(str));}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ tag\_message\ =\ \mbox{\hyperlink{group__Eina__Types__Group_ga1feb115f8e9913e806e090d9bd5a7301}{EINA\_FALSE}};}

\end{DoxyCodeInclude}
 Finally, we store our string in the array\+: 
\begin{DoxyCodeInclude}{0}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \mbox{\hyperlink{group__Eina__Array__Group_ga97ac1fe353f4cd7a9762af4d4db7b9c4}{eina\_array\_push}}(array,\ strdup(str));}

\end{DoxyCodeInclude}
 This is the callback to parse the attributes, we check for key name and keep the value\+: 
\begin{DoxyCodeInclude}{0}
\DoxyCodeLine{\textcolor{keyword}{static}\ \mbox{\hyperlink{group__Eina__Types__Group_ga3fe0caf72e93b1bab1ca8ee3ccf3f226}{Eina\_Bool}}}
\DoxyCodeLine{\_xml\_attr\_cb(\textcolor{keywordtype}{void}\ *data,\ \textcolor{keyword}{const}\ \textcolor{keywordtype}{char}\ *key,\ \textcolor{keyword}{const}\ \textcolor{keywordtype}{char}\ *value)}
\DoxyCodeLine{\{}
\DoxyCodeLine{\ \ \ \textcolor{keywordtype}{char}\ *str\ =\ data;}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \textcolor{keywordflow}{if}(!strcmp(\textcolor{stringliteral}{"{}id"{}},\ key))}
\DoxyCodeLine{\ \ \ \{}
\DoxyCodeLine{\ \ \ \ \ \ snprintf(str,\ \textcolor{keyword}{sizeof}(value)\ +\ 3,\ \textcolor{stringliteral}{"{}(\%s)\ "{}},\ value);}

\end{DoxyCodeInclude}
 This is the function that simply print items of the array\+: 
\begin{DoxyCodeInclude}{0}
\DoxyCodeLine{\ \ \ \}}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \textcolor{keywordflow}{return}\ \mbox{\hyperlink{group__Eina__Types__Group_ga05c12dacc8b4058994df842b41be85fc}{EINA\_TRUE}};}

\end{DoxyCodeInclude}
 You can see the full source code \mbox{\hyperlink{eina_simple_xml_parser_example_01}{here}}. 