Discussion:Introduction à la programmation en C des arbres Xml
De Wicri Manuel
Sommaire
Solution des exercices
Méthodes de construction de base
Premier exercice
cat <<... >TPxml.c
/* création de <a>a1<b>b1</b><c>c1</c></a> */
#include "SxmlNode.h"
main() {
SxmlNode *root;
root =SxmlLeafCreate("a", "a1");
SxmlAppendChild (root, SxmlLeafCreate("b", "b1"));
SxmlAppendChild (root, SxmlLeafCreate("c", "c1"));
SxmlPrint(root);
putchar('\n');
exit(0);
}
...
gcc TPxml.c $DILIB_CC -o TPxml
./TPxml | SxmlIndent
Deuxième exercice
cat <<...>TPtintin.c
#include "SxmlNode.h"
main() {
SxmlNode *root;
root =SxmlElementCreate("doc");
SxmlAppendChild (root, SxmlLeafCreate("tit", "Tintin au Congo"));
SxmlNode *auteurs;
auteurs =SxmlAppendChild (root, SxmlElementCreate("auteurs"));
SxmlAppendChild (auteurs, SxmlLeafCreate("auteur", "Hergé"));
SxmlNode *lkw;
lkw =SxmlAppendChild (root, SxmlElementCreate("lkw"));
SxmlAppendChild (lkw, SxmlLeafCreate("kw", "Tintin"));
SxmlAppendChild (lkw, SxmlLeafCreate("kw", "Milou"));
SxmlPrint(root);
putchar('\n');
exit(0);
}
...
gcc TPtintin.c $DILIB_CC -o TPtintin
./TPtintin | SxmlIndent
Troisième exercice
cat <<...>TPattribut.c
#include "SxmlNode.h"
main() {
SxmlNode *root;
root =SxmlElementCreate("pubmed");
SxmlNode *medline;
medline=SxmlElementCreate("MedlineCitation");
SxmlAppendChild (root, medline);
SxmlSetAttribute (medline, "Status", "MEDLINE");
SxmlSetAttribute (medline, "Owner", "NLM");
SxmlNode *pmid;
pmid=SxmlLeafCreate("PMID", "7843359");
SxmlAppendChild (medline, pmid);
SxmlSetAttribute (pmid, "Version", "1");
SxmlNode *date;
date=SxmlElementCreate("DateCompleted");
SxmlAppendChild (medline, date);
SxmlAppendChild (date, SxmlLeafCreate("Year", "1995"));
SxmlAppendChild (date, SxmlLeafCreate("Month", "03"));
SxmlAppendChild (date, SxmlLeafCreate("Day", "08"));
SxmlNode *author;
author=SxmlElementCreate("Author");
SxmlAppendChild (medline, author);
SxmlSetAttribute (author, "ValideYN", "Y");
SxmlAppendChild (author, SxmlLeafCreate("LastName", "Flahault"));
SxmlAppendChild (author, SxmlLeafCreate("ForeName", "A"));
SxmlAppendChild (author, SxmlLeafCreate("Initials", "A"));
SxmlNode *affiliation;
affiliation=SxmlElementCreate("AffiliationInfo");
SxmlAppendChild (author, affiliation);
SxmlAppendChild (affiliation, SxmlLeafCreate("Affiliation", "Faculté de Médecine Saint-Antoine, Paris, France."));
SxmlPrint(root);
putchar('\n');
exit(0);
}
...
gcc TPattribut.c $DILIB_CC -o TPattribut
./TPattribut | SxmlIndent