How to Echo Xml File in PHP

How to echo XML file in PHP

You can use HTTP URLs as if they were local files, thanks to PHP's wrappers

You can get the contents from an URL via file_get_contents() and then echo it, or even read it directly using readfile()

$file = file_get_contents('http://example.com/rss');
echo $file;

or

readfile('http://example.com/rss');

Don't forget to set the correct MIME type before outputing anything, though.

header('Content-type: text/xml');

How to display raw xml file in php to web browser

You do not have to use the simplexml_load_file: there is another function to read a file, this function is file_get_contents($filename).

Here is a simple code to use:

<?php
// Set the encoding to XML
header('Content-type: text/xml');

// Get contents of the file
$xml = file_get_contents("xmlfile.xml") ;

// Print contents
echo $xml;

?>

I hope it helped you! And sorry for the language mistakes ;)

Php echo xml file

If you want to stick with print_r(), just echo pre tags before and after.

eg:

<?php
$xml=simplexml_load_file("Artikelen.xml");
echo '<pre>';
print_r($xml);
echo '</pre>';
?>

How do I get PHP to echo XML tags?

Add the following code at the beginning of your file:

header('Content-Type: text/plain');

By serving the response using this header, the browser will not try to parse it as XML, but show the full response as plain text.

trying to echo xml data to html table with php

foreach($xml->movie as $movie){
echo "<tr>";
echo "<td>{$movie->title}</td>";
echo "<td>{$movie->year}</td>";
echo "<td>{$movie->director->producer}</td>";
echo "<td>";
foreach($movie->stars->star as $stars){
echo $stars[0] . '<br/>';
}
echo "</td>";
echo "<td>{$movie->budget}</td>";
echo "<td>{$movie->mst}</td>";
echo "<br />";
echo "</tr>";
}

I think this should work. You were creating a new <td> element for every star, but you only had one <th> tag corresponding.

Echo something with PHP code in the xml file

I found out from the url that you want to create a sitemap. You should see the output inside a xml code. for example:

<?php 
header("Content-type: text/xml");
$array = ["a", "b", "c"];

echo "<?xml version='1.0' encoding='UTF-8'?>"."\n";
echo "<urlset xmlns='http://www.sitemaps.org/schemas/sitemap/0.9'>"."\n";
echo "<url>";
echo "<loc>".$array[0]."</loc>";
echo "<lastmod>".$array[1]."</lastmod>";
echo "<changefreq>daily</changefreq>";
echo "</url>";
echo "</urlset>";

Echoing the contents of an xml-file with nested tags

I'm a total beginner, so please forgive me if this should be obvious.

You didn't share which problem you had finding the manual (or which manual), so you have to forgive yourself in the end for not asking for it.

So now for the less obvious things. You have not written if SimpleXML is your preferred API choice to access XML, so I will think so as you have it in your question and it might explain why you run into obstacles that early.

SimpleXML is handy when you want to access the XML like a data-structure, e.g. elements are like objects with properties.

So in the beginning this works very well, let's review an example with a title element:

<?php declare(strict_types=1);

$path = "text.xml";
$xml = simplexml_load_file($path);

header('content-type: text/html');

?>
<h2><?= htmlspecialchars((string)$xml->title, ENT_QUOTES | ENT_HTML5) ?></h2>

But then you need it a little bit different. You're interested at all the content within an element, specifically the text-content and first of all across all the <text> elements' descendants.

Lets review the <text> element example:

<text>
<line>Lorem ipsum <noun>dolor</noun> sit <verb>amet</verb>, <verb>consectetur</verb> adipiscing elit.</line>
<line>Sed do eiusmod <noun>tempor</noun> <verb>incididunt</verb> ut <noun>labore</noun> et dolore magna aliqua.</line>
</text>

Now here the "simple" in a SimpleXMLElement somehow comes to an end. You may have noticed it already and then tried by looping over the descendants etc..

If you take a look over at the sister-library named DomDocument - which is much more complicated at what it all has - but it has some pieces that would be very fitting in your scenario. Namely the DOMNode::textContent property:

textContent The text content of this node and its descendants.

Hey cool, just what we need, but it's DOMDocument, not SimpleXML?!

Fear not, they work together well, perhaps best shown by example:

<?= htmlspecialchars(dom_import_simplexml($xml->text)->textContent, ENT_QUOTES | ENT_HTML5) ?>

This shows the whole text:

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.

(you can add a line-break after each line by iterating over the line elements and after echoing the textContent you add the <br>, so looking into looping early was not such a bad idea in case you might think it was)

So and if I got your question right, this is what you're first of all looking for.


The example as a whole:

<?php declare(strict_types=1);

$xml = simplexml_load_string(<<<XML
<file>
<title>Lorem Ipsum</title>
<text>
<line>Lorem ipsum <noun>dolor</noun> sit <verb>amet</verb>, <verb>consectetur</verb> adipiscing elit.</line>
<line>Sed do eiusmod <noun>tempor</noun> <verb>incididunt</verb> ut <noun>labore</noun> et dolore magna aliqua.</line>
</text>
</file>
XML);

$view = new SimpleXMLElement('<v content-type="text/html"/>');
$view->title = htmlspecialchars((string)$xml->title, ENT_QUOTES | ENT_HTML5);
$view->text = htmlspecialchars(dom_import_simplexml($xml->text)->textContent, ENT_QUOTES | ENT_HTML5);

foreach($view->attributes() as $name => $value) {
header("$name: $value");
}
?>
<h2><?= $view->title) ?></h2>

<?= $view->text ?>

How to read xml file as it is in browser using php?

Use like below to show xml content as like it is on the web.

echo '<p>This is XML string content:</p>';
echo '<pre>';
echo htmlspecialchars(file_get_contents("test.xml"));
echo '</pre>';

test.xml content is like below

<?xml version="1.0"?>
<book>
<title>This is the title</title>
</book>


Related Topics



Leave a reply



Submit