S
H
A
R
E

Friday, September 23, 2011

Easy Way Convert HTML to XML in PHP DOMdocument

You know that this tag is not valid for XML DOM Document. if you will to load content from HTML page and you need to convert it to XML this img tag will be a big trouble. Let me to fix this problem.


See this sample code and you will understand how I can do it :-)


$ret = '';
$x = new DOMDocument;
$x->loadHTML($ret);
$x->loadXML($x->saveXML());
echo $x->childNodes->item(1)->childNodes->item(0)->childNodes->item(0)->getAttribute('src');
?>

$ret is the HTML text source
this code is shown how to converting HTML to XML using PHP DOMdocument class.

4 comments:

Leslie Lim said...

I recently came across your blog and have been reading along. I thought I would leave my first comment. I don't know what to say except that I have enjoyed reading. Nice blog. I will keep visiting this blog very often..


Kesh
www.imarksweb.org

Anonymous said...

not working...

andrea chiu said...

Just stopping by to see something to read and luckily I found it here what I am looking for. Have a great day! Try visiting my site. Thank you.
nhella

www.triciajoy.com

Cindy Dy said...

Awesome there, Great work, keep it up. I love returning back to this site and reading the quality content you always have on offer.

Wendy
www.gofastek.com

Post a Comment