Aller au contenu

simpleXML : extraire les données de balises xhtml qui se trouve sur la page ?


yhugo

Sujets conseillés

Bonjour à tous,

j'ai cherché sur le Web des tutoriels, mais je n'ai rien trouvé ... :blush:

J'ai une page PHP/XHTML qui contient du RDFa ... Elle ressemble un peu à ceci :


<div rel="ov:businessCard">
<div about="http://sw-app.org/mic.xhtml#businesscard" typeof="vcard:VCard">
<div rel="vcard:org">
<div about="http://sw-app.org/mic.xhtml#org" typeof="vcard:Organization">
<span property="vcard:organization-name" datatype="xsd:string">
Digital Enterprise Research Institute (DERI),<br />
National University of Ireland, Galway<br />
</span>
<span rel="owl:sameAs" resource="http://dbpedia.org/resource/Digital_Enterprise_Research_Institute" />
</div>
</div>
<div rel="vcard:workAdr">
<div about="http://sw-app.org/mic.xhtml#postaladress" typeof="vcard:Address">
<span property="vcard:street-address">IDA Business Park, Lower Dangan</span>,<br />
<span property="vcard:locality">Galway</span>,
<span property="vcard:country-name">Ireland</span>
</div>
</div>
Tel.: <span property="vcard:workTel">+353 91 495730</span>
</div>
</div>

J'aimerais savoir comment je peux extraire des données de ce bout de code avec simpleXML et xPath ? J'ai déjà utilisé simpleXML avec un fichier XML externe, mais là j'aimerais l'utiliser avec du XHTML qui ce trouve sur ma page... mais j'ai aucune idée de la façon de charger le DOM pour ensuite le parcourir.

Voilà, merci et bonne journée tout le monde...

Lien vers le commentaire
Partager sur d’autres sites

Bonjour SStephane,

Voilà j'ai essayé la syntaxe que tu m'as proposée et voici l'erreur qu'on me retourne :


Warning: SimpleXMLElement::__construct() [simplexmlelement.--construct]: Entity: line 255: parser error : Extra content at the end of the document in /home/yhs/www/cv.hugo.soucy.cc/mic.xhtml.php on line 257

Warning: SimpleXMLElement::__construct() [simplexmlelement.--construct]: <br /> in /home/yhs/www/cv.hugo.soucy.cc/mic.xhtml.php on line 257

Warning: SimpleXMLElement::__construct() [simplexmlelement.--construct]: ^ in /home/yhs/www/cv.hugo.soucy.cc/mic.xhtml.php on line 257

Fatal error: Uncaught exception 'Exception' with message 'String could not be parsed as XML' in /home/yhs/www/cv.hugo.soucy.cc/mic.xhtml.php:257 Stack trace: #0 /home/yhs/www/cv.hugo.soucy.cc/mic.xhtml.php(257): SimpleXMLElement->__construct('<!DOCTYPE html ...') #1 {main} thrown in /home/yhs/www/cv.hugo.soucy.cc/mic.xhtml.php on line 257

Le code :


<?php
$currentDocument = file_get_contents("http://cv.hugo.soucy.cc/mic.xhtml.php","r");
$vCard = new SimpleXMLElement($currentDocument);

print $vCard->div;
print $vCard->xpath('//h1');
?>

Merci, bonne journée

Lien vers le commentaire
Partager sur d’autres sites

Je dirai que la syntaxe de ton fichier n'est pas bonne :)

fichier qui traite :


<?php
$currentDocument = file_get_contents("test.html","r");
$vCard = new SimpleXMLElement($currentDocument);

print_r( $vCard->xpath('//body/h1'));
?>

test.html (j'ai repris ton html) :


<html>
<head>

</head>
<body>
<h1>Hello</h1>
<div rel="ov:businessCard">
<div about="http://sw-app.org/mic.xhtml#businesscard" typeof="vcard:VCard">
<div rel="vcard:org">
<div about="http://sw-app.org/mic.xhtml#org" typeof="vcard:Organization">
<span property="vcard:organization-name" datatype="xsd:string">
Digital Enterprise Research Institute (DERI),<br />
National University of Ireland, Galway<br />
</span>
<span rel="owl:sameAs" resource="http://dbpedia.org/resource/Digital_Enterprise_Research_Institute" />
</div>
</div>
<div rel="vcard:workAdr">
<div about="http://sw-app.org/mic.xhtml#postaladress" typeof="vcard:Address">
<span p[php][/php]

roperty="vcard:street-address">IDA Business Park, Lower Dangan</span>,<br />

<span property="vcard:locality">Galway</span>,

<span property="vcard:country-name">Ireland</span>

</div>

</div>

Tel.: <span property="vcard:workTel">+353 91 495730</span>

</div>

</div>

</body>

</html>

Qui fonctionne très bien, ça renvoi :

Array ( [0] => SimpleXMLElement Object ( [0] => Hello ) ) 

Lien vers le commentaire
Partager sur d’autres sites

Veuillez vous connecter pour commenter

Vous pourrez laisser un commentaire après vous êtes connecté.



Connectez-vous maintenant
×
×
  • Créer...