How to Display Xml in HTML in PHP

How to display XML in HTML in PHP?

If you just want a plain-text representation of your (pre-formatted) string, you can wrap it in HTML <pre/> tags and use htmlentities to escape the angle brackets:

<?PHP echo '<pre>', htmlentities($string), '</pre>'; ?>

how to display xml content with php

You can redirect to this file:

<?php header('Location: '.$xmlFile); ?>

Your browser will dispay the content of the file

Display XML inside HTML

If you want markup to be displayed then change all < to < and > to >.

Before displaying anything load your XML to string variable and use this:

$before = array('<','>');
$after = array('<','>');
$xml = str_replace($before,$after,$xml);
echo $xml;

Secondly - you cannot call header() function after displaying ANY character. Don't use it at all. You want default text/html in your document.

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 ;)

Displaying XML Contents in a table with php

Building on Neta Meta's answer:

I was having some trouble actually iterating through the XML object that was built until I realized that the XML returned is inconsistent in its capitalization. Here's some working code to parse that file into a table:

<?php
$xml = new SimpleXMLElement('http://data.fcc.gov/api/license-view/basicSearch/getLicenses?searchValue='.$_GET["callsign"], 0, TRUE);
?>
<table>
<thead>
<tr>
<th>Name</th>
<th>Call Sign</th>
<th>Type</th>
<th>Status</th>
<th>Expiration Date</th>
</tr>
</thead>
<tbody>

<?php foreach ($xml->Licenses->License as $licenseElement) :?>
<tr>
<td><?php echo $licenseElement->licName; ?></td>
<td><?php echo $licenseElement->callsign; ?></td>
<td><?php echo $licenseElement->serviceDesc; ?></td>
<td><?php echo $licenseElement->statusDesc; ?></td>
<td><?php echo $licenseElement->expiredDate; ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>

Use HTML tags in XML file and display/recognize them later as HTML

It is still not clear what is your problem and what is your goal.

Let me try to do some attempt to help you.

First thing - you have data:

<description>Die <strong> Zweckbestimmung </strong> ist ein eleme Konsequenzen.</description>
<comment>All <br/><a href="/someLink" class="button">Erfahren Sie hier mehr</a></comment>

That data portion has html encoded characters. The point is to never mix data with real html or xml.

So when you do:

$this->topics[$page] = $thema->description;

your value still contain html encoded characters:

$this->topics[$page] === `Die <strong> Zweckbestimmung </strong> ist ein eleme Konsequenzen.`

Which is probably kind wrong from your point of view. But to me I don't see any problem yet at this moment.

Then you do:

 <div class='question'><p><strong>Frage {i.cycle}</strong>: 
{question.questiontext}</p>

Lets simplify and focus only on this part:

 <p>{question.questiontext}</p>

That one of things which are not clear in your data xml there is no field with name questiontext. So lets assume that is topic->question->comment.

With your value it means:

<p>All  <br/><a href="/someLink" class="button">Erfahren Sie hier mehr</a></p>

Display xml file in table by using php

Here is the same question from another post. They are using a built-in library and explain how to use it. Link to my reference

$xml = simplexml_load_string($xmlstring, "SimpleXMLElement", LIBXML_NOCDATA);
$json = json_encode($xml);
$array = json_decode($json,TRUE);

Your XML data is now in an array. Then you would display with using just a While loop and build your table that way.

display xml code in a table with php

Instead of print_r($movies); you could do this:

echo $movies->asXML();

Find more in this PHP manual.

- EDIT -

I thought about it and maybe if you replace the whole file_get_contents() thing with the following, it might work:

$doc = new DOMDocument();
$doc->load('http://data.fcc.gov/api/license-view/basicSearch/getLicenses?searchValue=Verizon+Wireless');
echo $doc->saveXML();

I mean from the $file line to the print_r($movie) line.

- END EDIT -

- EDIT 2-

If the XML doesn't show up but it appears on the source code, then try wrapping it with the <pre> tag.

<td>
<pre>
<?php
header('Content-type text/xml');
#dom->preserveWhiteSpace = false;
$doc = new DOMDocument();
$doc->load('http://data.fcc.gov/api/license-view/basicSearch/getLicenses?searchValue=Verizon+Wireless');
echo $doc->saveXML();
?>
</pre>
</td>

- END EDIT 2 -



Related Topics



Leave a reply



Submit