

We are going to parse an X\+ML 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_gafee4bfc0e509b6ffed6598edf6f4b3de}{eina\_init}}();}

\end{DoxyCodeInclude}


We fill buffer with the X\+ML 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_ga4a389204db37b44b31d29f581258ac7d}{eina\_array\_new}}(10);}

\end{DoxyCodeInclude}


Here we call \mbox{\hyperlink{group__Eina__Simple__XML__Group_gacbbb2b975f6f3e1d0a3d2215cc37da82}{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_gacbbb2b975f6f3e1d0a3d2215cc37da82}{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 X\+ML 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_gaa356517d0c834904432275d7edd2a892}{eina\+\_\+simple\+\_\+xml\+\_\+tag\+\_\+attributes\+\_\+find()}} and give them to another parsing function using \mbox{\hyperlink{group__Eina__Simple__XML__Group_ga9525b33f8d78eb23def4f660b290f794}{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_gaa356517d0c834904432275d7edd2a892}{eina\_simple\_xml\_tag\_attributes\_find}}(content,}
\DoxyCodeLine{                                                                    length);}
\DoxyCodeLine{             \mbox{\hyperlink{group__Eina__Simple__XML__Group_ga9525b33f8d78eb23def4f660b290f794}{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_ga6bda31d2e1b6d2737b7fe1784780cd2f}{eina\_strlcpy}}(buffer, content, \textcolor{keyword}{sizeof}(buffer));}
\DoxyCodeLine{             \mbox{\hyperlink{group__Eina__String__Group_ga6c9583940f9f6b3c886a19f22f398ff2}{eina\_strlcat}}(str, \textcolor{stringliteral}{"<"}, 1);}
\DoxyCodeLine{             \mbox{\hyperlink{group__Eina__String__Group_ga6c9583940f9f6b3c886a19f22f398ff2}{eina\_strlcat}}(str, buffer, \textcolor{keyword}{sizeof}(str));}
\DoxyCodeLine{             \mbox{\hyperlink{group__Eina__String__Group_ga6c9583940f9f6b3c886a19f22f398ff2}{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 $<$post$>$ 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_ga6bda31d2e1b6d2737b7fe1784780cd2f}{eina\_strlcpy}}(buffer, content, \textcolor{keyword}{sizeof}(buffer));}
\DoxyCodeLine{             \mbox{\hyperlink{group__Eina__String__Group_ga6c9583940f9f6b3c886a19f22f398ff2}{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_gaf2f4a5907f1826354977adbd63e3379a}{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}}. 