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');
    ?>

图片转代码服务由CSDN问答提供 功能建议

我正在尝试使用php脚本从我的jobs.xml文件中删除子项。 \ n

我的jobs.xml的样子是:

 &lt; jobs&gt; 
&lt; event jobid =“1”&gt; 
&lt; title&gt;  jobtitle&lt; / title&gt; 
&lt; desc&gt; description&lt; / desc&gt; 
&lt; date&gt; postdate&lt; / date&gt; 
&lt; / event&gt; 
&lt; event jobid =“2”&gt; 
&lt;  title&gt; jobtitle&lt; / title&gt; 
&lt; desc&gt; description&lt; / desc&gt; 
&lt; date&gt; postdate&lt; / date&gt; 
&lt; / event&gt; 
&lt; event jobid =“3”&gt; 
  &lt; title&gt; jobtitle&lt; / title&gt; 
&lt; desc&gt;说明&lt; / desc&gt; 
&lt; date&gt; postdate&lt; / date&gt; 
&lt; / event&gt; 
&lt; / jobs&gt; 
   
 
 

我创建了一个php脚本,我从用户处获取jobid并提交并删除具有该jobid的事件子项。

但是 问题是,当我创建一个新工作时,我得到一个重复的jobid,因为当我创建新的工作事件时,我使用$ jobi d = $ xmlobj-&gt; count()+ 1;

有人可以帮助我。 我更喜欢使用php脚本,但java脚本也可以。

编辑:

这是我要删除的代码: <前> &lt;?php $ jobs = simplexml_load_file('jobs.xml'); $ jobid = $ _POST ['jobid']; foreach($ jobs-&gt; children( )作为$ event){ if($ event-&gt; attributes() - &gt; jobid == $ jobid) { $ dom = dom_import_simplexml($ event); $ dom-&gt; parentNode- &gt; removeChild($ dom); } } $ jobs-&gt; asXML('jobs.xml'); ?&gt;

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 收藏
  • 邀请回答

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条)

相关推荐 更多相似问题