How to display raw xml file in php to web browser [duplicate] - php

This question already has answers here:
How to return a file in PHP
(4 answers)
Closed 3 years ago.
Hi i got an xml file and i want to display it on a website.
I tried everything what i found and tried to do it my self but im a newbie to any code language.
My code is this right now:
<?php
header('Content-type: text/xml');
$xml = simplexml_load_file(xmlfile.xml) ;
echo $xml ;
?>
And the output what i see when i go to my website is nothing just a warning about: This XML file does not appear to have any style information associated with it. The document tree is shown below.
But i cant see anything. So please can someone help me write a code that outputs the whole xml file including xml declaration , tags and node values?

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

Try this code. It works for me.
<?php
header("Content-type: text/xml");
$yourFile = "xmlfile.xml";
$file = file_get_contents($yourFile);
echo $file;
If you insist on simple xml you can write like this.
$xml = simplexml_load_file("xmlfile.xml");
echo $xml->asXML();

Related

extract data from particular line in html page using php

this was my original question I was stuck and tried to solve my problem by trying something and got stuck again
I need to extract name of candidate and his id from a pdf ,so after using pdfparser I extracted the text and downloaded the html page using php
<?php
$filename = 'filename.html';
header('Content-disposition: attachment; filename=' . $filename);
header('Content-type: text/html');
// ... the rest of your file
?>
<?php
// Include Composer autoloader if not already done.
include 'C:\Users\amite\Downloads\pdfparser-master (1)\pdfparser-master\vendor\autoload.php';
// Parse pdf file and build necessary objects.
$parser = new \Smalot\PdfParser\Parser();
$pdf = $parser->parseFile('C:\Users\amite\Desktop\Data\001.ApplicationForm-CSE-2015-1-omokop (3).pdf');
$text = $pdf->getText();
echo $text;
?>
I did this cause the info I need that was on line 12 and 13 of the view source page and this was was with all the pdf's I need ,so after downloading the html file I used the code below to see the source page of html file
<?php
show_source("filename.html");
?>
now when I run the above program I got the source page of html file which I downloaded, now I need to extract data from line 12 and 13 , the output of program looks like this :-
<html>
text
text
text
text
text
text
there are no tags except html tag and info I need is on line 12,13, if you need any clarification please ask me I will tell you.
how should I extract text from line 12,13, if there is another way tell me pls. I am stuck again, if the question is vague I will clarify it or improve it, please help me.
Store the file source into an array with $source = file('filename.html'); and extract line 12 and 13 via array index 11 and 12 like this echo $source[11]; //line 12
Is this what you need?
<?php
$str = "1text
2text
3text
4text
5text
6text
7text
8text
9text
10text
11text
12text
13text
";
$k = array_slice(explode("\n",$str),11,1);
print_r($k);

Save XML to the server a file generated by PHP

After some searches on the web and some tests, i can't find the solution and need your help
From a php script, i create a XML output :
$xml = new SimpleXMLElement($string);
//some code
echo $xml->asXML();
I Would like to create a file contain the XML and save on my server by using PHP.
Thanks for your help
if echo $xml->asXML() displays the xml then I guess you could use file_put_contents or other similar function. ie:
$filepath='/path/to/directory/filename.xml';
file_put_contents( $filepath, $xml->asXML() );

convert doc to html by php script

In the last few hours I found script for php which converts a .doc file to a .html file but I've not been successful yet.
I just made some simple code for it, like this:
<?php
header("Content-type: application/html");
header("Content-Disposition: attachment;Filename=as.html");
echo $content = readfile('abc.doc');
?>
It makes an output like the one shown below in the image.
any other way to do this ? as same to same as .doc file ?
Did you already search for a solution? I just found this link, perhaps it might help you:
http://www.daniweb.com/web-development/php/threads/130342/convert-.doc-to-html-or-txt-on-fly-during-upload-with-php

Getting the content of an xml file creates an XML Parsing Error

I am trying to get the contents of an XML file in case that the file exists or I am generating a new xml file. The problem is that when I am trying to get the xml file I get this error:
XML Parsing Error: no element found Location:
http://mydomain.gr/generate.php Line Number 1, Column 1: ^
My code is this
<?php
include_once('xmlgenerator.php');
$xml = new XmlGenerator();
if($xml->cachefile_exists){
if(!$xml->is_uptodate()){
// echo "update it now";
$xml->createFile = 1;
$data = $xml->create();
Header('Content-type: text/xml');
print($data->asXML());
}else{
//echo "doesn't need any update.";
Header('Content-type: text/xml');
file_get_contents($xml->cached_file);
}
}else{
// echo "Didn't find any cache file. Lets Create it";
$xml->createFile = 1;
$data = $xml->create();
Header('Content-type: text/xml');
print($data->asXML());
}
?>
The XML structure is fine, and I double check about the XML file encoding or the php file that call the XML. Everything is UTF8 without BOM. When I open the XML file directly in a browsers it looks perfect and its a valid file ( checked it with w3c and online tools).
the 2 lines that create the problem(most probably):
Header('Content-type: text/xml');
file_get_contents($xml->cached_file)
I even deleted anything and just used this 2 lines and got the same error.
So what can be wrong? Is there any proper way to include an XML file in a php file, change the headers and show it to the end user? I really don't want to redirect, I need to stay to the same php file just show XML content.
echo file_get_contents($xml->cached_file)

I need to provide a XML file so my Android can fetch it

I really am trying to code from PHP to have a XML file. I got the code that can display items from PHPmyadmin. but how can I display those things in XML file is really my problem, I tried all the codes. I could find. But, still no luck.
While generating xml your first line in php file should be
header ("content-type: text/xml; charset=utf-8");
then use echo statement & print whatever you want within xml tags like
echo "<status>0</status>";
just follow xml standerds & test using IE will be better.

Categories