Page 1 of 1

XML DOM removeChild issues.

Posted: Mon Oct 16, 2006 5:55 am
by mikeeeeeeey
Happy Monday everyone.

Got a wee bit of a problem. Been using SimpleXML to parse and edit XML through PHP. And let me just say it's awesome, so simple (like the name).

But now the time has come to add the abililty to delete a whole set of elements, under one node (which will be taken from a variable passed in through the URL).

Code: Select all

$num = (int)$_GET['delete'];

$doc = new DOMDocument;
$doc->load('caseStudy.xml');

$presentation = $doc->documentElement;

// I'm pretty sure that the line underneath is wrong
$slide = $presentation->item('slide[$num]');
$oldslide = $presentation->removeChild($slide);

echo $doc->saveXML();
It's spewing objects and allsorts at me

Code: Select all

Fatal error: Call to undefined method DOMElement::item() in C:\wamp\www\xml_pres\TMP6cdd6786an.php on line 18
Anyone got any ideas how I can get this working?

Thanks in advance you lovely Monday Morning folk :D

Posted: Mon Oct 16, 2006 6:36 am
by volka
$num = (int)$_GET['delete'];
You want to delete the $numth element?
$slide = $presentation->item('slide[$num]');
$presentation is a DOMElement, but neither DOMElement nor it parent DOMNode defined a method item(). 'slide[$num]' would be passed as-is, there's no variable substitution in single-quoted strings. And what would slide be in this context?

Posted: Mon Oct 16, 2006 8:57 am
by Ollie Saunders
Perhaps

Code: Select all

$slide = $presentation->childNodes->item($num);
$oldslide = $presentation->removeChild($slide);

Posted: Mon Oct 16, 2006 9:00 am
by mikeeeeeeey
:?

there's something horribly wrong with that line of code, I know. but I've been exposed to DOM XML for only a couple of hours and I'm having trouble understanding what your saying.

I've tried parsing that line of code as everything I can think of, the main being:

Code: Select all

$presentation = $doc->presentation->documentElement;
$slide = $presentation->slide[$num];
but that's what giving me the errors.

if this was simpleXML, the above would work. but DOM sucks, 'cos I'm stuck!

Posted: Mon Oct 16, 2006 9:06 am
by volka
Please explain in more detail what you have and what you want to do.
An example xml document and an example GET parameter delete would also be great.

Posted: Mon Oct 16, 2006 9:18 am
by Ollie Saunders
there's something horribly wrong with that line of code, I know. but I've been exposed to DOM XML for only a couple of hours and I'm having trouble understanding what your saying.
DOM isn't the same as DOMXML, make sure you are looking at the correct documentation; DOM not DOMXML.
I've tried parsing that line of code
What my two lines?

Posted: Mon Oct 16, 2006 9:29 am
by mikeeeeeeey
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Right ok...here we go:

I'm building a small scale CMS for a presentation system, which uses a programmed .swf to format an XML document into slides. To update this XML document, I'm building a series of PHP pages (4 in total); a parsing page, editing page, adding new slide page (you'll see what I mean from the XML schema) and a deletion page. The editing and deleting pages work by taking url variables from the parsing page, such as;

[syntax="html"]<a href="edit.php?edit=1">go to editing page</a>
<a href="delete.php?delete=1">go to delete page</a>

etc. etc.

the XML document looks like...

Code: Select all

<?xml version="1.0" encoding="iso-8859-1"?>
<presentation> 
  <slide id="0">
    <element type="title">content</element>
    <element type="person">content</element>
    <element type="company">content</element>
    <element type="presenter">content</element>
    <element type="image">content</element>
  </slide>
  <slide id="1">
    <element...


all the sections work fine, except the deleting. this is because the rest of the site is programmed in SimpleXML, which does not have any built in methods for removing or deleting nodes, attributes or children. this is why I have had to use DOM XML for this section.

the code I'm using for this is[/syntax]

Code: Select all

$doc = new DOMDocument;
$doc->load('caseStudy.xml');

$presentation = $doc->documentElement;

// I'm pretty sure that the line underneath is wrong
$slide = $presentation->item('slide[$num]');
$oldslide = $presentation->removeChild($slide);

echo $doc->saveXML();
but this doesn't work, since its giving me errors.

the url for this page would look like delete.php?delete=3 with the number (delete=3) meaning the number child of presentation that should be deleted.


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Mon Oct 16, 2006 9:32 am
by volka
mikeeeeeeey wrote:the url for this page would look like delete.php?delete=3 with the number (delete=3) meaning the number child of presentation that should be deleted.
The important part is missing ;)

delete=3 => third <slide> element in document order
or
delete=3 => <slide id="3">
?

Posted: Mon Oct 16, 2006 9:57 am
by mikeeeeeeey
ahh. sorry :(

delete.php?delete=3 would be the third slide in the XML document i.e. slide[3]

I'd use the 'id' attribute for this type of referencing usually (esp database work) but in XML it seems troublesome, so instead I just use id for part of the front-end (flash) for ordering.

and off the point a bit, but volka, your probably the most dedicated person I've met in a while. kudos to you :D

Posted: Mon Oct 16, 2006 10:37 am
by volka
Quick&dirty

Code: Select all

<?php
$doc = simplexml_load_file('caseStudy.xml');

unset($doc->slide[2]);
echo dom_import_simplexml($doc)->ownerDocument->saveXML();
?>
but
mikeeeeeeey wrote:I'd use the 'id' attribute for this type of referencing usually (esp database work) but in XML it seems troublesome, so instead I just use id for part of the front-end (flash) for ordering.
you might be interested in the powers of xpath ;)

Code: Select all

<presentation>
  <slide id="0">
    <element type="title">a</element>
  </slide>
  <slide id="1">
    <element type="title">b</element>
  </slide>
  <slide id="2">
    <element type="title">c</element>
  </slide>
  <slide id="3">
    <element type="title">d</element>
  </slide>
  <slide id="4">
    <element type="title">e</element>
  </slide>
</presentation>

Code: Select all

<?php
$id = 3;
$doc = new DOMDocument;
$doc->load('caseStudy.xml') or die('cannot load xml doc');

$xpath = new DOMXPath($doc);
$nodelist = $xpath->query('//slide[@id='.$id.']');

if ( 0<$nodelist->length ) {
	$slide = $nodelist->item(0);
	$slide->parentNode->removeChild($slide);
}
echo $doc->saveXML();
?>
mikeeeeeeey wrote:and off the point a bit, but volka, your probably the most dedicated person I've met in a while. kudos to you :D
but can be all grumpy and unfair, too. ph34r m3 :twisted:

Posted: Mon Oct 16, 2006 10:46 am
by mikeeeeeeey
Ahh told XPath!

I orginally tried to do all this using XSLT, XPath and DOM XML, but the woman who wrote XML and PHP put me off it, by making it really hard to understand. Praise be XML.

Oh and thanks a lot, the XPath stuff works, you mass legend.

And with a name like volka...pffft. I could never phear ye', only give you huge amounts of respect!

Peace one time.