Current location: Hot Scripts Forums » Programming Languages » PHP » PHP and XML


PHP and XML

Reply
  #1 (permalink)  
Old 03-22-06, 10:08 PM
dwoody dwoody is offline
Newbie Coder
 
Join Date: Jul 2004
Posts: 48
Thanks: 0
Thanked 0 Times in 0 Posts
PHP and XML

I have an XML file that is formatted into HTML by an attached XSL style sheet. If I open the XML file on its own it works great and is formatted properly in HTML and everything. When I try to include this file into a PHP page however (using include() function) I get an error. I am trying to create a page that has this XML file included in it along with a number of other HTML pages. Is using the include function not a valid technique? Should I get rid of the XSL and use PHP to parse the XML and format it in the PHP script? Help please.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #2 (permalink)  
Old 03-23-06, 03:48 AM
NeverMind's Avatar
NeverMind NeverMind is offline
Community VIP
 
Join Date: Aug 2003
Location: K.S.A
Posts: 2,257
Thanks: 0
Thanked 2 Times in 1 Post
yes, you shouldn't use include! use file_get_contents() instead.
include/require are there to include PHP files and parse them in run time and since XML files have the "<?" php thought it was a PHP file and attempted to parse and then found it wasn't a sane php file so it complained!
PHP Code:

echo file_get_contents('myXMLFile.xml'); 

__________________
PHPSimplicity
We don't need a reason to help people - Zidane [FF9]
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #3 (permalink)  
Old 03-26-06, 12:43 PM
dwoody dwoody is offline
Newbie Coder
 
Join Date: Jul 2004
Posts: 48
Thanks: 0
Thanked 0 Times in 0 Posts
Displaying the file

Thanks for your help, this is a start.

Here is my situation more clearly with some simplified code.
XML file

Code:
<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="tempStyles.xsl"?>

<mainMenu>
    
    <menuItem>
    	<label>Home</label>
    	<subItem>Home 1</subItem>
    	<subItem>Home 2</subItem>
    	<subItem>Home 3</subItem>
    </menuItem>	
    
    <menuItem>
    	<label>Services</label>
    	<subItem>Service 1</subItem>
    	<subItem>Service 2</subItem>
    </menuItem>

</mainMenu>
Here is the attached XSL sheet
Code:
<?xml version="1.0" encoding="ISO-8859-1"?>

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">
  <html>
  <body>
    
    <xsl:for-each select="mainMenu/menuItem">
    
       <xsl:value-of select="label"/><br/>
       
       	<xsl:for-each select="subItem">
       		<xsl:value-of select="../subItem"/><br/>
       	</xsl:for-each>	
       	<br/>
    
    </xsl:for-each>
    
  </body>
  </html>
</xsl:template>

</xsl:stylesheet>
If you save both these files to your computer and open the xml file on your computer you will get an output of the label for each menuItem followed by all the subItems. This is the intended output. Now I would like to simply include this xml file with its formatted xsl output into a php page. How do I go about this?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #4 (permalink)  
Old 03-26-06, 01:07 PM
mab's Avatar
mab mab is offline
Community VIP
 
Join Date: Oct 2005
Location: Denver, Co. USA
Posts: 2,674
Thanks: 0
Thanked 0 Times in 0 Posts
So what exact errors are you getting? Specifically, what is working and not working (so we don't have to guess or mind read)?

Also, see the following from the PHP manual -
Quote:
11. How am I supposed to mix XML and PHP? It complains about my <?xml tags!

In order to embed <?xml straight into your PHP code, you'll have to turn off short tags by having the PHP directive short_open_tags set to 0. You cannot set this directive with ini_set(). Regardless of short_open_tags being on or off, you can do something like:
<?php echo '<?xml'; ?>.
__________________
Error checking, error reporting, and error recovery. If your code does not have these to get it to tell you why it is not working, what makes you think someone in a programming forum will be able to tell you why it is not working???
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #5 (permalink)  
Old 03-26-06, 08:35 PM
dwoody dwoody is offline
Newbie Coder
 
Join Date: Jul 2004
Posts: 48
Thanks: 0
Thanked 0 Times in 0 Posts
XML and PHP

Ok. I have 3 files sitting on the server; a PHP file, an XML file, and an XSL file. Ideally what I would like to do is include the XML file in the PHP file such that all the contents of the XML file still get processed by the XSL code. If I type the path of the XML file into the browser it opens the XML file and outputs the data according to the XSL code attached inside of it (the XSL is just a style cheet for displaying XML). So far so good.

Now I would like to include this XML file within the PHP file and have it display the same way that it did when it was opened directly. Thus far I have tried two methods for doing this.

1) <?php include('menuData.xml'); ?>
The following error is reported:

Parse error: parse error, unexpected T_STRING in /home/temp/menuData.xml on line 1

This makes sense since the include function is not meant for XML files.

2) <?php echo file_get_contents('menuData.xml'); ?>
The following is produced:

" button_one.gif 1 - 1 1 - 2 1 - 3 button_two.gif 2 - 1 2 - 2 2 - 3 button_three.gif 3 - 1 3 - 2 3 - 3 "

This is just all the contents of the XML file.

What I would like produced is the same thing that was outputed when the XML file was opened directly by the browser. Any help on the matter is appreciated. The files are shown below.

menuData.xml
Code:
<?xml version="1.0" encoding="ISO-8859-1" ?>
<?xml-stylesheet type="text/xsl" href="menuStyle.xsl"?> 

<menu>
	<menuItem>
		<label>button_one.gif</label>
		
			<subItem> 1 - 1</subItem>
			<subItem> 1 - 2</subItem>
			<subItem> 1 - 3</subItem>
		
	</menuItem>
	<menuItem>
		<label>button_two.gif</label>
	
			<subItem> 2 - 1</subItem>
			<subItem> 2 - 2</subItem>
			<subItem> 2 - 3</subItem>
		
	</menuItem>
	<menuItem>
		<label>button_three.gif</label>
	
			<subItem> 3 - 1</subItem>
			<subItem> 3 - 2</subItem>
			<subItem> 3 - 3</subItem>
	
	</menuItem>
</menu>
menuStyle.xsl
Code:
<?xml version="1.0" encoding="ISO-8859-1" ?> 

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" version="1.0" encoding="UTF-8" indent="yes" /> 

<xsl:template match="/">
  <html>
  <body>
    <table border="1" width="165" height="*">
	    
	    <xsl:for-each select="menu/menuItem">
	    <tr>
	      <td colspan="2">		 
			  <img src="{label}"/>
	      </td>
	      
	    </tr>
	    <tr>
	      <td width="20">
	    			 
	      </td>
	      <td width="145">			
			  <table border="1" width="145">
				<xsl:for-each select="subItem">
			   	  <tr>
			        <td>
			      	  <xsl:value-of select="../subItem" />
			        </td>
			      </tr> 
			    </xsl:for-each>   
			  </table>  
		  </td>  
	    </tr>  
	      
	    </xsl:for-each>
    </table> 
    
  </body>
  </html>
</xsl:template>

</xsl:stylesheet>
menu.php
Code:
<?php
// Comment out one or the other to try them.

//include('menuData.xml');

echo file_get_contents('menuData.xml');

?>
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #6 (permalink)  
Old 03-26-06, 09:40 PM
mab's Avatar
mab mab is offline
Community VIP
 
Join Date: Oct 2005
Location: Denver, Co. USA
Posts: 2,674
Thanks: 0
Thanked 0 Times in 0 Posts
I tried both versions (include and file_get_contents) on a PHP5 system with short_open_tags = off and both versions work. See the attached image.

When short_open_tags are ON, I receive the error you have for the include case, but the file_get_contents version works. Note too that there is browser buffering going on. Exit your browser to cause it to completely reload the page after switching your .php file contents.

The magic quote settings will also affect the reading of text files. However, when I turned on magic quotes runtime, I got a specific error pointing out the slash in the xml line.

You should be able to get the file_get_contents(...) version to work...
Attached Images
File Type: jpg Untitled (Custom).jpg (14.8 KB, 332 views)
__________________
Error checking, error reporting, and error recovery. If your code does not have these to get it to tell you why it is not working, what makes you think someone in a programming forum will be able to tell you why it is not working???

Last edited by mab; 03-26-06 at 09:44 PM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #7 (permalink)  
Old 03-26-06, 09:49 PM
dwoody dwoody is offline
Newbie Coder
 
Join Date: Jul 2004
Posts: 48
Thanks: 0
Thanked 0 Times in 0 Posts
PHP and XML

Thanks a ton! I'm glad you got it to work. Could you tell my how to turn short_open_tags off. Also, does doing this have any other effects I should be aware of.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #8 (permalink)  
Old 03-26-06, 11:20 PM
mab's Avatar
mab mab is offline
Community VIP
 
Join Date: Oct 2005
Location: Denver, Co. USA
Posts: 2,674
Thanks: 0
Thanked 0 Times in 0 Posts
From the php manual -
short_open_tag is settable for PHP_INI_PERDIR for PHP versions > 4.0.0 and settable for PHP_INI_ALL in PHP versions <= 4.0.0.

PHP_INI_PERDIR means - Entry can be set in php.ini, .htaccess or httpd.conf

PHP_INI_ALL means - Entry can be set anywhere (including in php script using ini_set(...) )

The only effect on PHP code is if you are using <? instead of <?php and if you are using <?= instead of <?php echo. The short versions would need to be changed to the longer versions.

If this is your own server, turning short_open_tag off is a positive step, from both an XML standpoint, and for code portability. See the following note about this setting -
Quote:
; NOTE: Using short tags should be avoided when developing applications or
; libraries that are meant for redistribution, or deployment on PHP
; servers which are not under your control, because short tags may not
; be supported on the target server. For portable, redistributable code,
; be sure not to use short tags.
__________________
Error checking, error reporting, and error recovery. If your code does not have these to get it to tell you why it is not working, what makes you think someone in a programming forum will be able to tell you why it is not working???
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #9 (permalink)  
Old 04-04-06, 11:29 PM
dwoody dwoody is offline
Newbie Coder
 
Join Date: Jul 2004
Posts: 48
Thanks: 0
Thanked 0 Times in 0 Posts
Working...kindof

Hey,
I got it working! but only in Explorer . Does that make any sense that it would not work equally well in firefox? I viewed the source code for both situations and it was identical (as expected) so I dont think anything funny is going on in the PHP side of things but I may be wrong. In explorer I get the expected table, but in firefox i get what I had previously, I printout of:

" button_one.gif 1 - 1 1 - 2 1 - 3 button_two.gif 2 - 1 2 - 2 2 - 3 button_three.gif 3 - 1 3 - 2 3 - 3 "

If I open the XML file directly via the URL neither browser has a problem and displays it perfectly. When I use the file_get_contents('menuData.xml'); explorer outputs the correct display but firefox outputs what I just wrote above.
Why might this be?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #10 (permalink)  
Old 04-05-06, 02:20 AM
thomasantony thomasantony is offline
New Member
 
Join Date: Apr 2006
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Cool

Hi,
Try doing this.
PHP Code:

<?php

header
('Content-type: application/xml');
echo 
file_get_contents('menuData.xml');
?>
or this:

PHP Code:

<?php

header
('Content-type: text/xml');
echo 
file_get_contents('menuData.xml');
?>
Hey, that was my first post here. Hope it was helpful

Thomas
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Write in a XML file with php pallabmondal123 PHP 2 04-15-06 06:34 AM
PHP multi-dimensional array sorting issue aqw PHP 2 06-25-05 12:09 AM
Need XML to PHP Parsing Done angmi90 Job Offers & Assistance 1 04-28-05 02:42 PM
PHP and XML (Used for lottery statistics) v1brazy PHP 2 08-19-04 12:00 PM
Syndicating XML data into a website using PHP... Jackal05 PHP 3 07-16-04 03:38 PM


All times are GMT -5. The time now is 01:32 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.