What php function can display formated XHTML from a XML file? - php

Ive been trying to display formatted content blocks from a xml file with no luck. Ive been using simplexml_load_file and other varients which Im now seeing cannot deal with the xhtml tags within the called tag ... eg.
//php contents
<?php $xml=simplexml_load_file("file.xml");
echo ($xml->entry); ?>
//xml contents
<entry>
<p>This does not work</p>
</entry>
whereas
<entry>This works</entry>
can someone please tell me which php function can do this from an xml file and or
what is the best way to display the contents with xhtml formatting?
Im trying to dynamically load content to a webpage without having to build too many pages. I like the idea of having all my content in one xml file for easy edits.
Theres not enough content to justify a database yet.
Thanks in advance

You can try dumping the contents of a certain simplexml node (in this case: $xml->entry) using the asXml function.
echo $xml->entry->asXml();
Check the php documentation on simplexml here (link to the asXml() call):
Simplexml documentation

im getting closer... I found asXML() which outputs the html tags... but not sure yet how to point to specific blocks.... eg $xml->asXML(block) displays 1
got it
$xml->block->asXML()
works
would still like to know if there is a better method tho.

Related

How to get tag's attribute using PHP simple HTML DOM parser

I am using the PHP Simple HTML DOM parser to scrap website data, but unfortunately not able to extract the data i want to. I have also tried to google and look in the documentation but could not solve the issue. The code structure of what i am trying to scrap is something like this.
<div id="section1">
<h1>Some content</h1>
<p>Some content</p>
............
<<Not fixed number of element>>
............
<script> <<Some script>></script>
<video>
<source src="www.exmple.com/34/exmple.mp4">
</video>
</div>
I tried with JavaScript and i could do the same like this
document.getElementById("section1").getElementsByTagName("source")[0].getAttribute("src");
But when i tried with PHP Dom parser i m not getting any data.
Here is how my code looks likes
require ''.$_SERVER['DOCUMENT_ROOT'].'/../lib/simplehtmldom/simple_html_dom.php';
$html_content = get($url); //This is cURL function to get website content.
$obj_content = str_get_html($html_content);
$linkURL = $obj_content->getElementById('section1')->find('source',0)->getAttribute('src');
var_dump($linkURL);
This results in an empty string. I also tried changing to code a bit here and there but none of those works every time came blank. But if i var dump $obj_content i get lot of dom element
I tried to follow these posts from stackoverflow which are similar to mine , but these did not help me.
How do I get the HTML code of a web page in PHP?
PHP Simple HTML DOM
PHP Simple HTML DOM Parser Call to a member function children() on a non-object
And their manual http://simplehtmldom.sourceforge.net/manual.htm
Can anyone please help me
Thank you
The code snippet is fine as it is. Problem was that the URL that I was targeting was not there at the time of page load. It was added by the <script> tag after page being loaded.
Thank you #WillardSolutions

What is a good way to change/replace elements in a php+css document?

Forgive me for possibly misusing certain terminology. I would like to:
open a html/php file, with php
find elements with certain class
change its innerHtml basically
than save the file.
I have a feeling DOMElement in php could help, since I've used it for similar things in javascript, but I am (still) unsure of its function in PHP and php.net sais a DOM document "Represents an entire HTML or XML document" (so no php/javascript containing document).
So: which function(s), libraries should I study to best perform those operations?
Possibly: Aside from php.net do you perhaps have a good tutorial for ^that^ solution?
Edit: Possibly related: Manipulate HTML from php
Edit2: If I would build something with my knowledge a.t.m. it would probably be almost as violent as solutions I tried earlier: https://stackoverflow.com/a/8960363/574700
There will be php in the document.
There will be some inline css and javascript in there.
1.open a html/php file, with php
Include htmlsimpledom and open you php file.
$html = file_get_html('myfile.html');
2.find elements with certain class
foreach($html -> find('.class-name') as $element)
$element - > plaintext. '<br>';
3.change its innerHtml basically
$html -> find('.class-name') -> innertext = 'text-here';
4.then save the file.
file_put_contents($filename, $html);

How to wrap my rss feed with asXML function

I have a page called rss.php that contains PHP SQL and XML which dynamically, and might I add PERFECTLY, produces the XML needed for the rss feed for my podcast. Only problem is, its a PHP file. I need to get the stuff this page spits out in to a file named rss.xml for iTunes to accept it. I came across this little bit of code in another thread:
echo $xml->asXML('filename.xml');
and this on http://php.tonnikala.org:
<?php
$string = <<<XML
<a>
<b>
<c>text</c>
<c>stuff</c>
</b>
<d>
<c>code</c>
</d>
</a>
XML;
$xml = new SimpleXMLElement($string);
echo $xml->asXML(); // <?xml ... <a><b><c>text</c><c>stuff</c> ...
?>
Problem is when I wrap my XML in this code, the browser gives me an error and it doesn't load. This may be because my XML isn't just XML, it's PHP and SQL. And now that I think about it, maybe it's cause my PHP hasn't been processed by the server yet...
Anyway, what I want to do is get this rss.php page to spit out XML and save it to a file called rss.xml. Also, can I control how often this happens? Or will it happen every time the page loads?
The answer to my question turned out to be that I DIDN'T need the page to be an XML document at all. As long as the declaration at the beginning says it's XML, its fine. Here's what I did. Remember place this at the VERY TOP of the PHP page.
<?php echo('<?xml version="1.0" encoding="UTF-8"?>') ?>

HTTP Headers Information to show XML file

I want to show XML file on my page, I have set the header header('Content-Type: application/xml')also tried for header(application/rss+xml),
but my URL can not show the page in XML format but in View Page Source it created the XML file
URL- http://submitsitelink.com/rss.php?p=d
Can you please help me ?
Thanks
Google Chrome can neither read RSS nor beautify XML natively. You have to find and install an extension:
RSS Subscription Extension
XML Tree
It sounds like you want to style the output of your XML - one method of doing this is via XSL technologies. You can also add CSS stylesheets to XML documents by adding something akin to the following near the top of your XML document.
<?xml-stylesheet href="common.css"?>
As far as I know you cannot do this with HTTP headers, only by modifying the XML document itself.

Creating a personalization engine with php

I am new to php and I want to create an php engine which changes the web content of a webpage with PHP with the use of data in mysql. For example (changing the order of navigation links on a webpage with the order of highest click count) I am not sure how PHP will read the HTML file and change the elements in the HTML file and also output the HTML file with the changes. Is this possible?
I am not quite sure why you would want to generate the html, read it, change it and then output it. It seems to be a lot easier to just generate it the way you want to in the first place.
I am not sure how PHP will read the HTML file and change the elements in the HTML file and also output the HTML file with the changes. Is this possible?
You could use file_get_contents:
$html = file_get_contents($url);
Then use a html-parser like Simple HTML DOM Parser, change what you want to do and output it.
If you want to modify HTML structure, use ganon - HTML DOM parser for PHP
include('path/ganon.php');
// Parse the google code website into a DOM
$html = file_get_dom('http://code.google.com/');
foreach($html('p[class]') as $element) {
echo $element->class, "<br>\n";
}

Categories