Hello,

I want to write xml with php and Im using the code in this tutorial:

http://www.ibm.com/developerworks/library/os-xmldomphp/

I've made the php page and everything looks fine when I access the PHP file, I get the XML structure and all.

the problem is, it works only if the file in in .php extension. I want to call the file when the .xml extension, how to do that?

Thnx, and sorry for the probably noobish question.

Recommended Answers

All 6 Replies

Hi,

The only way I can think of to do this would be to add a rewrite rule in your .htaccess file:

RewriteEngine On
RewriteRule ^/path/yourfile.xml$   /path/yourfile.php

This will basically execute the php file in the background while the user only sees the .xml extension.

Thanks for the response, but this didn't help, now whenever I access the file with .xml extension I get an XML error pointing to the end of the php script (after the ?>) and when viewing the source of the page I can see the php script. so the php script was parsed as XML.

Any ideas?

thnx

sorry, I think I typed something wrong in the .htaccess file, now it works, but is it normal that I can't see the XML as a regular XML file? and I see the title of the book as the title of the page? is it because of this line:

$title = $doc->createElement( "title" );
  $title->appendChild(
  $doc->createTextNode( $book['title'] )
  );
  $b->appendChild( $title );

So probably I have 2 problems:

1. the files looks like an echo of a php, no pink background, no XML tags visible and so on.

2. the title of the page is the title of the book.

thnx

Is this problem only occurring if you access it using the .xml extension, or even when you are calling the .php directly?

Also, if you do a "view source", does the file look correct?

when viewing the source it looks like a regular XML:

<?xml version="1.0"?>
<books>
  <book>
    <title>Romeo and Juliet</title>
    <id>1</id>
    <desc>William Shakespeare</desc>
  </book>
</books>

The page looks exactly the same, whether I access it using .xml extension or .php

any ideas?

You are not trying to display the XML as-is, right? I think you should try to feed it to whoever/whatever will consume this generated xml and see if it works.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.