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 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?