5715 lines · 264.8 KB · HTML
RawDownload
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"    "http://www.w3.org/TR/html4/loose.dtd"><html><head>  <title>The XML C parser and toolkit of Gnome</title>  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/></head><body bgcolor="#ffffff"><h1 align="center">The XML C parser and toolkit of Gnome</h1> <h1>Note: this is the flat content of the <a href="index.html">website</a></h1> <h1 style="text-align: center">libxml, a.k.a. gnome-xml</h1> <p></p> <pstyle="text-align: right; font-style: italic; font-size: 10pt">"Programmingwith libxml2 is like the thrilling embrace of an exotic stranger." <ahref="http://diveintomark.org/archives/2004/02/18/libxml2">MarkPilgrim</a></p> <p>Libxml2 is the XML C parser and toolkit developed for the Gnome project(but usable outside of the Gnome platform), it is free software availableunder the <a href="http://www.opensource.org/licenses/mit-license.html">MITLicense</a>. XML itself is a metalanguage to design markup languages, i.e.text language where semantic and structure are added to the content usingextra "markup" information enclosed between angle brackets. HTML is the mostwell-known markup language. Though the library is written in C <ahref="python.html">a variety of language bindings</a> make it available inother environments.</p> <p>Libxml2 is known to be very portable, the library should build and workwithout serious troubles on a variety of systems (Linux, Unix, Windows,CygWin, MacOS, MacOS X, RISC Os, OS/2, VMS, QNX, MVS, VxWorks, ...)</p> <p>Libxml2 implements a number of existing standards related to markuplanguages:</p><ul>  <li>the XML standard: <a    href="http://www.w3.org/TR/REC-xml">http://www.w3.org/TR/REC-xml</a></li>  <li>Namespaces in XML: <a    href="http://www.w3.org/TR/REC-xml-names/">http://www.w3.org/TR/REC-xml-names/</a></li>  <li>XML Base: <a    href="http://www.w3.org/TR/xmlbase/">http://www.w3.org/TR/xmlbase/</a></li>  <li><a href="http://www.cis.ohio-state.edu/rfc/rfc2396.txt">RFC 2396</a> :    Uniform Resource Identifiers <a    href="http://www.ietf.org/rfc/rfc2396.txt">http://www.ietf.org/rfc/rfc2396.txt</a></li>  <li>XML Path Language (XPath) 1.0: <a    href="http://www.w3.org/TR/xpath">http://www.w3.org/TR/xpath</a></li>  <li>HTML4 parser: <a    href="http://www.w3.org/TR/html401/">http://www.w3.org/TR/html401/</a></li>  <li>XML Pointer Language (XPointer) Version 1.0: <a    href="http://www.w3.org/TR/xptr">http://www.w3.org/TR/xptr</a></li>  <li>XML Inclusions (XInclude) Version 1.0: <a    href="http://www.w3.org/TR/xinclude/">http://www.w3.org/TR/xinclude/</a></li>  <li>ISO-8859-x encodings, as well as <a    href="http://www.cis.ohio-state.edu/rfc/rfc2044.txt">rfc2044</a> [UTF-8]    and <a href="http://www.cis.ohio-state.edu/rfc/rfc2781.txt">rfc2781</a>    [UTF-16] Unicode encodings, and more if using iconv support</li>  <li>part of SGML Open Technical Resolution TR9401:1997</li>  <li>XML Catalogs Working Draft 06 August 2001: <a    href="http://www.oasis-open.org/committees/entity/spec-2001-08-06.html">http://www.oasis-open.org/committees/entity/spec-2001-08-06.html</a></li>  <li>Canonical XML Version 1.0: <a    href="http://www.w3.org/TR/xml-c14n">http://www.w3.org/TR/xml-c14n</a>    and the Exclusive XML Canonicalization CR draft <a    href="http://www.w3.org/TR/xml-exc-c14n">http://www.w3.org/TR/xml-exc-c14n</a></li>  <li>Relax NG, ISO/IEC 19757-2:2003, <a    href="http://www.oasis-open.org/committees/relax-ng/spec-20011203.html">http://www.oasis-open.org/committees/relax-ng/spec-20011203.html</a></li>  <li>W3C XML Schemas Part 2: Datatypes <a    href="http://www.w3.org/TR/2001/REC-xmlschema-2-20010502/">REC 02 May    2001</a></li>  <li>W3C <a href="http://www.w3.org/TR/xml-id/">xml:id</a> Working Draft 7    April 2004</li></ul> <p>In most cases libxml2 tries to implement the specifications in arelatively strictly compliant way. As of release 2.4.16, libxml2 passed all1800+ tests from the <ahref="http://www.oasis-open.org/committees/xml-conformance/">OASIS XML TestsSuite</a>.</p> <p>To some extent libxml2 provides support for the following additionalspecifications but doesn't claim to implement them completely:</p><ul>  <li>Document Object Model (DOM) <a    href="http://www.w3.org/TR/DOM-Level-2-Core/">http://www.w3.org/TR/DOM-Level-2-Core/</a>    the document model, but it doesn't implement the API itself, gdome2 does    this on top of libxml2</li>  <li><a href="http://www.cis.ohio-state.edu/rfc/rfc959.txt">RFC 959</a> :    libxml2 implements a basic FTP client code</li>  <li><a href="http://www.cis.ohio-state.edu/rfc/rfc1945.txt">RFC 1945</a> :    HTTP/1.0, again a basic HTTP client code</li>  <li>SAX: a SAX2 like interface and a minimal SAX1 implementation compatible    with early expat versions</li></ul> <p>A partial implementation of <ahref="http://www.w3.org/TR/2001/REC-xmlschema-1-20010502/">XML Schemas Part1: Structure</a> is being worked on but it would be far too early to make anyconformance statement about it at the moment.</p> <p>Separate documents:</p><ul>  <li><a href="http://xmlsoft.org/XSLT/">the libxslt page</a> providing an    implementation of XSLT 1.0 and common extensions like EXSLT for  libxml2</li>  <li><a href="http://gdome2.cs.unibo.it/">the gdome2 page</a>    : a standard DOM2 implementation for libxml2</li>  <li><a href="http://www.aleksey.com/xmlsec/">the XMLSec page</a>: an    implementation of <a href="http://www.w3.org/TR/xmldsig-core/">W3C XML    Digital Signature</a> for libxml2</li>  <li>also check the related links section for more related and active    projects.</li></ul><p> Hosting sponsored by <a href="http://www.aoemedia.de/opensource-cms.html">Open Source CMS services</a> from AOE media.</p> <p>Logo designed by <a href="mailto:liyanage@access.ch">Marc Liyanage</a>.</p> <h2><a name="Introducti">Introduction</a></h2> <p>This document describes libxml, the <ahref="http://www.w3.org/XML/">XML</a> C parser and toolkit developed for the<a href="http://www.gnome.org/">Gnome</a> project. <ahref="http://www.w3.org/XML/">XML is a standard</a> for building tag-basedstructured documents/data.</p> <p>Here are some key points about libxml:</p><ul>  <li>Libxml2 exports Push (progressive) and Pull (blocking) type parser    interfaces for both XML and HTML.</li>  <li>Libxml2 can do DTD validation at parse time, using a parsed document    instance, or with an arbitrary DTD.</li>  <li>Libxml2 includes complete <a    href="http://www.w3.org/TR/xpath">XPath</a>, <a    href="http://www.w3.org/TR/xptr">XPointer</a> and <a    href="http://www.w3.org/TR/xinclude">XInclude</a> implementations.</li>  <li>It is written in plain C, making as few assumptions as possible, and    sticking closely to ANSI C/POSIX for easy embedding. Works on    Linux/Unix/Windows, ported to a number of other platforms.</li>  <li>Basic support for HTTP and FTP client allowing applications to fetch    remote resources.</li>  <li>The design is modular, most of the extensions can be compiled out.</li>  <li>The internal document representation is as close as possible to the <a    href="http://www.w3.org/DOM/">DOM</a> interfaces.</li>  <li>Libxml2 also has a <a    href="http://www.megginson.com/SAX/index.html">SAX like interface</a>;    the interface is designed to be compatible with <a    href="http://www.jclark.com/xml/expat.html">Expat</a>.</li>  <li>This library is released under the <a    href="http://www.opensource.org/licenses/mit-license.html">MIT    License</a>. See the Copyright file in the distribution for the precise    wording.</li></ul> <p>Warning: unless you are forced to because your application links with aGnome-1.X library requiring it,  <strong><spanstyle="background-color: #FF0000">Do Not Use libxml1</span></strong>, uselibxml2</p> <h2><a name="FAQ">FAQ</a></h2> <p>Table of Contents:</p><ul>  <li><a href="FAQ.html#License">License(s)</a></li>  <li><a href="FAQ.html#Installati">Installation</a></li>  <li><a href="FAQ.html#Compilatio">Compilation</a></li>  <li><a href="FAQ.html#Developer">Developer corner</a></li></ul> <h3><a name="License">License</a>(s)</h3><ol>  <li><em>Licensing Terms for libxml</em>    <p>libxml2 is released under the <a    href="http://www.opensource.org/licenses/mit-license.html">MIT    License</a>; see the file Copyright in the distribution for the precise    wording</p>  </li>  <li><em>Can I embed libxml2 in a proprietary application ?</em>    <p>Yes. The MIT License allows you to keep proprietary the changes you    made to libxml, but it would be graceful to send-back bug fixes and    improvements as patches for possible incorporation in the main    development tree.</p>  </li></ol> <h3><a name="Installati">Installation</a></h3><ol>  <li><strong><span style="background-color: #FF0000">Do Not Use    libxml1</span></strong>, use libxml2</li>  <p></p>  <li><em>Where can I get libxml</em> ?    <p>The original distribution comes from <a    href="ftp://xmlsoft.org/libxml2/">xmlsoft.org</a> or <a    href="ftp://ftp.gnome.org/pub/GNOME/sources/libxml2/2.6/">gnome.org</a></p>    <p>Most Linux and BSD distributions include libxml, this is probably the    safer way for end-users to use libxml.</p>    <p>David Doolin provides precompiled Windows versions at <a    href="http://www.ce.berkeley.edu/~doolin/code/libxmlwin32/         ">http://www.ce.berkeley.edu/~doolin/code/libxmlwin32/</a></p>  </li>  <p></p>  <li><em>I see libxml and libxml2 releases, which one should I install ?</em>    <ul>      <li>If you are not constrained by backward compatibility issues with        existing applications, install libxml2 only</li>      <li>If you are not doing development, you can safely install both.        Usually the packages <a        href="http://rpmfind.net/linux/RPM/libxml.html">libxml</a> and <a        href="http://rpmfind.net/linux/RPM/libxml2.html">libxml2</a> are        compatible (this is not the case for development packages).</li>      <li>If you are a developer and your system provides separate packaging        for shared libraries and the development components, it is possible        to install libxml and libxml2, and also <a        href="http://rpmfind.net/linux/RPM/libxml-devel.html">libxml-devel</a>        and <a        href="http://rpmfind.net/linux/RPM/libxml2-devel.html">libxml2-devel</a>        too for libxml2 &gt;= 2.3.0</li>      <li>If you are developing a new application, please develop against        libxml2(-devel)</li>    </ul>  </li>  <li><em>I can't install the libxml package, it conflicts with libxml0</em>    <p>You probably have an old libxml0 package used to provide the shared    library for libxml.so.0, you can probably safely remove it. The libxml    packages provided on <a    href="ftp://xmlsoft.org/libxml2/">xmlsoft.org</a> provide    libxml.so.0</p>  </li>  <li><em>I can't install the libxml(2) RPM package due to failed    dependencies</em>    <p>The most generic solution is to re-fetch the latest src.rpm , and    rebuild it locally with</p>    <p><code>rpm --rebuild libxml(2)-xxx.src.rpm</code>.</p>    <p>If everything goes well it will generate two binary rpm packages (one    providing the shared libs and xmllint, and the other one, the -devel    package, providing includes, static libraries and scripts needed to build    applications with libxml(2)) that you can install locally.</p>  </li></ol> <h3><a name="Compilatio">Compilation</a></h3><ol>  <li><em>What is the process to compile libxml2 ?</em>    <p>As most UNIX libraries libxml2 follows the "standard":</p>    <p><code>gunzip -c xxx.tar.gz | tar xvf -</code></p>    <p><code>cd libxml-xxxx</code></p>    <p><code>./configure --help</code></p>    <p>to see the options, then the compilation/installation proper</p>    <p><code>./configure [possible options]</code></p>    <p><code>make</code></p>    <p><code>make install</code></p>    <p>At that point you may have to rerun ldconfig or a similar utility to    update your list of installed shared libs.</p>  </li>  <li><em>What other libraries are needed to compile/install libxml2 ?</em>    <p>Libxml2 does not require any other library, the normal C ANSI API    should be sufficient (please report any violation to this rule you may    find).</p>    <p>However if found at configuration time libxml2 will detect and use the    following libs:</p>    <ul>      <li><a href="http://www.info-zip.org/pub/infozip/zlib/">libz</a> : a        highly portable and available widely compression library.</li>      <li>iconv: a powerful character encoding conversion library. It is        included by default in recent glibc libraries, so it doesn't need to        be installed specifically on Linux. It now seems a <a        href="http://www.opennc.org/onlinepubs/7908799/xsh/iconv.html">part        of the official UNIX</a> specification. Here is one <a        href="http://www.gnu.org/software/libiconv/">implementation of the        library</a> which source can be found <a        href="ftp://ftp.ilog.fr/pub/Users/haible/gnu/">here</a>.</li>    </ul>  </li>  <p></p>  <li><em>Make check fails on some platforms</em>    <p>Sometimes the regression tests' results don't completely match the    value produced by the parser, and the makefile uses diff to print the    delta. On some platforms the diff return breaks the compilation process;    if the diff is small this is probably not a serious problem.</p>    <p>Sometimes (especially on Solaris) make checks fail due to limitations    in make. Try using GNU-make instead.</p>  </li>  <li><em>I use the SVN version and there is no configure script</em>    <p>The configure script (and other Makefiles) are generated. Use the    autogen.sh script to regenerate the configure script and Makefiles,    like:</p>    <p><code>./autogen.sh --prefix=/usr --disable-shared</code></p>  </li>  <li><em>I have troubles when running make tests with gcc-3.0</em>    <p>It seems the initial release of gcc-3.0 has a problem with the    optimizer which miscompiles the URI module. Please use another    compiler.</p>  </li></ol> <h3><a name="Developer">Developer</a> corner</h3><ol>  <li><em>Troubles compiling or linking programs using libxml2</em>    <p>Usually the problem comes from the fact that the compiler doesn't get    the right compilation or linking flags. There is a small shell script    <code>xml2-config</code> which is installed as part of libxml2 usual    install process which provides those flags. Use</p>    <p><code>xml2-config --cflags</code></p>    <p>to get the compilation flags and</p>    <p><code>xml2-config --libs</code></p>    <p>to get the linker flags. Usually this is done directly from the    Makefile as:</p>    <p><code>CFLAGS=`xml2-config --cflags`</code></p>    <p><code>LIBS=`xml2-config --libs`</code></p>  </li>  <li><em>I want to install my own copy of libxml2 in my home directory and    link my programs against it, but it doesn't work</em>    <p>There are many different ways to accomplish this.  Here is one way to    do this under Linux.  Suppose your home directory is <code>/home/user.    </code>Then:</p>    <ul>      <li>Create a subdirectory, let's call it <code>myxml</code></li>      <li>unpack the libxml2 distribution into that subdirectory</li>      <li>chdir into the unpacked distribution        (<code>/home/user/myxml/libxml2 </code>)</li>      <li>configure the library using the "<code>--prefix</code>" switch,        specifying an installation subdirectory in        <code>/home/user/myxml</code>, e.g.        <p><code>./configure --prefix /home/user/myxml/xmlinst</code> {other        configuration options}</p>      </li>      <li>now run <code>make</code> followed by <code>make install</code></li>      <li>At this point, the installation subdirectory contains the complete        "private" include files, library files and binary program files (e.g.        xmllint), located in        <p><code>/home/user/myxml/xmlinst/lib,        /home/user/myxml/xmlinst/include </code> and <code>        /home/user/myxml/xmlinst/bin</code></p>        respectively.</li>      <li>In order to use this "private" library, you should first add it to        the beginning of your default PATH (so that your own private program        files such as xmllint will be used instead of the normal system        ones).  To do this, the Bash command would be        <p><code>export PATH=/home/user/myxml/xmlinst/bin:$PATH</code></p>      </li>      <li>Now suppose you have a program <code>test1.c</code> that you would        like to compile with your "private" library.  Simply compile it using        the command        <p><code>gcc `xml2-config --cflags --libs` -o test test.c</code></p>        Note that, because your PATH has been set with <code>        /home/user/myxml/xmlinst/bin</code> at the beginning, the xml2-config        program which you just installed will be used instead of the system        default one, and this will <em>automatically</em> get the correct        libraries linked with your program.</li>    </ul>  </li>   <p></p>  <li><em>xmlDocDump() generates output on one line.</em>    <p>Libxml2 will not <strong>invent</strong> spaces in the content of a    document since <strong>all spaces in the content of a document are    significant</strong>. If you build a tree from the API and want    indentation:</p>    <ol>      <li>the correct way is to generate those yourself too.</li>      <li>the dangerous way is to ask libxml2 to add those blanks to your        content <strong>modifying the content of your document in the        process</strong>. The result may not be what you expect. There is        <strong>NO</strong> way to guarantee that such a modification won't        affect other parts of the content of your document. See <a        href="http://xmlsoft.org/html/libxml-parser.html#xmlKeepBlanksDefault">xmlKeepBlanksDefault        ()</a> and <a        href="http://xmlsoft.org/html/libxml-tree.html#xmlSaveFormatFile">xmlSaveFormatFile        ()</a></li>    </ol>  </li>  <p></p>  <li><em>Extra nodes in the document:</em>    <p><em>For an XML file as below:</em></p>    <pre>&lt;?xml version="1.0"?&gt;&lt;PLAN xmlns="http://www.argus.ca/autotest/1.0/"&gt;&lt;NODE CommFlag="0"/&gt;&lt;NODE CommFlag="1"/&gt;&lt;/PLAN&gt;</pre>    <p><em>after parsing it with the function    pxmlDoc=xmlParseFile(...);</em></p>    <p><em>I want to the get the content of the first node (node with the    CommFlag="0")</em></p>    <p><em>so I did it as following;</em></p>    <pre>xmlNodePtr pnode;pnode=pxmlDoc-&gt;children-&gt;children;</pre>    <p><em>but it does not work. If I change it to</em></p>    <pre>pnode=pxmlDoc-&gt;children-&gt;children-&gt;next;</pre>    <p><em>then it works.  Can someone explain it to me.</em></p>    <p></p>    <p>In XML all characters in the content of the document are significant    <strong>including blanks and formatting line breaks</strong>.</p>    <p>The extra nodes you are wondering about are just that, text nodes with    the formatting spaces which are part of the document but that people tend    to forget. There is a function <a    href="http://xmlsoft.org/html/libxml-parser.html">xmlKeepBlanksDefault    ()</a>  to remove those at parse time, but that's an heuristic, and its    use should be limited to cases where you are certain there is no    mixed-content in the document.</p>  </li>  <li><em>I get compilation errors of existing code like when accessing    <strong>root</strong> or <strong>child fields</strong> of nodes.</em>    <p>You are compiling code developed for libxml version 1 and using a    libxml2 development environment. Either switch back to libxml v1 devel or    even better fix the code to compile with libxml2 (or both) by <a    href="upgrade.html">following the instructions</a>.</p>  </li>  <li><em>I get compilation errors about non existing    <strong>xmlRootNode</strong> or <strong>xmlChildrenNode</strong>    fields.</em>    <p>The source code you are using has been <a    href="upgrade.html">upgraded</a> to be able to compile with both libxml    and libxml2, but you need to install a more recent version:    libxml(-devel) &gt;= 1.8.8 or libxml2(-devel) &gt;= 2.1.0</p>  </li>  <li><em>Random crashes in threaded applications</em>    <p>Read and follow all advices on the <a href="threads.html">thread    safety</a> page, and make 100% sure you never call xmlCleanupParser()    while the library or an XML document might still be in use by another    thread.</p>  </li>  <li><em>The example provided in the web page does not compile.</em>    <p>It's hard to maintain the documentation in sync with the code    &lt;grin/&gt; ...</p>    <p>Check the previous points 1/ and 2/ raised before, and please send    patches.</p>  </li>  <li><em>Where can I get more examples and information than provided on the    web page?</em>    <p>Ideally a libxml2 book would be nice. I have no such plan ... But you    can:</p>    <ul>      <li>check more deeply the <a href="html/libxml-lib.html">existing        generated doc</a></li>      <li>have a look at <a href="examples/index.html">the set of        examples</a>.</li>      <li>look for examples of use for libxml2 function using the Gnome code          or by asking on Google.</li>      <li><a        href="http://svn.gnome.org/viewvc/libxml2/trunk/">Browse        the libxml2 source</a> , I try to write code as clean and documented        as possible, so looking at it may be helpful. In particular the code        of <a href="http://svn.gnome.org/viewvc/libxml2/trunk/xmllint.c?view=markup">xmllint.c</a> and of the various testXXX.c test programs should        provide good examples of how to do things with the library.</li>    </ul>  </li>  <p></p>  <li><em>What about C++ ?</em>    <p>libxml2 is written in pure C in order to allow easy reuse on a number    of platforms, including embedded systems. I don't intend to convert to    C++.</p>    <p>There is however a C++ wrapper which may fulfill your needs:</p>    <ul>      <li>by Ari Johnson &lt;ari@btigate.com&gt;:        <p>Website: <a        href="http://libxmlplusplus.sourceforge.net/">http://libxmlplusplus.sourceforge.net/</a></p>        <p>Download: <a        href="http://sourceforge.net/project/showfiles.php?group_id=12999">http://sourceforge.net/project/showfiles.php?group_id=12999</a></p>      </li>    </ul>  </li>  <li><em>How to validate a document a posteriori ?</em>    <p>It is possible to validate documents which had not been validated at    initial parsing time or documents which have been built from scratch    using the API. Use the <a    href="http://xmlsoft.org/html/libxml-valid.html#xmlValidateDtd">xmlValidateDtd()</a>    function. It is also possible to simply add a DTD to an existing    document:</p>    <pre>xmlDocPtr doc; /* your existing document */xmlDtdPtr dtd = xmlParseDTD(NULL, filename_of_dtd); /* parse the DTD */         dtd-&gt;name = xmlStrDup((xmlChar*)"root_name"); /* use the given root */         doc-&gt;intSubset = dtd;        if (doc-&gt;children == NULL) xmlAddChild((xmlNodePtr)doc, (xmlNodePtr)dtd);        else xmlAddPrevSibling(doc-&gt;children, (xmlNodePtr)dtd);          </pre>  </li>  <li><em>So what is this funky "xmlChar" used all the time?</em>    <p>It is a null terminated sequence of utf-8 characters. And only utf-8!    You need to convert strings encoded in different ways to utf-8 before    passing them to the API.  This can be accomplished with the iconv library    for instance.</p>  </li>  <li>etc ...</li></ol> <p></p> <h2><a name="Documentat">Developer Menu</a></h2> <p>There are several on-line resources related to using libxml:</p><ol>  <li>Use the <a href="search.php">search engine</a> to look up  information.</li>  <li>Check the <a href="FAQ.html">FAQ.</a></li>  <li>Check the <a href="http://xmlsoft.org/html/libxml-lib.html">extensive    documentation</a> automatically extracted from code comments.</li>

Lines 1–500 of 5715.

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t