-1

I want to copy the content from the PHP file into XML:

$channel = "15";
$start = "20180124021100 +0200";
$stop = "20180124031500 +0200";
$title = "news";
$desc = "show number 50";

I need to do later loop and add more channels, so I do not want only to edit the XML,

I want to open the XML file and add the PHP content to the page in a specific location:

XML:

<?xml version="1.0" encoding="UTF-8"?>

<channel id="1">
<display-name lang="he">1</display-name>
</channel>

// I want the content to stick to the end

<programme start="$start" stop="$stop" channel="$number">
<title lang="he">$title</title>
<desc lang="he">$desc</desc>
</programme>

// And that the next loop will be the same, will paste the content to the end as well:

<programme start="$start" stop="$stop" channel="$number">
<title lang="he">$title</title>
<desc lang="he">$desc</desc>
</programme>
dizzy
  • 37
  • 8

2 Answers2

1

SimpleXML for PHP is fairly simple to use, either for reading or writing XML. In this question How to convert array to SimpleXML you can find a number of suggestions on using SimpleXML in PHP. Documentation can be found here: https://secure.php.net/manual/en/book.simplexml.php

chrwahl
  • 8,675
  • 2
  • 20
  • 30
1

Rather than using strings to build the XML you ought to look at one of the existing classes to do all the heavy lifting - they are after all designed for this purpose. Looking at the xml in the question it does not appear valid XML as there is no root node for the content. The following ought to help get started.

<?php

    $channel = "15";
    $start = "20180124021100 +0200";
    $stop = "20180124031500 +0200";
    $title = "news";
    $desc = "show number 50";




    /* file path to saved xml document */
    $xmlfile= __DIR__ . '/xmlfile.xml';

    /* whether to display XML in browser or simply save */
    $displayxml=true;


    /* create DOMDocument and set args */
    $dom=new DOMDocument('1.0','utf-8');
    $dom->standalone=true;
    $dom->formatOutput=true;
    $dom->recover=true;
    $dom->strictErrorChecking=true;

    /* a ROOT node for the document */
    $rootnode='programmes';

    /* if the file already exists, open it */
    if( realpath( $xmlfile ) ){

        $dom->load( $xmlfile );
        $root=$dom->getElementsByTagName( $rootnode )->item(0);

    } else {
        /* File does not exist, create root elements & content */
        $root=$dom->createElement( $rootnode );
        $dom->appendChild( $root );

        $ochan=$dom->createElement('channel');
        $ochan->setAttribute('id',1);
        $root->appendChild( $ochan );

        $odisp=$dom->createElement('display-name',1);
        $odisp->setAttribute('lang','he');
        $ochan->appendChild( $odisp );      
    }


    /* process variables for new record/programme */
    if( isset( $channel, $start, $stop, $title, $desc ) ){

        $oprog=$dom->createElement('programme');
        $root->appendChild( $oprog );

        $oprog->setAttribute('start',$start);
        $oprog->setAttribute('end',$stop);
        $oprog->setAttribute('channel',$channel);


        $otitle=$dom->createElement('title',$title);
        $oprog->appendChild($otitle);
        $otitle->setAttribute('lang','he');


        $odescr=$dom->createElement('desc',$desc);
        $oprog->appendChild($odescr);
        $odescr->setAttribute('lang','he');
    }
    /* save the file */
    $dom->save( $xmlfile );

    /* if you want to see the generated xml in the browser */
    if( $displayxml ){
        header('Content-Type: application/xml');
        echo $dom->saveXML();
    }
?>
Professor Abronsius
  • 33,063
  • 5
  • 32
  • 46
  • Thank you very much :) I appreciate your work, works great! – dizzy Jan 24 '18 at 09:20
  • have option to change the name of rootnode to 'tv'? Instead programmes. I tried to replace the name but an error was received – dizzy Jan 24 '18 at 10:06