skip to Main Content

-MY GOAL is to display the html tags "My page and TESTING on the page" on the same page.

-If I remove the html tags. It give me the XML.enter image description here

-If I removed the header(‘Content-type: text/xml’); I get only the html and not the xml.
enter image description here

HOW CAN I HAVE BOTH IN THE SAME PAGE.

The code is as follows:

<?php
$test_array = array (
  'bla' => 'blub',
  'foo' => 'bar',
  'overflow' => 'stack',
 );
$xml = new SimpleXMLElement('<root/>');
 array_walk_recursive($test_array, array ($xml, 'addChild'));
ob_start();
print $xml->asXML();
$xml_output = ob_get_clean();
//header('Content-type: text/xml');
echo $xml_output;
?>
<?php // New PHP block for HTML content ?>
<!DOCTYPE html>
<html>
<head>
        <title>My Page</title>
</head>
<body>
<h1>TESTING</h1>
</body>
</html>

/Another try was/
TO put the header as header(‘Content-type: text/json’); //JSON
but here I get the problem whereby it prints the whole html tags
enter image description here

THIS WOULD ALSO BE GOOD FOR ME, IF AM ABLE TO SOLVE THIS

3

Answers


  1. You can’t mix different content types in a single response.

    You output a HTML page that displays the other content. However formatting/styling/interaction would all be your job. You need to escape the included content for HTML – like any other variable.

    $xml = <<<'XML'
    <?xml version="1.0">
    <foo>
      <bar/>
    <foo>
    XML;
    
    echo '<h1>Example</h1>';
    echo '<pre><code class="language-xml">', htmlspecialchars($xml), '</code></pre>';
    
    Login or Signup to reply.
  2. restructuring this a bit:

    <!DOCTYPE html>
    <html>
    <head>
            <title>My Page</title>
    </head>
    <body>
    <h1>TESTING</h1>
    <pre>
    <?php
    $test_array = array (
      'bla' => 'blub',
      'foo' => 'bar',
      'overflow' => 'stack',
     );
    $xml = new SimpleXMLElement('<root/>');
    array_walk_recursive($test_array, array ($xml, 'addChild'));
    print htmlspecialchars($xml->asXML());
    ?>
    </pre>
    </body>
    </html>
    

    Should give you this in your browser:
    result in the browser

    The <pre>-Tag tells the browser that this is preformatted text and the htmlspecialchars() makes sure to replace < and > (open and closing brackets) with HTML entities so that the browser won’t try to interpret this as HTML-Tags, but just as plain text.

    Hope this helps!

    Some links that might be of interest:

    Login or Signup to reply.
  3. If you just need to display html with xml (with tags), use this

    <h1>Test html header</h1>
    <?php
    $string =
    "
    <root>
        <blub>
            <example_value>EXAMPLE</example_value>
        </blub>
    </root>
    ";
    echo '<pre>', htmlentities($string), '</pre>';
    ?>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search