Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Php - retrieve informations from an xml file

Tags:

html

dom

php

xml

I'm approaching web programming. I need to retrieve some informations from a web page. I have the url of the page, so I want the html source code, translate it into xml and then use the dom functions of php to fetch the informations I need.

My php code is this:

$url=$_POST['url']; //url

$doc_html=new DOMDocument();
$doc_html->loadHTML($url); //html page
$doc_xml=new DOMDocument();
$doc_xml->loadXML($doc_html->saveXML()); //xml converted page

$nome_app=new DOMElement($doc_xml->getElementById('title'));

echo $nome_app->nodeValue;

I get this fatal error:

Uncaught exception 'DOMException' with message 'Invalid Character Error' on this line:

$nome_app=new DOMElement($doc_xml->getElementById('title'));

What's wrong? Is it the entire process html-to-xml? I found some example on the web and should work... Thanks!

like image 800
esseara Avatar asked Dec 15 '25 17:12

esseara


1 Answers

Solved! Simply:

$doc_html=new DOMDocument();
$doc_html->loadHTML(file_get_contents($url));
$doc_html->saveXML();
$nome = $doc_html->getElementsByTagName('h1');
foreach ($nome as $n) { 
   echo $n->nodeValue, PHP_EOL;
}

Maybe the code was too messy before. Thanks everybody for the answers!

like image 108
esseara Avatar answered Dec 17 '25 10:12

esseara



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!