📄 ch07_01.htm
字号:
<html><head><title>DOM (Perl and XML)</title><link rel="stylesheet" type="text/css" href="../style/style1.css" /><meta name="DC.Creator" content="Erik T. Ray and Jason McIntosh" /><meta name="DC.Format" content="text/xml" scheme="MIME" /><meta name="DC.Language" content="en-US" /><meta name="DC.Publisher" content="O'Reilly & Associates, Inc." /><meta name="DC.Source" scheme="ISBN" content="059600205XL" /><meta name="DC.Subject.Keyword" content="stuff" /><meta name="DC.Title" content="Perl and XML" /><meta name="DC.Type" content="Text.Monograph" /></head><body bgcolor="#ffffff"><img alt="Book Home" border="0" src="gifs/smbanner.gif" usemap="#banner-map" /><map name="banner-map"><area shape="rect" coords="1,-2,616,66" href="index.htm" alt="Perl & XML" /><area shape="rect" coords="629,-11,726,25" href="jobjects/fsearch.htm" alt="Search this book" /></map><div class="navbar"><table width="684" border="0"><tr><td align="left" valign="top" width="228"><a href="ch06_06.htm"><img alt="Previous" border="0" src="../gifs/txtpreva.gif" /></a></td><td align="center" valign="top" width="228" /><td align="right" valign="top" width="228"><a href="ch07_02.htm"><img alt="Next" border="0" src="../gifs/txtnexta.gif" /></a></td></tr></table></div><h1 class="chapter">Chapter 7. DOM</h1><div class="htmltoc"><h4 class="tochead">Contents:</h4> <p> <a href="#perlxml-CHP-7-SECT-1">DOM and Perl</a><br /><a href="ch07_02.htm">DOM Class Interface Reference</a><br /><a href="ch07_03.htm">XML::DOM</a><br /><a href="ch07_04.htm">XML::LibXML</a><br /></p></div><p>In this chapter, we return to standard APIs with the <a name="INDEX-518" /></a>Document Object Model(DOM). In <a href="ch05_01.htm">Chapter 5, "SAX"</a>, we talked about the benefitsof using standard APIs: increased compatibility with other softwarecomponents and (if implemented correctly) a guaranteed completesolution. The same concept applies in this chapter: what SAX does forevent streams, DOM does for tree processing.</p><div class="sect1"><a name="perlxml-CHP-7-SECT-1" /></a><h2 class="sect1">7.1. DOM and Perl</h2><p>DOM is a recommendation by the <a name="INDEX-519" /></a> <a name="INDEX-520" /></a>World Wide Web Consortium(W3C). Designed to be a language-neutral interface to an in-memoryrepresentation of an XML document, versions of DOM are available in<a name="INDEX-521" /></a>Java,<a name="INDEX-522" /></a>ECMAscript,<a href="#FOOTNOTE-27">[27]</a> Perl, and other languages. Perl alonehas several implementations of DOM,including<a name="INDEX-523" /></a><tt class="literal">XML::DOM</tt> and<a name="INDEX-524" /></a> <tt class="literal">XML::LibXML</tt>.</p><blockquote class="footnote"><a name="FOOTNOTE-27" /></a><p>[27]A standards-friendly language patterned afterJavaScript.</p> </blockquote><p>While SAX defines an interface of handler methods, the DOMspecification calls for a number of classes, each with an interfaceof methods that affect a particular type of XML markup. Thus, everyobject instance manages a portion of the document tree, providingaccessor methods to add, remove, or modify nodes and data. Theseobjects are typically created by a<a name="INDEX-525" /></a> <em class="emphasis">factory object</em>,making it a little easier for programmers who only have to initializethe factory object themselves.</p><p>In DOM, every piece of XML (the element, text, comment, etc.) is anode represented by a <tt class="literal">Node</tt><a name="INDEX-526" /></a> object. The <tt class="literal">Node</tt>class is extended by more specific classes that represent the typesof XML markup, including <tt class="literal">Element</tt>,<tt class="literal">Attr</tt> (attribute),<tt class="literal">ProcessingInstruction</tt>, <tt class="literal">Comment</tt>,<tt class="literal">EntityReference</tt>, <tt class="literal">Text</tt>,<tt class="literal">CDATASection,</tt> and <tt class="literal">Document</tt>.These classes are the building blocks of every XML tree in DOM.</p><p>The standard also calls for a couple of classes that serve ascontainers for nodes, convenient for shuttling XML fragments fromplace to place. These classes are <tt class="literal">NodeList</tt>, anordered list of nodes, like all the children of an element; and<tt class="literal">NamedNodeMap,</tt> an unordered set of nodes. Theseobjects are frequently required as arguments or given as returnvalues from methods. Note that these objects are all<em class="emphasis">live</em>, meaning that any changes done to them willimmediately affect the nodes in the document itself, rather than acopy.</p><p>When naming these classes and their methods, DOM merely specifies theoutward appearance of an implementation, but leaves the internalspecifics up to the developer. Particulars like memory management,data structures, and algorithms are not addressed at all, as thoseissues may vary among programming languages and the needs of users.This is like describing a key so a locksmith can make a lock that itwill fit into; you know the key will unlock the door, but you have noidea how it really works. Specifically, the outward appearance makesit easy to write extensions to legacy modules so they can comply withthe standard, but it does not guarantee efficiency or speed.</p><p>DOM is a very large standard, and you will find that implementationsvary in their level of compliance. To make things worse, the standardhas not one, but two (soon to be three) levels. DOM1 has been aroundsince 1998, DOM2 emerged more recently, and they'realready working on a third. The main difference between Levels 1 and2 is that the latter adds support for namespaces. If youaren't concerned about namespaces, then DOM1 shouldbe suitable for your needs.</p></div><hr width="684" align="left" /><div class="navbar"><table width="684" border="0"><tr><td align="left" valign="top" width="228"><a href="ch06_06.htm"><img alt="Previous" border="0" src="../gifs/txtpreva.gif" /></a></td><td align="center" valign="top" width="228"><a href="index.htm"><img alt="Home" border="0" src="../gifs/txthome.gif" /></a></td><td align="right" valign="top" width="228"><a href="ch07_02.htm"><img alt="Next" border="0" src="../gifs/txtnexta.gif" /></a></td></tr><tr><td align="left" valign="top" width="228">6.6. XML::Grove</td><td align="center" valign="top" width="228"><a href="index/index.htm"><img alt="Book Index" border="0" src="../gifs/index.gif" /></a></td><td align="right" valign="top" width="228">7.2. DOM Class Interface Reference</td></tr></table></div><hr width="684" align="left" /><img alt="Library Navigation Links" border="0" src="../gifs/navbar.gif" usemap="#library-map" /><p><p><font size="-1"><a href="copyrght.htm">Copyright © 2002</a> O'Reilly & Associates. All rights reserved.</font></p><map name="library-map"><area shape="rect" coords="1,0,85,94" href="../index.htm"><area shape="rect" coords="86,1,178,103" href="../lwp/index.htm"><area shape="rect" coords="180,0,265,103" href="../lperl/index.htm"><area shape="rect" coords="267,0,353,105" href="../perlnut/index.htm"><area shape="rect" coords="354,1,446,115" href="../prog/index.htm"><area shape="rect" coords="448,0,526,132" href="../tk/index.htm"><area shape="rect" coords="528,1,615,119" href="../cookbook/index.htm"><area shape="rect" coords="617,0,690,135" href="../pxml/index.htm"></map></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -