dsjhejw3232 2013-11-14 22:38
浏览 46
已采纳

需要帮助使用PHP删除xml中的子项

I am trying to delete child from my jobs.xml file using a php script.

the way my jobs.xml looks like is :

    <jobs>
    <event jobid="1">
    <title>jobtitle</title>
    <desc>description</desc>
    <date>postdate</date>
    </event>
    <event jobid="2">
    <title>jobtitle</title>
    <desc>description</desc>
    <date>postdate</date>
    </event>
    <event jobid="3">
    <title>jobtitle</title>
    <desc>description</desc>
    <date>postdate</date>
    </event>
    </jobs>

I have created a php script where I take jobid from the user and on submit and delete the event child who has that jobid.

but the problem is that when i create a new job i get a duplicate jobid as when I create new job event I use $jobid = $xmlobj->count() + 1;

Could some one help me in this. I prefer to use php script but java script will be fine too.

EDIT :

Here's my code to delete :

    <?php
    $jobs = simplexml_load_file('jobs.xml');
    $jobid = $_POST['jobid'];
    foreach ($jobs->children() as $event) {
    if($event->attributes()->jobid == $jobid)
    {
        $dom=dom_import_simplexml($event);
    $dom->parentNode->removeChild($dom);
    }
    }
    $jobs->asXML('jobs.xml');
    ?>
  • 写回答

2条回答 默认 最新

  • du12197 2013-11-14 23:29
    关注

    Please note: this only work for newly created files. Existing files will need to manual add next_jobid="N" on the root element <jobs>.

    Welcome to further improvements:

    class JobsXML
    {
        public function __construct($filename)
        {
            $this->filename = $filename;
            $this->dom = new DOMDocument;
            $this->dom->formatOutput = true;
            @$this->dom->load($filename);
            $this->xpath = new DOMXPath($this->dom);
    
            if ($this->xpath->query('//jobs')->length == 0) {
                $this->root = $this->dom->createElement('jobs');
                $this->root->setAttribute('next_jobid', 1);
                $this->dom->appendChild($this->root);
            } else {
                $this->root = $this->xpath->query('//jobs')->item(0);
            }
        }
    
        public function insertEvent($title, $desc, $date)
        {
            $next_jobid = $this->root->getAttribute('next_jobid');
            $event = $this->dom->createElement('event');
            $event->setAttribute('jobid', $next_jobid);
            $event->appendChild($this->dom->createElement('title', $title));
            $event->appendChild($this->dom->createElement('desc', $desc));
            $event->appendChild($this->dom->createElement('date', $date));
            $this->root->appendChild($event);
            $this->root->setAttribute('next_jobid', intval($next_jobid) + 1);
        }
    
        public function removeEvent($jobid)
        {
            foreach ($this->xpath->query("//event[@jobid=$jobid]") as $node) {
                $node->parentNode->removeChild($node);
            }
        }
    
        public function save()
        {
            $this->dom->save($this->filename);
        }
    }
    

    Inserting event:

    $jobs = new JobsXML('jobs.xml');
    $jobs->insertEvent('jobtitle', 'description', 'postdate');
    $jobs->save();
    

    Removing event:

    $jobs = new JobsXML('jobs.xml');
    $jobs->removeEvent(1);
    $jobs->save();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 路易威登官网 里边的参数逆向
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?
  • ¥50 需求一个up主付费课程