<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title>Dealing with XML errors</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="simplexml.examples-basic.html">Basic SimpleXML usage</a></div> <div class="next" style="text-align: right; float: right;"><a href="class.simplexmlelement.html">SimpleXMLElement</a></div> <div class="up"><a href="simplexml.examples.html">Examples</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><div id="simplexml.examples-errors" class="section"> <h2 class="title">Dealing with XML errors</h2> <p class="para"> Dealing with XML errors when loading documents is a very simple task. Using the <a href="book.libxml.html" class="link">libxml</a> functionality it is possible to suppress all XML errors when loading the document and then iterate over the errors. </p> <p class="para"> The <a href="class.libxmlerror.html" class="classname">libXMLError</a> object, returned by <span class="function"><a href="function.libxml-get-errors.html" class="function">libxml_get_errors()</a></span>, contains several properties including the <a href="class.libxmlerror.html#libxmlerror.props.message" class="link">message</a>, <a href="class.libxmlerror.html#libxmlerror.props.line" class="link">line</a> and <a href="class.libxmlerror.html#libxmlerror.props.column" class="link">column</a> (position) of the error. </p> <p class="para"> <div class="example" id="example-5495"> <p><strong>Example #1 Loading broken XML string</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br />libxml_use_internal_errors</span><span style="color: #007700">(</span><span style="color: #0000BB">true</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$sxe </span><span style="color: #007700">= </span><span style="color: #0000BB">simplexml_load_string</span><span style="color: #007700">(</span><span style="color: #DD0000">"<?xml version='1.0'><broken><xml></broken>"</span><span style="color: #007700">);<br />if (</span><span style="color: #0000BB">$sxe </span><span style="color: #007700">=== </span><span style="color: #0000BB">false</span><span style="color: #007700">) {<br /> echo </span><span style="color: #DD0000">"Failed loading XML\n"</span><span style="color: #007700">;<br /> foreach(</span><span style="color: #0000BB">libxml_get_errors</span><span style="color: #007700">() as </span><span style="color: #0000BB">$error</span><span style="color: #007700">) {<br /> echo </span><span style="color: #DD0000">"\t"</span><span style="color: #007700">, </span><span style="color: #0000BB">$error</span><span style="color: #007700">-></span><span style="color: #0000BB">message</span><span style="color: #007700">;<br /> }<br />}<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> <div class="example-contents"><p>The above example will output:</p></div> <div class="example-contents screen"> <div class="cdata"><pre> Failed loading XML Blank needed here parsing XML declaration: '?>' expected Opening and ending tag mismatch: xml line 1 and broken Premature end of data in tag broken line 1 </pre></div> </div> </div> </p> <div class="section"> <h2 class="title">See Also</h2> <p class="para"> <ul class="simplelist"> <li class="member"> <span class="function"><a href="function.libxml-use-internal-errors.html" class="function">libxml_use_internal_errors()</a></span></li> <li class="member"> <span class="function"><a href="function.libxml-get-errors.html" class="function">libxml_get_errors()</a></span></li> <li class="member"><a href="class.libxmlerror.html" class="xref">libXMLError</a></li> </ul> </p> </div> </div><hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="simplexml.examples-basic.html">Basic SimpleXML usage</a></div> <div class="next" style="text-align: right; float: right;"><a href="class.simplexmlelement.html">SimpleXMLElement</a></div> <div class="up"><a href="simplexml.examples.html">Examples</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>