Regina Schaukrug: HTML != XML

Beitrag lesen

Wenn man es mit SimpleXML lösen kann

Naja... Mit SimpleXML könnte es schon beim Laden des "XML" Probleme geben:

<p>: The end tag may be omitted if the <p> element is immediately followed by an <address>, <article>, <aside>, <blockquote>, <div>, <dl>, <fieldset>, <footer>, <form>, <h1>, <h2>, <h3>, <h4>, <h5>, <h6>, <header>, <hr>, <menu>, <nav>, <ol>, <pre>, <section>, <table>, <ul> or another <p> element, or if there is no more content in the parent element and the parent element is not an <a> element.

<li>: The end tag can be omitted if the list item is immediately followed by another <li> element, or if there is no more content in its parent element.

<td>: The end tag may be omitted, if it is immediately followed by a <th> or <td> element or if there are no more data in its parent element.

<th>: The end tag may be omitted, if it is immediately followed by a <th> or <td> element or if there are no more data in its parent element.

Testen wir das mal mit erlaubtem HTML… (Hinweis: Der Validator meckert nur den fehlenden <title> an.)

<?php

$html='<!DOCTYPE html>
<html lang="de">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=Edge">
    <meta name="viewport" content="width=device-width,initial-scale=1,shrink-to-fit=no">
  </head>
  <body>
	<h1>Hallo Welt!</h1>
	<p>Lorem ipsum 
	<p>Lorem ipsum 
	<p>Lorem ipsum 
  </body>
</html>';

$xml = simplexml_load_string( $html );

print_r( (array) $xml );

Ups!

PHP Warning:  simplexml_load_string(): Entity: line 7: parser error : Opening and ending tag mismatch: meta line 6 and head in /tmp/xml_simple_test.php on line 18
PHP Stack trace:
PHP   1. {main}() /tmp/xml_simple_test.php:0
PHP   2. simplexml_load_string() /tmp/xml_simple_test.php:18
PHP Warning:  simplexml_load_string():   </head> in /tmp/xml_simple_test.php on line 18
PHP Stack trace:
PHP   1. {main}() /tmp/xml_simple_test.php:0
PHP   2. simplexml_load_string() /tmp/xml_simple_test.php:18
PHP Warning:  simplexml_load_string():          ^ in /tmp/xml_simple_test.php on line 18
PHP Stack trace:
PHP   1. {main}() /tmp/xml_simple_test.php:0
PHP   2. simplexml_load_string() /tmp/xml_simple_test.php:18
PHP Warning:  simplexml_load_string(): Entity: line 13: parser error : Opening and ending tag mismatch: p line 12 and body in /tmp/xml_simple_test.php on line 18
PHP Stack trace:
PHP   1. {main}() /tmp/xml_simple_test.php:0
PHP   2. simplexml_load_string() /tmp/xml_simple_test.php:18
PHP Warning:  simplexml_load_string():   </body> in /tmp/xml_simple_test.php on line 18
PHP Stack trace:
PHP   1. {main}() /tmp/xml_simple_test.php:0
PHP   2. simplexml_load_string() /tmp/xml_simple_test.php:18
PHP Warning:  simplexml_load_string():          ^ in /tmp/xml_simple_test.php on line 18
PHP Stack trace:
PHP   1. {main}() /tmp/xml_simple_test.php:0
PHP   2. simplexml_load_string() /tmp/xml_simple_test.php:18
PHP Warning:  simplexml_load_string(): Entity: line 14: parser error : Opening and ending tag mismatch: p line 11 and html in /tmp/xml_simple_test.php on line 18
PHP Stack trace:
PHP   1. {main}() /tmp/xml_simple_test.php:0
PHP   2. simplexml_load_string() /tmp/xml_simple_test.php:18
PHP Warning:  simplexml_load_string(): Entity: line 14: parser error : Premature end of data in tag p line 10 in /tmp/xml_simple_test.php on line 18
PHP Stack trace:
PHP   1. {main}() /tmp/xml_simple_test.php:0
PHP   2. simplexml_load_string() /tmp/xml_simple_test.php:18
PHP Warning:  simplexml_load_string(): Entity: line 14: parser error : Premature end of data in tag body line 8 in /tmp/xml_simple_test.php on line 18
PHP Stack trace:
PHP   1. {main}() /tmp/xml_simple_test.php:0
PHP   2. simplexml_load_string() /tmp/xml_simple_test.php:18
PHP Warning:  simplexml_load_string(): Entity: line 14: parser error : Premature end of data in tag meta line 5 in /tmp/xml_simple_test.php on line 18
PHP Stack trace:
PHP   1. {main}() /tmp/xml_simple_test.php:0
PHP   2. simplexml_load_string() /tmp/xml_simple_test.php:18
PHP Warning:  simplexml_load_string(): Entity: line 14: parser error : Premature end of data in tag meta line 4 in /tmp/xml_simple_test.php on line 18
PHP Stack trace:
PHP   1. {main}() /tmp/xml_simple_test.php:0
PHP   2. simplexml_load_string() /tmp/xml_simple_test.php:18
PHP Warning:  simplexml_load_string(): Entity: line 14: parser error : Premature end of data in tag head line 3 in /tmp/xml_simple_test.php on line 18
PHP Stack trace:
PHP   1. {main}() /tmp/xml_simple_test.php:0
PHP   2. simplexml_load_string() /tmp/xml_simple_test.php:18
PHP Warning:  simplexml_load_string(): Entity: line 14: parser error : Premature end of data in tag html line 2 in /tmp/xml_simple_test.php on line 18
PHP Stack trace:
PHP   1. {main}() /tmp/xml_simple_test.php:0
PHP   2. simplexml_load_string() /tmp/xml_simple_test.php:18
Array
(
    [0] => 
)