mirror of
https://gitlab.gnome.org/GNOME/libxml2.git
synced 2025-07-30 22:43:14 +03:00
doc/tutorial/xmltutorial.xml doc/tutorial/ar01s07.html
Wed Jun 12 21:26:08 MDT 2002 John Fleck <jfleck@inkstain.net> * doc/tutorial/xmltutorial.xml * doc/tutorial/ar01s07.html * doc/tutorial/ape.html * doc/tutorial/includegetattribute.c adding section to tutorial about retrieving an attribute value
This commit is contained in:
committed by
John Fleck
parent
f5582f156c
commit
54520837ad
@ -1,3 +1,12 @@
|
|||||||
|
Wed Jun 12 21:26:08 MDT 2002 John Fleck <jfleck@inkstain.net>
|
||||||
|
|
||||||
|
* doc/tutorial/xmltutorial.xml
|
||||||
|
* doc/tutorial/ar01s07.html
|
||||||
|
* doc/tutorial/ape.html
|
||||||
|
* doc/tutorial/includegetattribute.c
|
||||||
|
adding section to tutorial about retrieving an attribute
|
||||||
|
value
|
||||||
|
|
||||||
Tue Jun 11 12:07:04 CEST 2002 Daniel Veillard <daniel@veillard.com>
|
Tue Jun 11 12:07:04 CEST 2002 Daniel Veillard <daniel@veillard.com>
|
||||||
|
|
||||||
* parser.c: applied a couple of patches from Peter Jacobi to start
|
* parser.c: applied a couple of patches from Peter Jacobi to start
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
|
||||||
<html><head><meta content="text/html; charset=ISO-8859-1" http-equiv="Content-Type"><title>A. Sample Document</title><meta name="generator" content="DocBook XSL Stylesheets V1.48"><link rel="home" href="index.html" title="Libxml Tutorial"><link rel="up" href="index.html" title="Libxml Tutorial"><link rel="previous" href="ar01s06.html" title="Writing Attribute"><link rel="next" href="apb.html" title="B. Code for Keyword Example"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">A. Sample Document</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="ar01s06.html">Prev</a><EFBFBD></td><th width="60%" align="center"><EFBFBD></th><td width="20%" align="right"><EFBFBD><a accesskey="n" href="apb.html">Next</a></td></tr></table><hr></div><div class="appendix"><h2 class="title" style="clear: both"><a name="sampledoc"></a>A. Sample Document</h2><pre class="programlisting">
|
<html><head><meta content="text/html; charset=ISO-8859-1" http-equiv="Content-Type"><title>A. Sample Document</title><meta name="generator" content="DocBook XSL Stylesheets V1.48"><link rel="home" href="index.html" title="Libxml Tutorial"><link rel="up" href="index.html" title="Libxml Tutorial"><link rel="previous" href="ar01s07.html" title="Retrieving Attributes"><link rel="next" href="apb.html" title="B. Code for Keyword Example"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">A. Sample Document</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="ar01s07.html">Prev</a><EFBFBD></td><th width="60%" align="center"><EFBFBD></th><td width="20%" align="right"><EFBFBD><a accesskey="n" href="apb.html">Next</a></td></tr></table><hr></div><div class="appendix"><h2 class="title" style="clear: both"><a name="sampledoc"></a>A. Sample Document</h2><pre class="programlisting">
|
||||||
<?xml version="1.0"?>
|
<?xml version="1.0"?>
|
||||||
<story>
|
<story>
|
||||||
<storyinfo>
|
<storyinfo>
|
||||||
@ -12,4 +12,4 @@
|
|||||||
<para>This is the body text.</para>
|
<para>This is the body text.</para>
|
||||||
</body>
|
</body>
|
||||||
</story>
|
</story>
|
||||||
</pre></div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="ar01s06.html">Prev</a><EFBFBD></td><td width="20%" align="center"><a accesskey="u" href="index.html">Up</a></td><td width="40%" align="right"><EFBFBD><a accesskey="n" href="apb.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">Writing Attribute<74></td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"><EFBFBD>B. Code for Keyword Example</td></tr></table></div></body></html>
|
</pre></div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="ar01s07.html">Prev</a><EFBFBD></td><td width="20%" align="center"><a accesskey="u" href="index.html">Up</a></td><td width="40%" align="right"><EFBFBD><a accesskey="n" href="apb.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">Retrieving Attributes<EFBFBD></td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"><EFBFBD>B. Code for Keyword Example</td></tr></table></div></body></html>
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
|
||||||
<html><head><meta content="text/html; charset=ISO-8859-1" http-equiv="Content-Type"><title>D. Code for Add Attribute Example</title><meta name="generator" content="DocBook XSL Stylesheets V1.48"><link rel="home" href="index.html" title="Libxml Tutorial"><link rel="up" href="index.html" title="Libxml Tutorial"><link rel="previous" href="apc.html" title="C. Code for Add Keyword Example"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">D. Code for Add Attribute Example</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="apc.html">Prev</a><EFBFBD></td><th width="60%" align="center"><EFBFBD></th><td width="20%" align="right"><EFBFBD></td></tr></table><hr></div><div class="appendix"><h2 class="title" style="clear: both"><a name="addattributeappendix"></a>D. Code for Add Attribute Example</h2><p>
|
<html><head><meta content="text/html; charset=ISO-8859-1" http-equiv="Content-Type"><title>D. Code for Add Attribute Example</title><meta name="generator" content="DocBook XSL Stylesheets V1.48"><link rel="home" href="index.html" title="Libxml Tutorial"><link rel="up" href="index.html" title="Libxml Tutorial"><link rel="previous" href="apc.html" title="C. Code for Add Keyword Example"><link rel="next" href="ape.html" title="E. Code for Retrieving Attribute Value Example"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">D. Code for Add Attribute Example</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="apc.html">Prev</a><EFBFBD></td><th width="60%" align="center"><EFBFBD></th><td width="20%" align="right"><EFBFBD><a accesskey="n" href="ape.html">Next</a></td></tr></table><hr></div><div class="appendix"><h2 class="title" style="clear: both"><a name="addattributeappendix"></a>D. Code for Add Attribute Example</h2><p>
|
||||||
<pre class="programlisting">
|
<pre class="programlisting">
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
@ -64,4 +64,4 @@ main(int argc, char **argv) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
</pre>
|
</pre>
|
||||||
</p></div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="apc.html">Prev</a><EFBFBD></td><td width="20%" align="center"><a accesskey="u" href="index.html">Up</a></td><td width="40%" align="right"><EFBFBD></td></tr><tr><td width="40%" align="left" valign="top">C. Code for Add Keyword Example<6C></td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"><EFBFBD></td></tr></table></div></body></html>
|
</p></div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="apc.html">Prev</a><EFBFBD></td><td width="20%" align="center"><a accesskey="u" href="index.html">Up</a></td><td width="40%" align="right"><EFBFBD><a accesskey="n" href="ape.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">C. Code for Add Keyword Example<6C></td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"><EFBFBD>E. Code for Retrieving Attribute Value Example</td></tr></table></div></body></html>
|
||||||
|
73
doc/tutorial/ape.html
Normal file
73
doc/tutorial/ape.html
Normal file
@ -0,0 +1,73 @@
|
|||||||
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
|
||||||
|
<html><head><meta content="text/html; charset=ISO-8859-1" http-equiv="Content-Type"><title>E. Code for Retrieving Attribute Value Example</title><meta name="generator" content="DocBook XSL Stylesheets V1.48"><link rel="home" href="index.html" title="Libxml Tutorial"><link rel="up" href="index.html" title="Libxml Tutorial"><link rel="previous" href="apd.html" title="D. Code for Add Attribute Example"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">E. Code for Retrieving Attribute Value Example</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="apd.html">Prev</a><EFBFBD></td><th width="60%" align="center"><EFBFBD></th><td width="20%" align="right"><EFBFBD></td></tr></table><hr></div><div class="appendix"><h2 class="title" style="clear: both"><a name="getattributeappendix"></a>E. Code for Retrieving Attribute Value Example</h2><p>
|
||||||
|
<pre class="programlisting">
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <libxml/xmlmemory.h>
|
||||||
|
#include <libxml/parser.h>
|
||||||
|
|
||||||
|
void
|
||||||
|
getReference (xmlDocPtr doc, xmlNodePtr cur) {
|
||||||
|
|
||||||
|
cur = cur->xmlChildrenNode;
|
||||||
|
while (cur != NULL) {
|
||||||
|
if ((!xmlStrcmp(cur->name, (const xmlChar *)"reference"))) {
|
||||||
|
printf("uri: %s\n", xmlGetProp(cur, "uri"));
|
||||||
|
}
|
||||||
|
cur = cur->next;
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void
|
||||||
|
parseDoc(char *docname) {
|
||||||
|
|
||||||
|
xmlDocPtr doc;
|
||||||
|
xmlNodePtr cur;
|
||||||
|
|
||||||
|
doc = xmlParseFile(docname);
|
||||||
|
|
||||||
|
if (doc == NULL ) {
|
||||||
|
fprintf(stderr,"Document not parsed successfully. \n");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
cur = xmlDocGetRootElement(doc);
|
||||||
|
|
||||||
|
if (cur == NULL) {
|
||||||
|
fprintf(stderr,"empty document\n");
|
||||||
|
xmlFreeDoc(doc);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (xmlStrcmp(cur->name, (const xmlChar *) "story")) {
|
||||||
|
fprintf(stderr,"document of the wrong type, root node != story");
|
||||||
|
xmlFreeDoc(doc);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
getReference (doc, cur);
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
main(int argc, char **argv) {
|
||||||
|
|
||||||
|
char *docname;
|
||||||
|
|
||||||
|
if (argc <= 1) {
|
||||||
|
printf("Usage: %s docname\n", argv[0]);
|
||||||
|
return(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
docname = argv[1];
|
||||||
|
parseDoc (docname);
|
||||||
|
|
||||||
|
return (1);
|
||||||
|
}
|
||||||
|
|
||||||
|
</pre>
|
||||||
|
</p></div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="apd.html">Prev</a><EFBFBD></td><td width="20%" align="center"><a accesskey="u" href="index.html">Up</a></td><td width="40%" align="right"><EFBFBD></td></tr><tr><td width="40%" align="left" valign="top">D. Code for Add Attribute Example<6C></td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"><EFBFBD></td></tr></table></div></body></html>
|
@ -1,5 +1,5 @@
|
|||||||
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
|
||||||
<html><head><meta content="text/html; charset=ISO-8859-1" http-equiv="Content-Type"><title>Writing Attribute</title><meta name="generator" content="DocBook XSL Stylesheets V1.48"><link rel="home" href="index.html" title="Libxml Tutorial"><link rel="up" href="index.html" title="Libxml Tutorial"><link rel="previous" href="ar01s05.html" title="Writing element content"><link rel="next" href="apa.html" title="A. Sample Document"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">Writing Attribute</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="ar01s05.html">Prev</a><EFBFBD></td><th width="60%" align="center"><EFBFBD></th><td width="20%" align="right"><EFBFBD><a accesskey="n" href="apa.html">Next</a></td></tr></table><hr></div><div class="sect1"><div class="titlepage"><div><h2 class="title" style="clear: both"><a name="xmltutorialwritingattribute"></a>Writing Attribute</h2></div></div><p>Writing an attribute is similar to writing text to a new element. In
|
<html><head><meta content="text/html; charset=ISO-8859-1" http-equiv="Content-Type"><title>Writing Attribute</title><meta name="generator" content="DocBook XSL Stylesheets V1.48"><link rel="home" href="index.html" title="Libxml Tutorial"><link rel="up" href="index.html" title="Libxml Tutorial"><link rel="previous" href="ar01s05.html" title="Writing element content"><link rel="next" href="ar01s07.html" title="Retrieving Attributes"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">Writing Attribute</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="ar01s05.html">Prev</a><EFBFBD></td><th width="60%" align="center"><EFBFBD></th><td width="20%" align="right"><EFBFBD><a accesskey="n" href="ar01s07.html">Next</a></td></tr></table><hr></div><div class="sect1"><div class="titlepage"><div><h2 class="title" style="clear: both"><a name="xmltutorialwritingattribute"></a>Writing Attribute</h2></div></div><p>Writing an attribute is similar to writing text to a new element. In
|
||||||
this case, we'll add a reference URI to our
|
this case, we'll add a reference URI to our
|
||||||
document. Full code:<a href="apd.html" title="D. Code for Add Attribute Example">Appendix D</a>.</p><p>
|
document. Full code:<a href="apd.html" title="D. Code for Add Attribute Example">Appendix D</a>.</p><p>
|
||||||
A <tt class="sgmltag-element">reference</tt> is a child of the <tt class="sgmltag-element">story</tt>
|
A <tt class="sgmltag-element">reference</tt> is a child of the <tt class="sgmltag-element">story</tt>
|
||||||
@ -27,4 +27,4 @@
|
|||||||
<div class="calloutlist"><table border="0" summary="Callout list"><tr><td width="5%" valign="top" align="left"><a href="#addreferencenode"><img src="images/callouts/1.png" alt="1" border="0"></a> </td><td valign="top" align="left"><p>First we add a new node at the location of the current node
|
<div class="calloutlist"><table border="0" summary="Callout list"><tr><td width="5%" valign="top" align="left"><a href="#addreferencenode"><img src="images/callouts/1.png" alt="1" border="0"></a> </td><td valign="top" align="left"><p>First we add a new node at the location of the current node
|
||||||
pointer, <tt>cur.</tt> using the <a href="http://xmlsoft.org/html/libxml-tree.html#XMLNEWTEXTCHILD" target="_top">xmlNewTextChild</a> function.</p></td></tr></table></div>
|
pointer, <tt>cur.</tt> using the <a href="http://xmlsoft.org/html/libxml-tree.html#XMLNEWTEXTCHILD" target="_top">xmlNewTextChild</a> function.</p></td></tr></table></div>
|
||||||
</p><p>Once the node is added, the file is written to disk just as in the
|
</p><p>Once the node is added, the file is written to disk just as in the
|
||||||
previous example in which we added an element with text content.</p></div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="ar01s05.html">Prev</a><EFBFBD></td><td width="20%" align="center"><a accesskey="u" href="index.html">Up</a></td><td width="40%" align="right"><EFBFBD><a accesskey="n" href="apa.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">Writing element content<6E></td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"><EFBFBD>A. Sample Document</td></tr></table></div></body></html>
|
previous example in which we added an element with text content.</p></div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="ar01s05.html">Prev</a><EFBFBD></td><td width="20%" align="center"><a accesskey="u" href="index.html">Up</a></td><td width="40%" align="right"><EFBFBD><a accesskey="n" href="ar01s07.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">Writing element content<6E></td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"><EFBFBD>Retrieving Attributes</td></tr></table></div></body></html>
|
||||||
|
35
doc/tutorial/ar01s07.html
Normal file
35
doc/tutorial/ar01s07.html
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
|
||||||
|
<html><head><meta content="text/html; charset=ISO-8859-1" http-equiv="Content-Type"><title>Retrieving Attributes</title><meta name="generator" content="DocBook XSL Stylesheets V1.48"><link rel="home" href="index.html" title="Libxml Tutorial"><link rel="up" href="index.html" title="Libxml Tutorial"><link rel="previous" href="ar01s06.html" title="Writing Attribute"><link rel="next" href="apa.html" title="A. Sample Document"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">Retrieving Attributes</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="ar01s06.html">Prev</a><EFBFBD></td><th width="60%" align="center"><EFBFBD></th><td width="20%" align="right"><EFBFBD><a accesskey="n" href="apa.html">Next</a></td></tr></table><hr></div><div class="sect1"><div class="titlepage"><div><h2 class="title" style="clear: both"><a name="xmltutorialattribute"></a>Retrieving Attributes</h2></div></div><p>Retrieving the value of an attribute is similar to the previous
|
||||||
|
example in which we retrieved a node's text contents. In this case we'll
|
||||||
|
extract the value of the URI we added in the previous
|
||||||
|
section. Full code: <a href="ape.html" title="E. Code for Retrieving Attribute Value Example">Appendix E</a>.</p><p>
|
||||||
|
The initial steps for this example are similar to the previous ones: parse
|
||||||
|
the doc, find the element you are interested in, then enter a function to
|
||||||
|
carry out the specific task required. In this case, we call
|
||||||
|
<tt>getReference</tt>:
|
||||||
|
<pre class="programlisting">
|
||||||
|
void
|
||||||
|
getReference (xmlDocPtr doc, xmlNodePtr cur) {
|
||||||
|
|
||||||
|
cur = cur->xmlChildrenNode;
|
||||||
|
while (cur != NULL) {
|
||||||
|
if ((!xmlStrcmp(cur->name, (const xmlChar *)"reference"))) {
|
||||||
|
<a name="getattributevalue"></a><img src="images/callouts/1.png" alt="1" border="0"> printf("uri: %s\n", xmlGetProp(cur, "uri"));
|
||||||
|
}
|
||||||
|
cur = cur->next;
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<div class="calloutlist"><table border="0" summary="Callout list"><tr><td width="5%" valign="top" align="left"><a href="#getattributevalue"><img src="images/callouts/1.png" alt="1" border="0"></a> </td><td valign="top" align="left"><p>
|
||||||
|
The key function is <tt><a href="http://xmlsoft.org/html/libxml-tree.html#XMLGETPROP" target="_top">xmlGetProp</a></tt>, which returns an
|
||||||
|
<tt>xmlChar</tt> containing the attribute's value. In this case,
|
||||||
|
we just print it out.
|
||||||
|
<div class="note" style="margin-left: 0.5in; margin-right: 0.5in;"><h3 class="title">Note</h3><p>
|
||||||
|
If you are using a DTD that declares a fixed or
|
||||||
|
default value for the attribute, this function will retrieve it.
|
||||||
|
</p></div>
|
||||||
|
</p></td></tr></table></div>
|
||||||
|
|
||||||
|
</p></div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="ar01s06.html">Prev</a><EFBFBD></td><td width="20%" align="center"><a accesskey="u" href="index.html">Up</a></td><td width="40%" align="right"><EFBFBD><a accesskey="n" href="apa.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">Writing Attribute<74></td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"><EFBFBD>A. Sample Document</td></tr></table></div></body></html>
|
69
doc/tutorial/includegetattribute.c
Normal file
69
doc/tutorial/includegetattribute.c
Normal file
@ -0,0 +1,69 @@
|
|||||||
|
<![CDATA[
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <libxml/xmlmemory.h>
|
||||||
|
#include <libxml/parser.h>
|
||||||
|
|
||||||
|
void
|
||||||
|
getReference (xmlDocPtr doc, xmlNodePtr cur) {
|
||||||
|
|
||||||
|
cur = cur->xmlChildrenNode;
|
||||||
|
while (cur != NULL) {
|
||||||
|
if ((!xmlStrcmp(cur->name, (const xmlChar *)"reference"))) {
|
||||||
|
printf("uri: %s\n", xmlGetProp(cur, "uri"));
|
||||||
|
}
|
||||||
|
cur = cur->next;
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void
|
||||||
|
parseDoc(char *docname) {
|
||||||
|
|
||||||
|
xmlDocPtr doc;
|
||||||
|
xmlNodePtr cur;
|
||||||
|
|
||||||
|
doc = xmlParseFile(docname);
|
||||||
|
|
||||||
|
if (doc == NULL ) {
|
||||||
|
fprintf(stderr,"Document not parsed successfully. \n");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
cur = xmlDocGetRootElement(doc);
|
||||||
|
|
||||||
|
if (cur == NULL) {
|
||||||
|
fprintf(stderr,"empty document\n");
|
||||||
|
xmlFreeDoc(doc);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (xmlStrcmp(cur->name, (const xmlChar *) "story")) {
|
||||||
|
fprintf(stderr,"document of the wrong type, root node != story");
|
||||||
|
xmlFreeDoc(doc);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
getReference (doc, cur);
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
int
|
||||||
|
main(int argc, char **argv) {
|
||||||
|
|
||||||
|
char *docname;
|
||||||
|
|
||||||
|
if (argc <= 1) {
|
||||||
|
printf("Usage: %s docname\n", argv[0]);
|
||||||
|
return(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
docname = argv[1];
|
||||||
|
parseDoc (docname);
|
||||||
|
|
||||||
|
return (1);
|
||||||
|
}
|
||||||
|
]]>
|
@ -1,5 +1,5 @@
|
|||||||
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
|
||||||
<html><head><meta content="text/html; charset=ISO-8859-1" http-equiv="Content-Type"><title>Libxml Tutorial</title><meta name="generator" content="DocBook XSL Stylesheets V1.48"><link rel="home" href="index.html" title="Libxml Tutorial"><link rel="next" href="ar01s02.html" title="Data Types"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">Libxml Tutorial</th></tr><tr><td width="20%" align="left"><EFBFBD></td><th width="60%" align="center"><EFBFBD></th><td width="20%" align="right"><EFBFBD><a accesskey="n" href="ar01s02.html">Next</a></td></tr></table><hr></div><div class="article"><div class="titlepage"><div><h1 class="title"><a name="id2759085"></a>Libxml Tutorial</h1></div><div><h3 class="author">John Fleck</h3></div><div><p class="copyright">Copyright <20> 2002 John Fleck</p></div><div><div class="revhistory"><table border="1" width="100%" summary="Revision history"><tr><th align="left" valign="top" colspan="2"><b>Revision History</b></th></tr><tr><td align="left">Revision 1</td><td align="left">June 4,2002</td></tr></table></div></div><hr></div><div class="toc"><p><b>Table of Contents</b></p><dl><dt><a href="index.html#introduction">Introduction</a></dt><dt><a href="ar01s02.html">Data Types</a></dt><dt><a href="ar01s03.html">Parsing the file</a></dt><dt><a href="ar01s04.html">Retrieving Element Content</a></dt><dt><a href="ar01s05.html">Writing element content</a></dt><dt><a href="ar01s06.html">Writing Attribute</a></dt><dt>A. <a href="apa.html">Sample Document</a></dt><dt>B. <a href="apb.html">Code for Keyword Example</a></dt><dt>C. <a href="apc.html">Code for Add Keyword Example</a></dt><dt>D. <a href="apd.html">Code for Add Attribute Example</a></dt></dl></div><div class="abstract"><p><b>Abstract</b></p><p>Libxml is a freely licensed C language library for handling
|
<html><head><meta content="text/html; charset=ISO-8859-1" http-equiv="Content-Type"><title>Libxml Tutorial</title><meta name="generator" content="DocBook XSL Stylesheets V1.48"><link rel="home" href="index.html" title="Libxml Tutorial"><link rel="next" href="ar01s02.html" title="Data Types"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">Libxml Tutorial</th></tr><tr><td width="20%" align="left"><EFBFBD></td><th width="60%" align="center"><EFBFBD></th><td width="20%" align="right"><EFBFBD><a accesskey="n" href="ar01s02.html">Next</a></td></tr></table><hr></div><div class="article"><div class="titlepage"><div><h1 class="title"><a name="id2755382"></a>Libxml Tutorial</h1></div><div><h3 class="author">John Fleck</h3></div><div><p class="copyright">Copyright <20> 2002 John Fleck</p></div><div><div class="revhistory"><table border="1" width="100%" summary="Revision history"><tr><th align="left" valign="top" colspan="2"><b>Revision History</b></th></tr><tr><td align="left">Revision 1</td><td align="left">June 4,2002</td></tr><tr><td align="left">Revision 2</td><td align="left">June 12, 2002</td></tr></table></div></div><hr></div><div class="toc"><p><b>Table of Contents</b></p><dl><dt><a href="index.html#introduction">Introduction</a></dt><dt><a href="ar01s02.html">Data Types</a></dt><dt><a href="ar01s03.html">Parsing the file</a></dt><dt><a href="ar01s04.html">Retrieving Element Content</a></dt><dt><a href="ar01s05.html">Writing element content</a></dt><dt><a href="ar01s06.html">Writing Attribute</a></dt><dt><a href="ar01s07.html">Retrieving Attributes</a></dt><dt>A. <a href="apa.html">Sample Document</a></dt><dt>B. <a href="apb.html">Code for Keyword Example</a></dt><dt>C. <a href="apc.html">Code for Add Keyword Example</a></dt><dt>D. <a href="apd.html">Code for Add Attribute Example</a></dt><dt>E. <a href="ape.html">Code for Retrieving Attribute Value Example</a></dt></dl></div><div class="abstract"><p><b>Abstract</b></p><p>Libxml is a freely licensed C language library for handling
|
||||||
XML, portable across a large number of platforms. This
|
XML, portable across a large number of platforms. This
|
||||||
tutorial provides examples of its basic functions.</p></div><div class="sect1"><div class="titlepage"><div><h2 class="title" style="clear: both"><a name="introduction"></a>Introduction</h2></div></div><p>Libxml is a C language library implementing functions for reading,
|
tutorial provides examples of its basic functions.</p></div><div class="sect1"><div class="titlepage"><div><h2 class="title" style="clear: both"><a name="introduction"></a>Introduction</h2></div></div><p>Libxml is a C language library implementing functions for reading,
|
||||||
creating and manipulating XML data. This tutorial
|
creating and manipulating XML data. This tutorial
|
||||||
@ -11,6 +11,5 @@
|
|||||||
</p><p>The tutorial is based on a simple XML application I
|
</p><p>The tutorial is based on a simple XML application I
|
||||||
use for articles I write. The format includes metadata and the body
|
use for articles I write. The format includes metadata and the body
|
||||||
of the article.</p><p>The example code in this tutorial demonstrates how to:
|
of the article.</p><p>The example code in this tutorial demonstrates how to:
|
||||||
<div class="itemizedlist"><ul type="disc"><li><p>Parse the document.</p></li><li><p>Extract the text within a specified element.</p></li><li><p>Add an element and its content.</p></li><li><p>Extract the value of an attribute.</p></li><li><p>Add an attribute.</p></li></ul></div>
|
<div class="itemizedlist"><ul type="disc"><li><p>Parse the document.</p></li><li><p>Extract the text within a specified element.</p></li><li><p>Add an element and its content.</p></li><li><p>Add an attribute.</p></li><li><p>Extract the value of an attribute.</p></li></ul></div>
|
||||||
|
|
||||||
</p><p>Full code for the examples is included in the appendices.</p></div></div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><EFBFBD></td><td width="20%" align="center"><EFBFBD></td><td width="40%" align="right"><EFBFBD><a accesskey="n" href="ar01s02.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top"><EFBFBD></td><td width="20%" align="center"><EFBFBD></td><td width="40%" align="right" valign="top"><EFBFBD>Data Types</td></tr></table></div></body></html>
|
</p><p>Full code for the examples is included in the appendices.</p></div></div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><EFBFBD></td><td width="20%" align="center"><EFBFBD></td><td width="40%" align="right"><EFBFBD><a accesskey="n" href="ar01s02.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top"><EFBFBD></td><td width="20%" align="center"><EFBFBD></td><td width="40%" align="right" valign="top"><EFBFBD>Data Types</td></tr></table></div></body></html>
|
||||||
|
@ -5,6 +5,7 @@
|
|||||||
<!ENTITY STORY SYSTEM "includestory.xml">
|
<!ENTITY STORY SYSTEM "includestory.xml">
|
||||||
<!ENTITY ADDKEYWORD SYSTEM "includeaddkeyword.c">
|
<!ENTITY ADDKEYWORD SYSTEM "includeaddkeyword.c">
|
||||||
<!ENTITY ADDATTRIBUTE SYSTEM "includeaddattribute.c">
|
<!ENTITY ADDATTRIBUTE SYSTEM "includeaddattribute.c">
|
||||||
|
<!ENTITY GETATTRIBUTE SYSTEM "includegetattribute.c">
|
||||||
]>
|
]>
|
||||||
<article>
|
<article>
|
||||||
<articleinfo>
|
<articleinfo>
|
||||||
@ -22,6 +23,10 @@
|
|||||||
<revnumber>1</revnumber>
|
<revnumber>1</revnumber>
|
||||||
<date>June 4,2002</date>
|
<date>June 4,2002</date>
|
||||||
</revision>
|
</revision>
|
||||||
|
<revision>
|
||||||
|
<revnumber>2</revnumber>
|
||||||
|
<date>June 12, 2002</date>
|
||||||
|
</revision>
|
||||||
</revhistory>
|
</revhistory>
|
||||||
</articleinfo>
|
</articleinfo>
|
||||||
<abstract>
|
<abstract>
|
||||||
@ -58,14 +63,13 @@
|
|||||||
<listitem>
|
<listitem>
|
||||||
<para>Add an element and its content.</para>
|
<para>Add an element and its content.</para>
|
||||||
</listitem>
|
</listitem>
|
||||||
|
<listitem>
|
||||||
|
<para>Add an attribute.</para>
|
||||||
|
</listitem>
|
||||||
<listitem>
|
<listitem>
|
||||||
<para>Extract the value of an attribute.</para>
|
<para>Extract the value of an attribute.</para>
|
||||||
</listitem>
|
</listitem>
|
||||||
<listitem>
|
|
||||||
<para>Add an attribute.</para>
|
|
||||||
</listitem>
|
|
||||||
</itemizedlist>
|
</itemizedlist>
|
||||||
|
|
||||||
</para>
|
</para>
|
||||||
<para>Full code for the examples is included in the appendices.</para>
|
<para>Full code for the examples is included in the appendices.</para>
|
||||||
|
|
||||||
@ -288,15 +292,7 @@ parseStory (xmlDocPtr doc, xmlNodePtr cur, char *keyword) {
|
|||||||
the old file. The second parameter is a pointer to the xmlDoc
|
the old file. The second parameter is a pointer to the xmlDoc
|
||||||
structure. Setting the third parameter equal to one ensures indenting on output.
|
structure. Setting the third parameter equal to one ensures indenting on output.
|
||||||
</para>
|
</para>
|
||||||
|
|
||||||
</sect1>
|
</sect1>
|
||||||
<!--
|
|
||||||
<sect1 id="xmltutorialattribute">
|
|
||||||
<title>Retrieving Attributes</title>
|
|
||||||
<para>Retrieving</para>
|
|
||||||
</sect1>
|
|
||||||
-->
|
|
||||||
|
|
||||||
|
|
||||||
<sect1 id="xmltutorialwritingattribute">
|
<sect1 id="xmltutorialwritingattribute">
|
||||||
<title>Writing Attribute</title>
|
<title>Writing Attribute</title>
|
||||||
@ -341,6 +337,52 @@ parseStory (xmlDocPtr doc, xmlNodePtr cur, char *keyword) {
|
|||||||
|
|
||||||
</sect1>
|
</sect1>
|
||||||
|
|
||||||
|
<sect1 id="xmltutorialattribute">
|
||||||
|
<title>Retrieving Attributes</title>
|
||||||
|
<para>Retrieving the value of an attribute is similar to the previous
|
||||||
|
example in which we retrieved a node's text contents. In this case we'll
|
||||||
|
extract the value of the <acronym>URI</acronym> we added in the previous
|
||||||
|
section. Full code: <xref linkend="getattributeappendix" />.</para>
|
||||||
|
<para>
|
||||||
|
The initial steps for this example are similar to the previous ones: parse
|
||||||
|
the doc, find the element you are interested in, then enter a function to
|
||||||
|
carry out the specific task required. In this case, we call
|
||||||
|
<function>getReference</function>:
|
||||||
|
<programlisting>
|
||||||
|
void
|
||||||
|
getReference (xmlDocPtr doc, xmlNodePtr cur) {
|
||||||
|
|
||||||
|
cur = cur->xmlChildrenNode;
|
||||||
|
while (cur != NULL) {
|
||||||
|
if ((!xmlStrcmp(cur->name, (const xmlChar *)"reference"))) {
|
||||||
|
<co id="getattributevalue" /> printf("uri: %s\n", xmlGetProp(cur, "uri"));
|
||||||
|
}
|
||||||
|
cur = cur->next;
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
</programlisting>
|
||||||
|
|
||||||
|
<calloutlist>
|
||||||
|
<callout arearefs="getattributevalue">
|
||||||
|
<para>
|
||||||
|
The key function is <function><ulink
|
||||||
|
url="http://xmlsoft.org/html/libxml-tree.html#XMLGETPROP">xmlGetProp</ulink></function>, which returns an
|
||||||
|
<varname>xmlChar</varname> containing the attribute's value. In this case,
|
||||||
|
we just print it out.
|
||||||
|
<note>
|
||||||
|
<para>
|
||||||
|
If you are using a <acronym>DTD</acronym> that declares a fixed or
|
||||||
|
default value for the attribute, this function will retrieve it.
|
||||||
|
</para>
|
||||||
|
</note>
|
||||||
|
</para>
|
||||||
|
</callout>
|
||||||
|
</calloutlist>
|
||||||
|
|
||||||
|
</para>
|
||||||
|
</sect1>
|
||||||
|
|
||||||
<!--
|
<!--
|
||||||
<appendix id="furtherresources">
|
<appendix id="furtherresources">
|
||||||
<title>Further Resources</title>
|
<title>Further Resources</title>
|
||||||
@ -369,4 +411,10 @@ parseStory (xmlDocPtr doc, xmlNodePtr cur, char *keyword) {
|
|||||||
<programlisting>&ADDATTRIBUTE;</programlisting>
|
<programlisting>&ADDATTRIBUTE;</programlisting>
|
||||||
</para>
|
</para>
|
||||||
</appendix>
|
</appendix>
|
||||||
|
<appendix id="getattributeappendix">
|
||||||
|
<title>Code for Retrieving Attribute Value Example</title>
|
||||||
|
<para>
|
||||||
|
<programlisting>&GETATTRIBUTE;</programlisting>
|
||||||
|
</para>
|
||||||
|
</appendix>
|
||||||
</article>
|
</article>
|
||||||
|
Reference in New Issue
Block a user