需要帮助使用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');
    ?>

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();
$jobs = simplexml_load_file('jobs.xml');
$jobid = 1;
foreach ($jobs->children() as $event) {
    if($event->attributes()->jobid == $jobid){
        $dom=dom_import_simplexml($event);
        $dom->parentNode->removeChild($dom);
    }
}

echo $jobs->asXML();