PHP
downloads | documentation | faq | getting help | mailing lists | wiki | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

DOMDocument::__construct> <DOMComment::__construct
Last updated: Fri, 05 Dec 2008

view this page in

La classe DOMDocument

Introduction

Représente un document HTML ou XML entier ; ce sera la racine de l'arbre document.

Synopsis de la classe

DOMDocument
DOMDocument étend DOMNode {
/* Propriétés */
readonly public string $actualEncoding ;
readonly public DOMConfiguration $config ;
readonly public DOMDocumentType $doctype ;
readonly public DOMElement $documentElement ;
public string $documentURI ;
public string $encoding ;
public bool $formatOutput ;
public bool $preserveWhiteSpace =true ;
public bool $recover ;
public bool $resolveExternals ;
public bool $standalone ;
public bool $strictErrorChecking =true ;
public bool $substituteEntities ;
public bool $validateOnParse =false ;
public string $version ;
readonly public string $xmlEncoding ;
public bool $xmlStandalone ;
public string $xmlVersion ;
/* Méthodes */
DOMDocument::__construct ([ string $version [, string $encoding ]] )
DOMAttr DOMDocument::createAttribute ( string $name )
DOMAttr DOMDocument::createAttributeNS ( string $namespaceURI , string $qualifiedName )
DOMCDATASection DOMDocument::createCDATASection ( string $data )
DOMComment DOMDocument::createComment ( string $data )
DOMDocumentFragment DOMDocument::createDocumentFragment ( void )
DOMElement DOMDocument::createElement ( string $name [, string $value ] )
DOMElement DOMDocument::createElementNS ( string $namespaceURI , string $qualifiedName [, string $value ] )
DOMEntityReference DOMDocument::createEntityReference ( string $name )
DOMProcessingInstruction DOMDocument::createProcessingInstruction ( string $target [, string $data ] )
DOMText DOMDocument::createTextNode ( string $content )
DOMElement DOMDocument::getElementById ( string $elementId )
DOMNodeList DOMDocument::getElementsByTagName ( string $name )
DOMNodeList DOMDocument::getElementsByTagNameNS ( string $namespaceURI , string $localName )
DOMNode DOMDocument::importNode ( DOMNode $importedNode [, bool $deep ] )
mixed DOMDocument::load ( string $filename [, int $options ] )
bool DOMDocument::loadHTML ( string $source )
bool DOMDocument::loadHTMLFile ( string $filename )
mixed DOMDocument::loadXML ( string $source [, int $options ] )
bool DOMDocument::registerNodeClass ( string $baseclass , string $extendedclass )
bool DOMDocument::relaxNGValidate ( string $filename )
bool DOMDocument::relaxNGValidateSource ( string $source )
int DOMDocument::save ( string $filename [, int $options ] )
string DOMDocument::saveHTML ( void )
int DOMDocument::saveHTMLFile ( string $filename )
string DOMDocument::saveXML ([ DOMNode $node [, int $options ]] )
bool DOMDocument::schemaValidate ( string $filename )
bool DOMDocument::schemaValidateSource ( string $source )
bool DOMDocument::validate ( void )
int DOMDocument::xinclude ([ int $options ] )
/* Méthodes héritées */
DOMNode DOMNode::appendChild ( DOMNode $newnode )
DOMNode DOMNode::cloneNode ([ bool $deep ] )
bool DOMNode::hasAttributes ( void )
bool DOMNode::hasChildNodes ( void )
DOMNode DOMNode::insertBefore ( DOMNode $newnode [, DOMNode $refnode ] )
bool DOMNode::isDefaultNamespace ( string $namespaceURI )
bool DOMNode::isSupported ( string $feature , string $version )
string DOMNode::lookupNamespaceURI ( string $prefix )
string DOMNode::lookupPrefix ( string $namespaceURI )
void DOMNode::normalize ( void )
DOMNode DOMNode::removeChild ( DOMNode $oldnode )
DOMNode DOMNode::replaceChild ( DOMNode $newnode , DOMNode $oldnode )
}

Propriétés

actualEncoding

Obsolète. L'encodage actuel du document, en lecture seule, équivalent àencoding.

config

Obsolète. Configuration utilisée lorsque DOMDocument::normalizeDocument() est appelé.

doctype

Le doctype associé au document.

documentElement

C'est un attribut de convenence, qui permet un accès direct au noeud fils, qui est l'élément document du document.

documentURI

La localisation du document, ou NULL si indéfini.

encoding

L'encodage du document, tel que spécifié par la déclaration XML. Cet attribut n'est pas présent dans la spécification DOM Level 3 finale, mais représente la seule façon de manipuler l'encodage du document XML dans cette implémentation.

formatOutput

Formate la sortie avec une jolie indentation et des espaces supplémentaires.

implementation

L'objet DOMImplementation qui gère ce document.

preserveWhiteSpace

Ne pas supprimer les espaces redondants. Vaut par défaut TRUE.

recover

Propriétaire. Active le mode "recovery", i.e. tente d'analyser un document mal formé. Cet attribut ne fait pas parti de la spécification DOM et est spécifique à libxml.

resolveExternals

Définissez-le à TRUE pour charger des entités externes depuis la déclaration doctype. C'est utile pour inclure des entités dans vos documents XML.

standalone

Obsolète. Si le document est "standalone", ou non, tel que spécifié par la déclaration XML, correspondant à xmlStandalone.

strictErrorChecking

Lance une DOMException en cas d'erreur. Par défaut, vaut TRUE.

substituteEntities

Propriétaire. Si l'on doit ou non substituer les entités. Cet attribut ne fait pas parti de la spécification DOM et est spécifique à libxml.

validateOnParse

Charge et valide la DTD. Par défaut, vaut FALSE.

version

Obsolète. Version du XML, correspond à xmlVersion

xmlEncoding

Un attribut spécifiant l'encodage du document. Il vaut NULL lorsque l'encodage n'est pas spécifié, ou lorsqu'il est inconnu, comme c'est le cas lorsque le document a été créé en mémoire.

xmlStandalone

Un attribut spécifiant si le document est "standalone". Il vaut FALSE lorsque non spécifié.

xmlVersion

Un attribut spécifiant le numéro de version du document. S'il n'y a pas de déclaration et si le document supporte la fonctionnalité "XML", la valeur sera "1.0".

Sommaire



DOMDocument::__construct> <DOMComment::__construct
Last updated: Fri, 05 Dec 2008
 
add a note add a note User Contributed Notes
DOMDocument
Atanas Markov (dreamer79bg at gmail dot com)
30-Nov-2008 07:59
Here is a simple web scraping example using the PHP DOM that tries to get the largest text body of a HTML document. I needed it for a spider that had to show a short description for a page. It assumes that document annotation can be the largest <div>, <td> or <p> element in the page.
In the example I show a way to prevent a bug in the DOM as it sometimes just doesn't recognize html encoding. It seems to work if you put charset meta tag right after the head tag of the document.

<?php
$ch
= curl_init();
curl_setopt ($ch, CURLOPT_URL, '...put url here...' );
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch,CURLOPT_VERBOSE,1);
curl_setopt($ch, CURLOPT_USERAGENT, 'set sth...');
curl_setopt ($ch, CURLOPT_REFERER, '...set sth...'); //just a fake referer
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch,CURLOPT_POST,0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 20);

$html= curl_exec($ch);
$html1= curl_getinfo($ch);

//try to get page encoding as it was sent from server
if ($html1['content_type']){
   
$arr= explode('charset=',$html1['content_type']);
   
$csethdr= strtolower(trim($arr[1]));
} else {
   
$csethdr= false;
}

$cset= false;
$arr= array();

//This has to replace page meta tags for charset with utf-8, but it doesn't actually help(see the bug info).
if (preg_match_all(
'/(<meta\s*http-equiv="Content-Type"\s*content="[^;]*;
\s*charset=([^"]*?)(?:"|\;)[^>]*>)/'
//merge this line
,$html,$arr,PREG_PATTERN_ORDER)){
   
$cset= strtolower(trim($arr[2][0]));
    if (
$cset!='utf-8'||$cset!=$csethdr){
       
$new= str_replace($arr[2][0],'utf-8',$arr[1][0]);
       
$html= str_replace($arr[1][0],$new,$html);
       
$cset= $csethdr;
    } else {
       
$cset= false;
    }

    if (
$cset=='utf-8'){
       
$cset= false;
    }
}
unset(
$arr);
if (
$cset){
   
$html= iconv($cset,'utf-8',$html);
}
unset(
$cset);

//solve dom bug
$html=preg_replace('/<head[^>]*>/','<head>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=utf-8">
'
,$html);

$dom= new DOMDocument();
$dom->loadHTML($html);
$dom->preserveWhiteSpace = false;

function
getMaxTextBody($dom){
   
$content = $dom->getElementsByTagname('div');
   
$content2= $dom->getElementsByTagname('td');
   
$content3= $dom->getElementsByTagname('p');

   
$new= array();
    foreach (
$content as $value) {
       
$new[]= $value;
        unset(
$value);
    }
    unset(
$content);

    foreach (
$content2 as $value) {
       
$new[]= $value;
        unset(
$value);
    }
    unset(
$content2);

    foreach (
$content3 as $value) {
       
$new[]= $value;
        unset(
$value);
    }
    unset(
$content3);

   
$maxlen= 0;
   
$result= '';
    foreach (
$new as $item)
    {
       
$str= $item->nodeValue;
        if (
strlen($str)>$maxlen){
           
$content1= $item->getElementsByTagName('div');
           
$content2= $item->getElementsByTagname('td');
                       
$content3= $item->getElementsByTagname('p');
           
$contentnew= array();
            foreach (
$content1 as $value) {
               
$contentnew[]= $value;
                unset(
$value);
            }
            unset(
$content1);
            foreach (
$content2 as $value) {
               
$contentnew[]= $value;
                unset(
$value);
            }
            unset(
$content2);
            foreach (
$content3 as $value) {
               
$contentnew[]= $value;
                unset(
$value);
            }
            unset(
$content3);

            if (
count($contentnew)==0){
               
$result= $str;
            } else {
                foreach (
$contentnew as $value) {
                   
$str1= getMaxTextBody($value);
                   
$str2= $value->nodeValue;
                       
//let's say largest body has more than 50% of the text in its parent
                                   
if (strlen($str1)*2<strlen($str2)){
                       
$str1= $str2;
                    }
                    if (
strlen($str1)*2>strlen($str)&&strlen($str1)>$maxlen){
                       
$result= $str1;
                    } elseif (
strlen($str1)>$maxlen){
                       
$result= $str1;
                    }
                   
$maxlen= strlen($result);
                }
            }
           
$maxlen= strlen($result);
            unset(
$contnentnew);
        }
    }

    unset(
$new);
    return
$result;
}
print
getMaxTextBody($dom);

?>
Jochem Blok
15-May-2008 01:58
To indent a XML in a pretty way I use:

<?
$sXML
= '<root><element><key>a</key><value>b</value></element></root>';
$doc = new DOMDocument();
$doc->preserveWhiteSpace = false;
$doc->formatOutput   = true;
$doc->loadXML($sXML);
echo
$doc->saveXML();
?>
Fernando H
11-Apr-2008 07:48
Showing a quick example of how to use this class, just so that new users can get a quick start without having to figure it all out by themself. ( At the day of posting, this documentation just got added and is lacking examples. )

<?php

// Set the content type to be XML, so that the browser will   recognise it as XML.
header( "content-type: application/xml; charset=ISO-8859-15" );

// "Create" the document.
$xml = new DOMDocument( "1.0", "ISO-8859-15" );

// Create some elements.
$xml_album = $xml->createElement( "Album" );
$xml_track = $xml->createElement( "Track", "The ninth symphony" );

// Set the attributes.
$xml_track->setAttribute( "length", "0:01:15" );
$xml_track->setAttribute( "bitrate", "64kb/s" );
$xml_track->setAttribute( "channels", "2" );

// Create another element, just to show you can add any (realistic to computer) number of sublevels.
$xml_note = $xml->createElement( "Note", "The last symphony composed by Ludwig van Beethoven." );

// Append the whole bunch.
$xml_track->appendChild( $xml_note );
$xml_album->appendChild( $xml_track );

// Repeat the above with some different values..
$xml_track = $xml->createElement( "Track", "Highway Blues" );

$xml_track->setAttribute( "length", "0:01:33" );
$xml_track->setAttribute( "bitrate", "64kb/s" );
$xml_track->setAttribute( "channels", "2" );
$xml_album->appendChild( $xml_track );

$xml->appendChild( $xml_album );

// Parse the XML.
print $xml->saveXML();

?>

Output:
<Album>
  <Track length="0:01:15" bitrate="64kb/s" channels="2">
    The ninth symphony
    <Note>
      The last symphony composed by Ludwig van Beethoven.
    </Note>
  </Track>
  <Track length="0:01:33" bitrate="64kb/s" channels="2">Highway Blues</Track>
</Album>

If you want your PHP->DOM code to run under the .xml extension, you should set your webserver up to run the .xml extension with PHP ( Refer to the installation/configuration configuration for PHP on how to do this ).

Note that this:
<?php
$xml
= new DOMDocument( "1.0", "ISO-8859-15" );
$xml_album = $xml->createElement( "Album" );
$xml_track = $xml->createElement( "Track" );
$xml_album->appendChild( $xml_track );
$xml->appendChild( $xml_album );
?>

is NOT the same as this:
<?php
// Will NOT work.
$xml = new DOMDocument( "1.0", "ISO-8859-15" );
$xml_album = new DOMElement( "Album" );
$xml_track = new DOMElement( "Track" );
$xml_album->appendChild( $xml_track );
$xml->appendChild( $xml_album );
?>

although this will work:
<?php
$xml
= new DOMDocument( "1.0", "ISO-8859-15" );
$xml_album = new DOMElement( "Album" );
$xml->appendChild( $xml_album );
?>

DOMDocument::__construct> <DOMComment::__construct
Last updated: Fri, 05 Dec 2008
 
 
show source | credits | stats | sitemap | contact | advertising | mirror sites