xml.html 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Parsing XML</title><link rel="stylesheet" type="text/css" href="../manual.css"><meta name="generator" content="DocBook XSL Stylesheets V1.78.1"><link rel="home" href="index.html" title="neon HTTP/WebDAV client library"><link rel="up" href="api.html" title="Chapter 2. The neon C language interface"><link rel="prev" href="api.html" title="Chapter 2. The neon C language interface"><link rel="next" href="ref.html" title="neon API reference"></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">Parsing XML</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="api.html">Prev</a> </td><th width="60%" align="center">Chapter 2. The neon C language interface</th><td width="20%" align="right"> <a accesskey="n" href="ref.html">Next</a></td></tr></table><hr></div><div class="sect1"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="xml"></a>Parsing XML</h2></div></div></div><p>The neon XML interface is exposed by the
  2. <code class="filename">ne_xml.h</code> header file. This interface gives a
  3. wrapper around the standard <a class="ulink" href="http://www.saxproject.org/" target="_top">SAX</a> API used by XML
  4. parsers, with an additional abstraction, <em class="firstterm">stacked SAX
  5. handlers</em>, and also giving consistent <a class="ulink" href="http://www.w3.org/TR/REC-xml-names" target="_top">XML Namespace</a> support.</p><div class="sect2"><div class="titlepage"><div><div><h3 class="title"><a name="xml-sax"></a>Introduction to SAX</h3></div></div></div><p>A SAX-based parser works by emitting a sequence of
  6. <em class="firstterm">events</em> to reflect the tokens being parsed
  7. from the XML document. For example, parsing the following document
  8. fragment:
  9. </p><pre class="programlisting">
  10. &lt;hello&gt;world&lt;/hello&gt;
  11. </pre><p>
  12. results in the following events:
  13. </p><div class="orderedlist"><ol class="orderedlist" type="1"><li class="listitem"><span class="emphasis"><em>start-element</em></span> "hello"</li><li class="listitem"><span class="emphasis"><em>character-data</em></span> "world"</li><li class="listitem"><span class="emphasis"><em>end-element</em></span> "hello"</li></ol></div><p>
  14. This example demonstrates the three event types used used in the
  15. subset of SAX exposed by the neon XML interface: <span class="emphasis"><em>start-element</em></span>,
  16. <span class="emphasis"><em>character-data</em></span> and <span class="emphasis"><em>end-element</em></span>. In a C API, an <span class="quote">“<span class="quote">event</span>”</span> is
  17. implemented as a function callback; three callback types are used in
  18. neon, one for each type of event.</p></div><div class="sect2"><div class="titlepage"><div><div><h3 class="title"><a name="xml-stacked"></a>Stacked SAX handlers</h3></div></div></div><p>WebDAV property values are represented as fragments of XML,
  19. transmitted as parts of larger XML documents over HTTP (notably in
  20. the body of the response to a <code class="literal">PROPFIND</code> request).
  21. When neon parses such documents, the SAX events generated for
  22. these property value fragments may need to be handled by the
  23. application, since neon has no knowledge of the structure of
  24. properties used by the application.</p><p>To solve this problem<a href="#ftn.foot.xml.sax" class="footnote" name="foot.xml.sax"><sup class="footnote">[1]</sup></a> the neon XML interface introduces
  25. the concept of a <em class="firstterm">SAX handler</em>. A SAX handler
  26. comprises a <span class="emphasis"><em>start-element</em></span>, <span class="emphasis"><em>character-data</em></span> and <span class="emphasis"><em>end-element</em></span> callback; the
  27. <span class="emphasis"><em>start-element</em></span> callback being defined such that each handler may
  28. <span class="emphasis"><em>accept</em></span> or <span class="emphasis"><em>decline</em></span> the
  29. <span class="emphasis"><em>start-element</em></span> event. Handlers are composed into a <em class="firstterm">handler
  30. stack</em> before parsing a document. When a new <span class="emphasis"><em>start-element</em></span>
  31. event is generated by the XML parser, neon invokes each <span class="emphasis"><em>start-element</em></span>
  32. callback in the handler stack in turn until one accepts the event.
  33. The handler which accepts the event will then be subsequently be
  34. passed <span class="emphasis"><em>character-data</em></span> events if the element contains character data,
  35. followed by an <span class="emphasis"><em>end-element</em></span> event when the element is closed. If no
  36. handler in the stack accepts a <span class="emphasis"><em>start-element</em></span> event, the branch of the
  37. tree is ignored.</p><p>To illustrate, given a handler A, which accepts the
  38. <code class="literal">cat</code> and <code class="literal">age</code> elements, and a
  39. handler B, which accepts the <code class="literal">name</code> element, the
  40. following document:
  41. </p><div class="example"><a name="xml-example"></a><p class="title"><b>Example 2.1. An example XML document</b></p><div class="example-contents"><pre class="programlisting">
  42. &lt;cat&gt;
  43. &lt;age&gt;3&lt;/age&gt;
  44. &lt;name&gt;Bob&lt;/name&gt;
  45. &lt;/cat&gt;
  46. </pre></div></div><p><br class="example-break">
  47. would be parsed as follows:
  48. </p><div class="orderedlist"><ol class="orderedlist" type="1"><li class="listitem">A <span class="emphasis"><em>start-element</em></span> "cat" → <span class="emphasis"><em>accept</em></span></li><li class="listitem">A <span class="emphasis"><em>start-element</em></span> "age" → <span class="emphasis"><em>accept</em></span></li><li class="listitem">A <span class="emphasis"><em>character-data</em></span> "3"</li><li class="listitem">A <span class="emphasis"><em>end-element</em></span> "age"</li><li class="listitem">A <span class="emphasis"><em>start-element</em></span> "name" → <span class="emphasis"><em>decline</em></span></li><li class="listitem">B <span class="emphasis"><em>start-element</em></span> "name" → <span class="emphasis"><em>accept</em></span></li><li class="listitem">B <span class="emphasis"><em>character-data</em></span> "Bob"</li><li class="listitem">B <span class="emphasis"><em>end-element</em></span> "name"</li><li class="listitem">A <span class="emphasis"><em>end-element</em></span> "cat"</li></ol></div><p>The search for a handler which will accept a <span class="emphasis"><em>start-element</em></span> event
  49. begins at the handler of the parent element and continues toward the
  50. top of the stack. For the root element, it begins at the base of
  51. the stack. In the above example, handler A is at the base, and
  52. handler B at the top; if the <code class="literal">name</code> element had any
  53. children, only B's <span class="emphasis"><em>start-element</em></span> would be invoked to accept
  54. them.</p></div><div class="sect2"><div class="titlepage"><div><div><h3 class="title"><a name="xml-state"></a>Maintaining state</h3></div></div></div><p>To facilitate communication between independent handlers, a
  55. <em class="firstterm">state integer</em> is associated with each element
  56. being parsed. This integer is returned by <span class="emphasis"><em>start-element</em></span> callback and
  57. is passed to the subsequent <span class="emphasis"><em>character-data</em></span> and <span class="emphasis"><em>end-element</em></span> callbacks
  58. associated with the element. The state integer of the parent
  59. element is also passed to each <span class="emphasis"><em>start-element</em></span> callback, the value zero
  60. used for the root element (which by definition has no
  61. parent).</p><p>To further extend <a class="xref" href="xml.html#xml-example" title="Example 2.1. An example XML document">Example 2.1, “An example XML document”</a>: if handler A
  62. defines that the state of the root element <code class="sgmltag-element">cat</code>
  63. will be <code class="literal">42</code>, the event trace would be as
  64. follows:
  65. </p><div class="orderedlist"><ol class="orderedlist" type="1"><li class="listitem">A <span class="emphasis"><em>start-element</em></span> (parent = 0, "cat") →
  66. <span class="emphasis"><em>accept</em></span>, state = 42
  67. </li><li class="listitem">A <span class="emphasis"><em>start-element</em></span> (parent = 42, "age") →
  68. <span class="emphasis"><em>accept</em></span>, state = 50
  69. </li><li class="listitem">A <span class="emphasis"><em>character-data</em></span> (state = 50, "3")</li><li class="listitem">A <span class="emphasis"><em>end-element</em></span> (state = 50, "age")</li><li class="listitem">A <span class="emphasis"><em>start-element</em></span> (parent = 42, "name") →
  70. <span class="emphasis"><em>decline</em></span></li><li class="listitem">B <span class="emphasis"><em>start-element</em></span> (parent = 42, "name") →
  71. <span class="emphasis"><em>accept</em></span>, state = 99</li><li class="listitem">B <span class="emphasis"><em>character-data</em></span> (state = 99, "Bob")</li><li class="listitem">B <span class="emphasis"><em>end-element</em></span> (state = 99, "name")</li><li class="listitem">A <span class="emphasis"><em>end-element</em></span> (state = 42, "cat")</li></ol></div><p>To avoid collisions between state integers used by different
  72. handlers, the interface definition of any handler includes the range
  73. of integers it will use.</p></div><div class="sect2"><div class="titlepage"><div><div><h3 class="title"><a name="xml-ns"></a>XML namespaces</h3></div></div></div><p>To support XML namespaces, every element name is represented
  74. as a <span class="emphasis"><em>(namespace, name)</em></span> pair. The <span class="emphasis"><em>start-element</em></span>
  75. and <span class="emphasis"><em>end-element</em></span> callbacks are passed namespace and name strings
  76. accordingly. If an element in the XML document has no declared
  77. namespace, the namespace given will be the empty string,
  78. <code class="literal">""</code>.</p></div><div class="footnotes"><br><hr style="width:100; text-align:left;margin-left: 0"><div id="ftn.foot.xml.sax" class="footnote"><p><a href="#foot.xml.sax" class="para"><sup class="para">[1] </sup></a>This
  79. <span class="quote">“<span class="quote">problem</span>”</span> only needs solving because the SAX interface
  80. is so inflexible when implemented as C function callbacks; a better
  81. approach would be to use an XML parser interface which is not based
  82. on callbacks.</p></div></div></div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="api.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="api.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="ref.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">Chapter 2. The neon C language interface </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> neon API reference</td></tr></table></div></body></html>