IF YOU WOULD LIKE TO GET AN ACCOUNT, please write an
email to Administrator. User accounts are meant only to access repo
and report issues and/or generate pull requests.
This is a purpose-specific Git hosting for
BaseALT
projects. Thank you for your understanding!
Только зарегистрированные пользователи имеют доступ к сервису!
Для получения аккаунта, обратитесь к администратору.
<coid="getrootelement"/> cur = xmlDocGetRootElement(doc);
<coid="checkemptyerror"/> if (cur == NULL) {
fprintf(stderr,"empty document\n");
xmlFreeDoc(doc);
return;
}
<coid="checkroottype"/> if (xmlStrcmp(cur->name, (const xmlChar *) "story")) {
fprintf(stderr,"document of the wrong type, root node != story");
xmlFreeDoc(doc);
return;
}
</programlisting>
<calloutlist>
<calloutarearefs="declaredoc">
<para>Declare the pointer that will point to your parsed document.</para>
</callout>
<calloutarearefs="declarenode">
<para>Declare a node pointer (you'll need this in order to
interact with individual nodes).</para>
</callout>
<calloutarearefs="checkparseerror">
<para>Check to see that the document was successfully parsed.</para>
</callout>
<calloutarearefs="getrootelement">
<para>Retrieve the document's root element.</para>
</callout>
<calloutarearefs="checkemptyerror">
<para>Check to make sure the document actually contains something.</para>
</callout>
<calloutarearefs="checkroottype">
<para>In our case, we need to make sure the document is the right
type. "story" is the root type of my documents.</para>
</callout>
</calloutlist>
</para>
</sect1>
<sect1id="xmltutorialgettext">
<title>Retrieving Element Content</title>
<para>Retrieving the content of an element involves traversing the document
tree until you find what you are looking for. In this case, we are looking
for an element called "keyword" contained within element called "story". The
process to find the node we are interested in involves tediously walking the
tree. We assume you already have an xmlDocPtr called <varname>doc</varname>
and an xmlNodPtr called <varname>cur</varname>.</para>
<para>
<programlisting>
<coid="getchildnode"/> cur = cur->xmlChildrenNode;
<coid="huntstoryinfo"/> while (cur != NULL) {
if ((!xmlStrcmp(cur->name, (const xmlChar *)"storyinfo"))){
parseStory (doc, cur);
}
cur = cur->next;
}
</programlisting>
<calloutlist>
<calloutarearefs="getchildnode">
<para>Get the first child node of <varname>cur</varname>. At this
point, <varname>cur</varname> points at the document root, which is
the element "story".</para>
</callout>
<calloutarearefs="huntstoryinfo">
<para>This loop iterates through the elements that are children of
"story", looking for one called "storyinfo". That
is the element that will contain the "keywords" we are
looking for. It uses the <application>libxml</application> string
comparison
function, <function><ulink
url="http://xmlsoft.org/html/libxml-parser.html#XMLSTRCMP">xmlStrcmp</ulink></function>. If there is a match, it calls the function <function>parseStory</function>.</para>
</callout>
</calloutlist>
</para>
<para>
<programlisting>
void
parseStory (xmlDocPtr doc, xmlNodePtr cur) {
<coid="anothergetchild"/> cur = cur->xmlChildrenNode;
<coid="findkeyword"/> while (cur != NULL) {
if ((!xmlStrcmp(cur->name, (const xmlChar *)"keyword"))) {
<para>Like the loop above, we then iterate through the nodes, looking
for one that matches the element we're interested in, in this case
"keyword".</para>
</callout>
<calloutarearefs="foundkeyword">
<para>When we find the "keyword" element, we need to print
its contents. Remember that in <acronym>XML</acronym>, the text
contained within an element is a child node of that element, so we
turn to <varname>cur->xmlChildrenNode</varname>. To retrieve it, we
use the function <function><ulink
url="http://xmlsoft.org/html/libxml-tree.html#XMLNODELISTGETSTRING">xmlNodeListGetString</ulink></function>, which also takes the <varname>doc</varname> pointer as an argument. In this case, we just print it out.</para>
</callout>
</calloutlist>
</para>
</sect1>
<sect1id="xmltutorialwritingcontent">
<title>Writing element content</title>
<para>Writing element content uses many of the same steps we used above
— parsing the document and walking the tree. We parse the document,
then traverse the tree to find the place we want to insert our element. For
this example, we want to again find the "storyinfo" element and
this time insert a keyword. Then we'll write the file to disk. Full code: