0

I use this code

$html = file_get_html('http://example.com');

But I want convert it to XML file then use some functions on it. how can I do this?

I want to use this code. Can I?

$html = file_get_html('http://example.com'); 
$doc = new DOMDocument();
$doc->loadHTML($html);
$xml=simplexml_load_string($doc) or die("Errooooooor");
print_r($xml);
Faegy
  • 942
  • 1
  • 12
  • 29
m-tech
  • 338
  • 2
  • 14
  • Iterate over the elements and output the content as XML.. – chris85 Dec 26 '16 at 21:39
  • Just put your HTML as String into a DOMDocument Object. [Topic has been discussed here](http://stackoverflow.com/a/4881456/5482243) – Ben Smith Dec 26 '16 at 21:41
  • [tagsoup](https://sourceforge.net/projects/tagsoup/) is a SAX parser for HTML that may be useful here. See also [How to use JAXB with HTML?](http://stackoverflow.com/q/24791422/6002174), [parse any HTML to XML using html5lib](http://stackoverflow.com/q/26717379/6002174) and [jTidy and TagSoup documentation](http://stackoverflow.com/q/4452566/6002174). – Tsundoku Dec 27 '16 at 03:14

1 Answers1

0

It would be easier to work with HTML in this case, with DOMDocument();
http://php.net/manual/en/domdocument.loadhtml.php

$html = file_get_html('http://example.com'); 
$doc = new DOMDocument();
$doc->loadHTML($html);
Alex K.
  • 18
  • 2
  • Alex thanks but when I use this for some domain it work but for some of them it dosent work. why? – m-tech Dec 26 '16 at 22:23
  • Alex i edite my question please read it a gain – m-tech Dec 26 '16 at 22:40
  • `$xml=simplexml_load_string($doc) or die("Errooooooor"); print_r($xml);` - no, you cannot do this! In your case $doc is an object and simplexml_load_string() take a well-formed XML string as parameter. If you want parse your HTML, you can do like this [http://stackoverflow.com/questions/4881255/generating-xml-from-html-list-using-php](example) – Alex K. Dec 27 '16 at 19:35
  • Alex you are kind – m-tech Dec 27 '16 at 19:39