dqtok88424 2018-01-24 07:47
浏览 71
已采纳

如何用PHP将内容添加到xml?

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>
  • 写回答

2条回答 默认 最新

  • duanliang1999 2018-01-24 08:45
    关注

    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();
        }
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。
  • ¥20 CST怎么把天线放在座椅环境中并仿真