home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2004 December / PCpro_2004_12.ISO / files / webserver / tsw / TSW_3.4.0.exe / Apache2 / perl / Document.pod < prev    next >
Encoding:
Text File  |  2003-08-22  |  16.7 KB  |  592 lines

  1. =head1 NAME
  2.  
  3. XML::LibXML::Document - XML::LibXML DOM Document Class
  4.  
  5. =head1 SYNOPSIS
  6.  
  7.   $dom = XML::LibXML::Document->new( $version, $encoding );
  8.   $dom = XML::LibXML::Document->createDocument( $version, $encoding );
  9.   $strEncoding = $doc->encoding();
  10.   $doc->setEncoding($new_encoding);
  11.   $strVersion = $doc->version();
  12.   $doc->standalone
  13.   $doc->setStandalone($numvalue);
  14.   my $compression = $doc->compression;
  15.   $doc->setCompression($ziplevel);
  16.   $docstring = $dom->toString($format);
  17.   $c14nstr = $doc->toStringC14N($comment_flag,$xpath); 
  18.   $str = $doc->serialze($format); 
  19.   $c14nstr = $doc->serialize_c14n($comment_flag,$xpath); 
  20.   $state = $doc->toFile($filename, $format);
  21.   $state = $doc->toFH($fh, $format);
  22.   $str = $document->toStringHTML();
  23.   $str = $document->serialize_html();
  24.   $bool = $dom->is_valid();
  25.   $dom->validate();
  26.   $root = $dom->documentElement();
  27.   $dom->setDocumentElement( $root );
  28.   $element = $dom->createElement( $nodename );
  29.   $element = $dom->createElementNS( $namespaceURI, $qname );
  30.   $text = $dom->createTextNode( $content_text );
  31.   $comment = $dom->createComment( $comment_text );
  32.   $attrnode = $doc->createAttribute($name [,$value]);
  33.   $attrnode = $doc->createAttributeNS( namespaceURI, $name [,$value] );
  34.   $fragment = $doc->createDocumentFragment()
  35.   $cdata = $dom->create( $cdata_content );
  36.   my $pi = $doc->createProcessingInstruction( $target, $data );
  37.   my $entref = $doc->createEntityReference($refname);
  38.   $dtd = $document->createInternalSubset( $rootnode, $public, $system);
  39.   $dtd = $document->createExternalSubset( $rootnode, $public, $system);
  40.   $document->importNode( $node );
  41.   $document->adoptNode( $node );
  42.   my $dtd = $doc->externalSubset;
  43.   my $dtd = $doc->internalSubset;
  44.   $doc->setExternalSubset($dtd);
  45.   $doc->setInternalSubset($dtd);
  46.   my $dtd = $doc->removeExternalSubset();
  47.   my $dtd = $doc->removeInternalSubset();
  48.   my @nodelist = $doc->getElementsByTagName($tagname);
  49.   my @nodelist = $doc->getElementsByTagName($nsURI,$tagname);
  50.   my @nodelist = $doc->getElementsByLocalName($localname);
  51.   my $node = $doc->getElementsById($id);
  52.  
  53.  
  54. =head1 DESCRIPTION
  55.  
  56. The Document Class is in most cases the result of a parsing process. But
  57. sometimes it is necessary to create a Document from scratch. The DOM Document
  58. Class provides functions that are conform to the DOM Core naming style.
  59.  
  60. It inherits all functions from XML::LibXML::Node as specified in the DOM
  61. specification. This enables to access the nodes beside the root element on
  62. document level - a DTD for example. The support for these nodes is limited at
  63. the moment.
  64.  
  65. While generaly nodes are bound to a document in the DOM concept it is suggested
  66. that one should always create a node not bound to any document. There is no
  67. need of really including the node to the document, but once the node is bound
  68. to a document, it is quite safe that all strings have the correct encoding. If
  69. an unbound textnode with an iso encoded string is created (e.g. with
  70. $CLASS->new()), the toString function may not return the expected result.
  71.  
  72. All this seems like a limitation as long UTF8 encoding is ashured. If iso
  73. encoded strings come into play it is much safer to use the node creation
  74. functions of XML::LibXML::Document.
  75.  
  76. =over 4
  77.  
  78. =item B<new>
  79.  
  80.   $dom = XML::LibXML::Document->new( $version, $encoding );
  81.  
  82. alias for createDocument()
  83.  
  84.  
  85. =item B<createDocument>
  86.  
  87.   $dom = XML::LibXML::Document->createDocument( $version, $encoding );
  88.  
  89. The constructor for the document class. As Parameter it takes the version
  90. string and (optionally) the ecoding string. Simply calling createDocument()
  91. will create the document:
  92.  
  93.   <?xml version="your version" encoding="your encoding"?>
  94.  
  95. Both parameter are optional. The default value for $version is 1.0, of course.
  96. If the $encoding parameter is not set, the encoding will be left unset, which
  97. means UTF8 is implied.
  98.  
  99. The call of createDocument() without any parameter will result the following
  100. code:
  101.  
  102.   <?xml version="1.0"?>              
  103.  
  104. Alternatively one can call this constructor directly from the XML::LibXML class
  105. level, to avoid some typing. This will not cause any effect to the class
  106. instance, which is alway XML::LibXML::Document.
  107.  
  108.    my $document = XML::LibXML->createDocument( "1.0", "UTF8" );
  109.  
  110. is therefore a shortcut for
  111.  
  112.   my $document = XML::LibXML::Document->createDocument( "1.0", "UTF8" );
  113.  
  114.  
  115. =item B<encoding>
  116.  
  117.   $strEncoding = $doc->encoding();
  118.  
  119. returns the encoding string of the document.
  120.  
  121.   my $doc = XML::LibXML->createDocument( "1.0", "ISO-8859-15" );
  122.   print $doc->encoding; # prints ISO-8859-15
  123.  
  124. Optionally this function can be accessed by actualEncoding or getEncoding.
  125.  
  126.  
  127. =item B<setEncoding>
  128.  
  129.   $doc->setEncoding($new_encoding);
  130.  
  131. From time to time it is useful to change the effective encoding of a document.
  132. This method provides the interface to manipulate the encoding of a document.
  133.  
  134. Note that this function has to be used very careful, since you can't simply
  135. convert one encoding in any other, since some (or even all) characters may not
  136. exist in the new encoding. XML::LibXML will not test if the operation is
  137. allowed or possible for the given document. The only switching ashured to work
  138. is to UTF8.
  139.  
  140.  
  141. =item B<version>
  142.  
  143.   $strVersion = $doc->version();
  144.  
  145. returns the version string of the document
  146.  
  147. getVersion() is an alternative form of this function.
  148.  
  149.  
  150. =item B<standalone>
  151.  
  152.   $doc->standalone
  153.  
  154. This function returns the Numerical value of a documents XML declarations
  155. standalone attribute. It returns 1 if standalone="yes" was found, 0 if
  156. standalone="no" was found and -1 if standalone was not specified (default on
  157. creation).
  158.  
  159.  
  160. =item B<setStandalone>
  161.  
  162.   $doc->setStandalone($numvalue);
  163.  
  164. Through this method it is possible to alter the value of a documents standalone
  165. attribute. Set it to 1 to set standalone="yes", to 0 to set standalone="no" or
  166. set it to -1 to remove the standalone attribute from the XML declaration.
  167.  
  168.  
  169. =item B<compression>
  170.  
  171.   my $compression = $doc->compression;
  172.  
  173. libxml2 allows to read documents directly from gziped files. In this case the
  174. compression variable is set to the compression level of that file (0-8). If
  175. XML::LibXML parsed a different source or the file wasn't compressed, the
  176. returned value will be -1.
  177.  
  178.  
  179. =item B<setCompression>
  180.  
  181.   $doc->setCompression($ziplevel);
  182.  
  183. If one intends to write the document directly to a file, it is possible to set
  184. the compression level for a given document. This level can be in the range from
  185. 0 to 8. If XML::LibXML should not try to compress use -1 (default).
  186.  
  187. Note that this feature will only work if libxml2 is compiled with zlib support
  188. and toFile() is used for output.
  189.  
  190.  
  191. =item B<toString>
  192.  
  193.   $docstring = $dom->toString($format);
  194.  
  195. toString is a deparsing function, so the DOM Tree can be translated into a
  196. string, ready for output.
  197.  
  198. The optional $format parameter sets the indenting of the output. This parameter
  199. is expected to be an integer value, that specifies that indentation should be
  200. used. The format parameter can have three different values if it is used:
  201.  
  202. If $format is 0, than the document is dumped as it was originally parsed
  203.  
  204. If $format is 1, libxml2 will add ignoreable whitespaces, so the nodes content
  205. is easier to read. Existing text nodes will not be altered
  206.  
  207. If $format is 2 (or higher), libxml2 will act as $format == 1 but it add a
  208. leading and a trailing linebreak to each text node.
  209.  
  210. libxml2 uses a hardcoded indentation of 2 space characters per indentation
  211. level. This value can not be altered on runtime.
  212.  
  213. NOTE: XML::LibXML::Document::toString returns the data in the document encoding
  214. rather than UTF8! If you want UTF8 ecoded XML, you have to change the conding
  215. by using setEncoding()
  216.  
  217.  
  218. =item B<toStringC14N>
  219.  
  220. $c14nstr = $doc->toStringC14N($comment_flag,$xpath); A variation to toString,
  221. that returns the canonized from of the given document.
  222.  
  223.  
  224. =item B<serialize>
  225.  
  226.   $str = $doc->serialze($format); 
  227.  
  228. Alternative form of toString(). This function name added to be more conform
  229. with libxml2's examples.
  230.  
  231.  
  232. =item B<serialize_c14n>
  233.  
  234.   $c14nstr = $doc->serialize_c14n($comment_flag,$xpath); 
  235.  
  236. Alternative form of toStringC14N().
  237.  
  238.  
  239. =item B<toFile>
  240.  
  241.   $state = $doc->toFile($filename, $format);
  242.  
  243. This function is similar to toString(), but it writes the document directly
  244. into a filesystem. This function is very usefull, if one needs to store large
  245. documents.
  246.  
  247. The format parameter has the same behaviour as in toString().
  248.  
  249.  
  250. =item B<toFH>
  251.  
  252.   $state = $doc->toFH($fh, $format);
  253.  
  254. This function is similar to toString(), but it writes the document directly to
  255. a filehandler or a stream.
  256.  
  257. The format parameter has the same behaviour as in toString().
  258.  
  259.  
  260. =item B<toStringHTML>
  261.  
  262.   $str = $document->toStringHTML();
  263.  
  264. toStringHTML deparses the tree to a string as HTML. With this method indenting
  265. is automatic and managed by libxml2 internally.
  266.  
  267.  
  268. =item B<serialize_html>
  269.  
  270.   $str = $document->serialize_html();
  271.  
  272. Alternative form of toStringHTML().
  273.  
  274.  
  275. =item B<is_valid>
  276.  
  277.   $bool = $dom->is_valid();
  278.  
  279. Returns either TRUE or FALSE depending on the DOM Tree is a valid Document or
  280. not.
  281.  
  282. You may also pass in a XML::LibXML::Dtd object, to validate against an external
  283. DTD:
  284.  
  285.    if (!$dom->is_valid($dtd)) {
  286.        warn("document is not valid!");
  287.    }
  288.  
  289.  
  290. =item B<validate>
  291.  
  292.   $dom->validate();
  293.  
  294. This is an exception throwing equivalent of is_valid. If the document is not
  295. valid it will throw an exception containing the error. This allows you much
  296. better error reporting than simply is_valid or not.
  297.  
  298. Again, you may pass in a DTD object
  299.  
  300.  
  301. =item B<documentElement>
  302.  
  303.   $root = $dom->documentElement();
  304.  
  305. Returns the root element of the Document. A document can have just one root
  306. element to contain the documents data.
  307.  
  308. Optionaly one can use getDocumentElement.
  309.  
  310.  
  311. =item B<setDocumentElement>
  312.  
  313.   $dom->setDocumentElement( $root );
  314.  
  315. This function enables you to set the root element for a document. The function
  316. supports the import of a node from a different document tree.
  317.  
  318.  
  319. =item B<createElement>
  320.  
  321.   $element = $dom->createElement( $nodename );
  322.  
  323. This function creates a new Element Node bound to the DOM with the name
  324. $nodename.
  325.  
  326.  
  327. =item B<createElementNS>
  328.  
  329.   $element = $dom->createElementNS( $namespaceURI, $qname );
  330.  
  331. This function creates a new Element Node bound to the DOM with the name
  332. $nodename and placed in the given namespace.
  333.  
  334.  
  335. =item B<createTextNode>
  336.  
  337.   $text = $dom->createTextNode( $content_text );
  338.  
  339. As an equivalent of createElement, but it creates a Text Node bound to the DOM.
  340.  
  341.  
  342. =item B<createComment>
  343.  
  344.   $comment = $dom->createComment( $comment_text );
  345.  
  346. As an equivalent of createElement, but it creates a Comment Node bound to the
  347. DOM.
  348.  
  349.  
  350. =item B<createAttribute>
  351.  
  352.   $attrnode = $doc->createAttribute($name [,$value]);
  353.  
  354. Creates a new Attribute node.
  355.  
  356.  
  357. =item B<createAttributeNS>
  358.  
  359.   $attrnode = $doc->createAttributeNS( namespaceURI, $name [,$value] );
  360.  
  361. Creates an Attribute bound to a namespace.
  362.  
  363.  
  364. =item B<createDocumentFragment>
  365.  
  366.   $fragment = $doc->createDocumentFragment()
  367.  
  368. This function creates a DocumentFragment.
  369.  
  370.  
  371. =item B<createCDATASection>
  372.  
  373.   $cdata = $dom->create( $cdata_content );
  374.  
  375. Similar to createTextNode and createComment, this function creates a
  376. CDataSection bound to the current DOM.
  377.  
  378.  
  379. =item B<createProcessingInstruction>
  380.  
  381.   my $pi = $doc->createProcessingInstruction( $target, $data );
  382.  
  383. create a processing instruction node.
  384.  
  385. Since this method is quite long one may use its short form createPI().
  386.  
  387.  
  388. =item B<createEntityReference>
  389.  
  390.   my $entref = $doc->createEntityReference($refname);
  391.  
  392. If a docuemnt has a DTD specified, one can create entity refereferences by
  393. using this function. If one wants to add a entity reference to the document,
  394. this reference has to be created by this function.
  395.  
  396. An entity reference is unique to a document and cannot passed to other
  397. documents as other nodes can be passed.
  398.  
  399. NOTE: A text content containing something that looks like an entity reference,
  400. will not be expanded to a real entity reference unless it is a predefined
  401. entity
  402.  
  403.    my $string = "&foo;";
  404.    $some_element->appendText( $string );
  405.    print $some_element->textContent; # prints "&foo;"
  406.  
  407.  
  408. =item B<createInternalSubset>
  409.  
  410.   $dtd = $document->createInternalSubset( $rootnode, $public, $system);
  411.  
  412. This function creates and adds an internal subset to the given document.
  413. Because the function automaticly adds the DTD to the document there is no need
  414. to add the created node explicitly to the document.
  415.  
  416.    my $document = XML::LibXML::Document->new();
  417.    my $dtd      = $document->createInternalSubset( "foo", undef, "foo.dtd" );
  418.  
  419. will result in the following XML document:
  420.  
  421.   <?xml version="1.0"?>
  422.    <!DOCTYPE foo SYSTEM "foo.dtd">                    
  423.  
  424. By setting the public parameter it is possible to set PUBLIC dtds to a given
  425. document. So
  426.  
  427.   my $document = XML::LibXML::Document->new();
  428.   my $dtd      = $document->createInternalSubset( "foo", "-//FOO//DTD FOO 0.1//EN", undef );
  429.   
  430.  
  431. will cause the following declaration to be created on the document:
  432.  
  433.   <?xml version="1.0"?>
  434.   <!DOCTYPE foo PUBLIC "-//FOO//DTD FOO 0.1//EN">
  435.  
  436.  
  437. =item B<createExternalSubset>
  438.  
  439.   $dtd = $document->createExternalSubset( $rootnode, $public, $system);
  440.  
  441. This function is similar to createInternalSubset() but this DTD is concidered
  442. to be external and is therefore not added to the document itself. Nevertheless
  443. it can be used for validation purposes.
  444.  
  445.  
  446. =item B<importNode>
  447.  
  448.   $document->importNode( $node );
  449.  
  450. If a node is not part of a document, it can be imported to another document. As
  451. specified in DOM Level 2 Specification the Node will not be altered or removed
  452. from its original document ($node->cloneNode(1) will get called implicitly).
  453.  
  454. NOTE: Don't try to use importNode() to import subtrees that contain an entity
  455. reference - even if the entity reference is the root node of the subtree. This
  456. will cause serious problems to your program. This is a limitation of libxml2
  457. and not of XML::LibXML itself.
  458.  
  459.  
  460. =item B<adoptNode>
  461.  
  462.   $document->adoptNode( $node );
  463.  
  464. If a node is not part of a document, it can be imported to another document. As
  465. specified in DOM Level 3 Specification the Node will not be altered but it will
  466. removed from its original document.
  467.  
  468. After a document adopted a node, the node, its attributes and all its
  469. descendants belong to the new document. Because the node does not belong to the
  470. old document, it will be unlinked from its old location first.
  471.  
  472. NOTE: Don't try to adoptNode() to import subtrees that contain entity
  473. references - even if the entity reference is the root node of the subtree. This
  474. will cause serious problems to your program. This is a limitation of libxml2
  475. and not of XML::LibXML itself.
  476.  
  477.  
  478. =item B<externalSubset>
  479.  
  480.   my $dtd = $doc->externalSubset;
  481.  
  482. If a document has an external subset defined it will be returned by this
  483. function.
  484.  
  485. NOTE Dtd nodes are no ordinary nodes in libxml2. The support for these nodes in
  486. XML::LibXML is still limited. In particular one may not want use common node
  487. function on doctype declaration nodes!
  488.  
  489.  
  490. =item B<internalSubset>
  491.  
  492.   my $dtd = $doc->internalSubset;
  493.  
  494. If a document has an internal subset defined it will be returned by this
  495. function.
  496.  
  497. NOTE Dtd nodes are no ordinary nodes in libxml2. The support for these nodes in
  498. XML::LibXML is still limited. In particular one may not want use common node
  499. function on doctype declaration nodes!
  500.  
  501.  
  502. =item B<setExternalSubset>
  503.  
  504.   $doc->setExternalSubset($dtd);
  505.  
  506. EXPERIMENTAL!
  507.  
  508. This method sets a DTD node as an external subset of the given document.
  509.  
  510.  
  511. =item B<setInternalSubset>
  512.  
  513.   $doc->setInternalSubset($dtd);
  514.  
  515. EXPERIMENTAL!
  516.  
  517. This method sets a DTD node as an internal subset of the given document.
  518.  
  519.  
  520. =item B<removeExternalSubset>
  521.  
  522.   my $dtd = $doc->removeExternalSubset();
  523.  
  524. EXPERIMENTAL!
  525.  
  526. If a document has an external subset defined it can be removed from the
  527. document by using this function. The removed dtd node will be returned.
  528.  
  529.  
  530. =item B<removeInternalSubset>
  531.  
  532.   my $dtd = $doc->removeInternalSubset();
  533.  
  534. EXPERIMENTAL!
  535.  
  536. If a document has an internal subset defined it can be removed from the
  537. document by using this function. The removed dtd node will be returned.
  538.  
  539.  
  540. =item B<getElementsByTagName>
  541.  
  542.   my @nodelist = $doc->getElementsByTagName($tagname);
  543.  
  544. Implements the DOM Level 2 function
  545.  
  546. In SCALAR context this function returns a XML::LibXML::NodeList object.
  547.  
  548.  
  549. =item B<getElementsByTagNameNS>
  550.  
  551.   my @nodelist = $doc->getElementsByTagName($nsURI,$tagname);
  552.  
  553. Implements the DOM Level 2 function
  554.  
  555. In SCALAR context this function returns a XML::LibXML::NodeList object.
  556.  
  557.  
  558. =item B<getElementsByLocalName>
  559.  
  560.   my @nodelist = $doc->getElementsByLocalName($localname);
  561.  
  562. This allows to fetch all nodes from a given document with the given Localname.
  563.  
  564. In SCALAR context this function returns a XML::LibXML::NodeList object.
  565.  
  566.  
  567. =item B<getElementsById>
  568.  
  569.   my $node = $doc->getElementsById($id);
  570.  
  571. This allows to fetch the node at a given position in the DOM.
  572.  
  573. Note: The Id of a node might change while manipulating the document.
  574.  
  575.  
  576.  
  577. =back
  578.  
  579. =head1 AUTHORS
  580.  
  581. Matt Sergeant, 
  582. Christian Glahn, 
  583. =head1 VERSION
  584.  
  585. 1.56
  586.  
  587. =head1 COPYRIGHT
  588.  
  589. 2001-2002, AxKit.com Ltd; 2001-2003 Christian Glahn, All rights reserved.
  590.  
  591. =cut
  592.