Hi.
In a class to manage xml file
I've got this method:
public function delete($id){
if(!is_numeric($id)){
throw new InvalidArgException('Invalid parameter in delete method.Parameter ['.$id.'] is not a number in ['.__CLASS__.']');
}
$query= "/items/{$this->item}[id/text()='{$id}']";
$items= $this->xp->query($query);
if(is_null($items->item(0))) {
throw new InvalidArgException('Error deleting item ['.$id.'] in ['.__CLASS__.']');
}
$this->doc->documentElement->removeChild($items->item(0));
$this->xml->save();
}
and for instance this xml file:
<?xml version="1.0" encoding="UTF-8"?>
<items>
<item>
<id>1</id>
<date>10 Aprile 2007</date>
<content>First post</content>
</item><item>
<id>2</id>
<date>10 Aprile 2007</date>
<content>Second Post </content>
</item>
</items>
if there is more than one item it works fine
<?xml version="1.0" encoding="UTF-8"?>
<items>
<item>
<id>2</id>
<date>10 Aprile 2007</date>
<content>Second Post </content>
</item>
</items>
but if I've only a item I've got this:
<?xml version="1.0" encoding="UTF-8"?>
<items/>
Have you got some ideas why ????????
Bye.Is it a problem? <element/> is a perfectly legal way to denote an element that doesn't have any children (an "empty tag") and equivalent to <element></element>. You'll note that adding children to it works. On the other hand,
<element>
</element>
does not indicate an empty element, because it contains a text node.
(The converse of this is the fact that <br></br> legally denotes a br element in XHTML.)
If it is a problem (the spec suggests that "for interoperability" it may be - but only if the XML is being validated....) then using the LIBXML_NOEMPTYTAG option when you save() ought to take care of it.Thanks a lot for the replay
Is it a problem? <element/> is a perfectly legal way to denote an element that doesn't have any children (an "empty tag") and equivalent to <element></element>. You'll note that adding children to it works. On the other hand,
<element>
</element>
I supposed that
Bye.
In a class to manage xml file
I've got this method:
public function delete($id){
if(!is_numeric($id)){
throw new InvalidArgException('Invalid parameter in delete method.Parameter ['.$id.'] is not a number in ['.__CLASS__.']');
}
$query= "/items/{$this->item}[id/text()='{$id}']";
$items= $this->xp->query($query);
if(is_null($items->item(0))) {
throw new InvalidArgException('Error deleting item ['.$id.'] in ['.__CLASS__.']');
}
$this->doc->documentElement->removeChild($items->item(0));
$this->xml->save();
}
and for instance this xml file:
<?xml version="1.0" encoding="UTF-8"?>
<items>
<item>
<id>1</id>
<date>10 Aprile 2007</date>
<content>First post</content>
</item><item>
<id>2</id>
<date>10 Aprile 2007</date>
<content>Second Post </content>
</item>
</items>
if there is more than one item it works fine
<?xml version="1.0" encoding="UTF-8"?>
<items>
<item>
<id>2</id>
<date>10 Aprile 2007</date>
<content>Second Post </content>
</item>
</items>
but if I've only a item I've got this:
<?xml version="1.0" encoding="UTF-8"?>
<items/>
Have you got some ideas why ????????
Bye.Is it a problem? <element/> is a perfectly legal way to denote an element that doesn't have any children (an "empty tag") and equivalent to <element></element>. You'll note that adding children to it works. On the other hand,
<element>
</element>
does not indicate an empty element, because it contains a text node.
(The converse of this is the fact that <br></br> legally denotes a br element in XHTML.)
If it is a problem (the spec suggests that "for interoperability" it may be - but only if the XML is being validated....) then using the LIBXML_NOEMPTYTAG option when you save() ought to take care of it.Thanks a lot for the replay
Is it a problem? <element/> is a perfectly legal way to denote an element that doesn't have any children (an "empty tag") and equivalent to <element></element>. You'll note that adding children to it works. On the other hand,
<element>
</element>
I supposed that
Bye.