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 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号