-1

How could I inlcude the following php code in a xml file or how I change the xml file to a php file? Maybe SimpleXML is a solution, but I don't understand, how to use it.

You can find the live search script for what I need such a XML file here: http://www.w3schools.com/php/php_ajax_livesearch.asp

<?php
foreach ($verbs["a"] as $key => $list) {
echo '<link>'."\n";
echo '<title>'.($verbs["a"][$key]).'</title>'."\n";
echo '<url>a/'.letter($verbs["a"][$key]).'/</url>'."\n";
echo '</link>'."\n";
} 
?>
Grischa
  • 70
  • 8
  • 26
  • _“or how I change the xml file to a php file?”_ – by renaming it …? – CBroe Aug 30 '14 at 13:41
  • the script (a live search) don't work then. – Grischa Aug 30 '14 at 13:44
  • Probably because it does not recognize the output as XML then – so send an appropriate `Content-Type` header first. – CBroe Aug 30 '14 at 13:46
  • `header("Content-type: text/xml");` doesn't help me. – Grischa Aug 30 '14 at 13:56
  • And just _“it doesn’t work”_ doesn’t help us … so please add a problem description that makes this reproducible. – CBroe Aug 30 '14 at 13:59
  • Let me close it against the reference question for PHP and XML as your question is pretty vague. Keep in mind that you should ask about *own* code only, not just some code you stumble over on some site, spot it, and then have an itch to scratch along the way dropping it here. No thank you, esp. not from that site. Ask there instead, ask the original author, if you don't understand a code example for your needs yet. – hakre Aug 30 '14 at 16:03

2 Answers2

1

To elaborate on Jeff Clayton's answer, you'll probably want to look into PHP's support for actually creating XML documents. If your requirements are that you must have a .xml file and you must use PHP to create elements, SimpleXML has functions to meet both requirements:

http://php.net/manual/en/book.simplexml.php

You can instantiate a SimpleXMLElement object by reading what I assume is an existing XML file into a string.

$my_xml_file = file_get_contents('somexml.xml');

You can then use that string to instantiate a SimpleXMLElement:

$xml_for_parsing = new SimpleXMLElement($my_xml_file);

You can then add elements to your XML programmatically via the SimpleXMLElement object's addChild method.

Finally, you can use SimpleXMLElements asXML method to save the updated XML as an XML file.

If you require more robust (while arguably more complex) functionality, you might consider looking into PHP's DOM:

http://php.net/manual/en/book.dom.php

Derek
  • 827
  • 11
  • 23
0

XML files don't process PHP, but PHP can read or output XML files. Look into it in the reverse manner-- including XML within a PHP page. An example of this is SimpleXML (from the php manual online: http://php.net/manual/en/simplexml.examples-basic.php).

You can modify the header for PHP output so that a browser sees it as XML like so:

<?php
header('Content-Type: text/xml');
echo '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>';
?>
Jeff Clayton
  • 7,053
  • 1
  • 31
  • 36
  • I have already add `AddType application/x-httpd-php .xml` to my htaccess file but it don't work. How can I translate my php code into SimpleXML? – Grischa Aug 30 '14 at 13:54
  • It sounds like you are also asking about web server configuration here. There are modules such as php-xml that may not be installed in your PHP package. Check here for ways to do that: http://php.net/manual/en/dom.setup.php – Jeff Clayton Aug 30 '14 at 13:59
  • I can't install anything on my PHP package. – Grischa Aug 30 '14 at 14:02
  • There is a good read on XML with PHP 5 or newer here, with SimpleXML it may help you: http://www.phpfreaks.com/tutorial/handling-xml-data – Jeff Clayton Aug 30 '14 at 14:05