Able
Able

Reputation: 3912

PHP : HTML formating is not happening while reading the content from XML

I'm reading the contents from an XML file to show the web pages contents, please see the code snippet to read the XML file,

    <html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title></title>
    </head>
    <body>
    <?php
    $arg = $_GET['content'];
    //echo $arg;
    $doc = new DOMDocument();
    $doc->load('History.XML');
    $contents = $doc->getElementsByTagName($arg);
    $content1 = $contents->item(0)->nodeValue;
    $content2 = $contents->item(1)->nodeValue;

    ?>

        <p> 
        <?php 
        echo <<<EOM
        $content1 
    EOM;
        ?> 
        </p> 
    </body>
</html>

The XML file contents have some HTML formatting tags like <b> <h3> etc, but reading and showing those contents from XML , the HTML formatting are not happening. Please clarify me if I did any mistake.

Upvotes: 0

Views: 152

Answers (1)

VolkerK
VolkerK

Reputation: 96159

nodeValue in your case is a concatenation of all text nodes.
Use string DOMDocument::saveXML ([ DOMNode $node [, int $options ]] ) instead.

self-contained example:

<?php
$arg = 'foo';
$doc = new DOMDocument();
$doc->loadxml('<x>
    <foo><h1>lalala</h1><b>xyz</b></foo>
    <foo>12345</foo>
</x>');
$contents = $doc->getElementsByTagName($arg);
$content1 = $contents->item(0);
$content2 = $contents->item(1);
?>
<html>
  <head>
      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
      <title></title>
  </head>
  <body>
    <p><?php echo $doc->savexml($content1); ?></p> 
  </body>
</html>

prints

<html>
  <head>
      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
      <title></title>
  </head>
  <body>
    <p><foo><h1>lalala</h1><b>xyz</b></foo></p> 
  </body>
</html>

Upvotes: 3

Related Questions