XML::LibXMLã¨ãCPANã¨ã
ã³ã¼ããæ¸ãã¤ã¤ããããããã¦CPANã«ä½¿ãåæãããã¢ã¸ã¥ã¼ã«ããã£ã¦ããããããªããã¨æ¼ãæ¥ã ã
XML::LibXMLã§DOM使ã£ã¦æ¸ãããã¨ãåãã£ãã®ã§ã延ã å¼ã£ã¦ãã
use XML::LibXML;
ã§ãããã
iãã¼ã
#!/usr/bin/perl use strict; use warnings; use XML::LibXML; my $doctype = 'html'; my $system = '-//W3C//DTD XHTML 1.0 Strict//EN'; my $dtd = 'http://www.w3.org/TR/xhtml/DTD/xhtml1-strict.dtd'; my $doc= XML::LibXML::Document->new('1.0','UTF-8'); $doc->createInternalSubset($doctype, $system, $dtd); my $html = $doc->createElement('html'); my $h1 = $doc->createElement('h1'); my $p = $doc->createElement('p'); $p->appendText('sample'); $h1->appendText('Hello, World!'); $doc->importNode($h1); $doc->importNode($p); $html->appendChild($h1); $html->appendChild($p); $doc->setDocumentElement($html); print $doc->toStringHTML();
escã§æããã
:save domsample.pl
:quit
chmod 705 domsample.pl
æ°ãã¤ããã¹ãç¹ã¯ãcreateElementããç´å¾ã®è¦ç´ ã¯$docã®ãªãã«åã¾ã£ã¦ãããã
$doc->importNode();
ããå¾ã§appendãå¯è½ã«ãªããã»ãã®è¨èªã ã¨ãã®ã¾ã¾appendã§ãããããããã¡ãã£ã¨æ°ãã¤ããªãã¨ãããªãã
my $p = $doc->createElement('p'); my $p1 = $doc->createElement('p');
ã¨
my $p = $doc->createElement('p'); my $p1 = $p;
ã®éãã¯ãåè ã¯ãããããç¬ç«ããè¦ç´ ã§ãå¾è ã¯$p1ã$pã®ä¸èº«ãåç §ãã¦ãç¶æ ãå¾è ã®ç¶æ ã§$pã«å¤æ´ãè¡ãã¨$p1ãåãç¶æ ã«ãªãã
$doc->importNode($p); $doc->importNode($p);
ã¯åãè¦ç´ ãè¤è£½ãããããã§ã¯ãªãã
表è¨ä¸è¤è£½ãããã³ãã¼ããããã¦ããããã«è¦ãããããããªããã©ãã¡ã¢ãªä¸ã§ã®æ±ããå®éã©ããªå½¢ã«ãªã£ã¦ããããææ¡ãã¦ããã¹ãã$docã«setDocumentElementããã«ãtoString()ãã¦printããã¨ãããããããããããªãã
my $p = $doc->createElement('p')->appendText('hello');
ã¯ä¸å¯è½ããã®appendTextæã«ã¯æªå®ç¾©ç¶æ ã ããã