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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?