XML/Output: Difference between revisions
Content added Content deleted
(→{{header|jq}}: reduce) |
|||
Line 954: | Line 954: | ||
=={{header|C}}== |
=={{header|C}}== |
||
{{libheader|libXML}} |
==={{libheader|libXML}}=== |
||
<syntaxhighlight lang="c">#include <stdio.h> |
<syntaxhighlight lang="c">#include <stdio.h> |
||
#include <stdlib.h> |
#include <stdlib.h> |
||
Line 995: | Line 995: | ||
return EXIT_SUCCESS; |
return EXIT_SUCCESS; |
||
}</syntaxhighlight> |
}</syntaxhighlight> |
||
==={{libheader|Gadget}}=== |
|||
<syntaxhighlight lang="c"> |
|||
#include <gadget/gadget.h> |
|||
LIB_GADGET_START |
|||
const char* codes[] = {"&",">","<"}; |
|||
const char* repl[] = {"&",">","<"}; |
|||
char* change_codes( const char* xml_line ) |
|||
{ |
|||
int i; |
|||
String xml; |
|||
Let( xml, xml_line ); |
|||
Iterator up i [0:1:3]{ |
|||
Get_fn_let( xml, Str_tran( xml, repl[i], codes[i] ) ); |
|||
} |
|||
return xml; |
|||
} |
|||
char* generate_xml( const char* names[], int lnames, const char* remarks[] ) |
|||
{ |
|||
String xml; |
|||
char attrib[100]; |
|||
int i; |
|||
Iterator up i [0:1:lnames]{ |
|||
String remk; |
|||
Get_fn_let ( remk, change_codes( remarks[i] ) ); |
|||
sprintf(attrib,"name=\"%s\"",names[i]); |
|||
if(!i) { |
|||
Get_fn_let ( xml, Parser("Character", attrib, remk, NORMAL_TAG )); |
|||
}else{ |
|||
Get_fn_cat ( xml, Parser("Character", attrib, remk, NORMAL_TAG )); |
|||
} |
|||
Free secure remk; |
|||
} |
|||
Get_fn_let ( xml, Parser("CharacterRemarks", "", xml, NORMAL_TAG )); |
|||
Get_fn_let ( xml, Str_tran(xml,"><",">\n<") ); |
|||
return xml; |
|||
} |
|||
#define alen(_X_) ( sizeof(_X_) / sizeof(const char*) ) |
|||
Main |
|||
const char *names[] = { |
|||
"April", "Tam O'Shanter", "Emily" |
|||
}; |
|||
const char *remarks[] = { |
|||
"Bubbly: I'm > Tam and <= Emily", |
|||
"Burns: \"When chapman billies leave the street ...\"", |
|||
"Short & shrift" |
|||
}; |
|||
char* xml = generate_xml( names, alen(names), remarks ); |
|||
Print "%s\n", xml; |
|||
Free secure xml; |
|||
End |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
$ ./tests/RC_xmlout |
|||
<CharacterRemarks> |
|||
<Character name="April">Bubbly: I'm > Tam and <= Emily</Character> |
|||
<Character name="Tam O'Shanter">Burns: "When chapman billies leave the street ..."</Character> |
|||
<Character name="Emily">Short & shrift</Character> |
|||
</CharacterRemarks> |
|||
</pre> |
|||
=={{header|C sharp}}== |
=={{header|C sharp}}== |