Re: Problem with DOM
| From: | Niel Archer | Date: | Tue, 12 May 2009 20:37:33 +0000 |
| Subject: | Re: Problem with DOM | ||
| References: | 1 2 | Groups: | php.windows |
| Request: | Send a blank email to php-windows+get-29330@lists.php.net to get a copy of this message | ||
> > Hi
> > I'm sorry, but I don't understand well what you are trying to tell me.....
> >
> > Let's imagine I want to modify an existent XML file called customers.xml
> >
> > I should do this steps
> >
> > $doc = new DOMDocument();
> > $doc->load('customers.xml');
> > $root = $doc->getElementsByTagName("customers"); // to take the root
> >
> > .... // create one customer
> >
> > $doc->save('customer.xml'); // to save the changes
> >
> > that is exactly what I did in the else part (in the if, in the case the file customers.xml
> > didn't exist, I create the file with only the root node).
> > Isn't is right?
>
> Apologies. I missed the '!' in the if and got the logic reversed.
>
> You do not say what does or does not happen on the second attempt? Am I
> correct in assuming there is no change?
The problem appears to be with the line:
$root->appendChild($customer);
in your addXMLCustomer method.
This give me the following error:
PHP Fatal error: Call to undefined method DOMNodeList::appendChild() in
D:\Scripts\PHP\TESTS\DOM-test.php
> > For what concerns the two type of DOMDocument istantiation.... I forgot to chance them
> > both :)
> >
> > Thks
> > Maurizio Bellemo
> > Sytel Reply Deutschland
> > __________________
> >
> > m.bellemo@reply.it
> > Prinzenalle 7
> > 40549 Düsseldorf
> > GERMANY
> > ________________________________________
> > From: Niel Archer [not@chance.now]
> > Sent: Tuesday, May 12, 2009 7:16 PM
> > To: php-windows@lists.php.net
> > Subject: Re: [PHP-WIN] Problem with DOM
> >
> > > Hi all,
> > >
> > > I'm trying to create some XML from PHP code to save data about customers, but I
> > > found many problem and it doesn't work.
> > >
> > > The class Customer is quite simple I put it right below
> > >
> > > <?php
> > >
> > > class customer {
> > > var $name;
> > > var $phone;
> > >
> > > function customer($name, $phone) {
> > > $this->name = $name;
> > > $this->phone = $phone;
> > > }
> > >
> > > function getName() {
> > > return $name;
> > > }
> > >
> > > function setName($name) {
> > > $this->name = $name;
> > > }
> > >
> > > function getPhone() {
> > > return $phone;
> > > }
> > >
> > > function setPhone($phone) {
> > > $this->phone = $phone;
> > > }
> > >
> > > function addXMLCustomer($dom, $root) {
> > > $customer = $dom->createElement('customer');
> > >
> > > $name_at = $dom->createAttribute('name');
> > > $customer->appendChild($name_at);
> > > $name_at_val = $dom->createTextNode($this->name);
> > > $name_at->appendChild($name_at_val);
> > >
> > > $phone_at = $dom->createAttribute('phone');
> > > $customer->appendChild($phone_at);
> > > $phone_at_val = $dom->createTextNode($this->phone);
> > > $phone_at->appendChild($phone_at_val);
> > >
> > > $root->appendChild($customer);
> > >
> > > }
> > > }
> > >
> > > ?>
> > >
> > > the main page is simply a little form asking to introduce a new customer.... (the
> > > problem is not when you try to create the XML file for the first time, but something on
> > > getElementsByTagName, that is when you try to reopen the file... for example for a second
> > > customer....
> > >
> > > <html>
> > > <head>
> > > <title>MCare administration</title>
> > > <script type="text/javascript"
> > > src="script/scriptaculous-js-1.8.2/prototype.js"></script>
> > > <script type="text/javascript"
> > > src="script/scriptaculous-js-1.8.2/scriptaculous.js"></script>
> > > </head>
> > > <?php
> > > // require class file
> > > require("customer.php");
> > >
> > > $xmlfile = "C:/Users/Maurizio/Desktop/customers.xml";
> > >
> > > if(!file_exists($xmlfile)) {
> > > $doc = new DOMDocument('1.0', 'iso-8859-1');
> > > $root = $doc->createElement('customers');
> > > $doc->appendChild($root);
> > >
> > > if (isset($_GET['add_customer']) &&
> > > isset($_GET['new_customer_name'])
> > > && isset($_GET['new_customer_phone'])) {
> > > $c = new customer($_GET['new_customer_name'],
> > > $_GET['new_customer_phone']);
> > > $c->addXMLCustomer($doc, $root);
> > > }
> > > $doc->save($xmlfile);
> > > }
> > > else {
> > > $doc = new DOMDocument();
> > > $doc->load($xmlfile);
> > > $root = $doc->getElementsByTagName("customers");
> > > if (isset($_GET['add_customer']) &&
> > > isset($_GET['new_customer_name'])
> > > && isset($_GET['new_customer_phone'])) {
> > > $c = new customer($_GET['new_customer_name'],
> > > $_GET['new_customer_phone']);
> > > $c->addXMLCustomer($doc, $root);
> > > }
> > > $doc->save($xmlfile);
> > > }
> > > ?>
> > > <body>
> > > <div>
> > > <fieldset><legend>Add new customer</legend>
> > > <form method="get" action="<?php echo
> > > $_SERVER['PHP_SELF']; ?>">
> > > <label for="new_customer_name">Name:</label>
> > > <input id="new_customer_name" name="new_customer_name"
> > > type="text" />
> > > <label for="new_customer_phone">Phone:</label>
> > > <input id="new_customer_phone" name="new_customer_phone"
> > > type="text" />
> > > <button id="add_customer" name="add_customer"
> > > type="submit">Add</button>
> > > </form>
> > > </fieldset>
> > > </div>
> > > </body>
> > > </html>
> > >
> > > Any suggestions??
> >
> > It looks to me that you are creating a new DOM object for the file
> > existing condition, but not loading the actual file into the object with
> > $doc->load() or similar. Consequently you are creating a new file with
> > single entry each time.
> > Also, I wonder why your new DOMDocument()s have different parameters?
> > Wouldn't it be wise to make them the same to avoid possible problems?
> >
> >
> > > Thks
> > > Maurizio
> > >
> > > --
> > > The information transmitted is intended for the person or entity to which it is
> > > addressed and may contain confidential and/or privileged material. Any review, retransmission,
> > > dissemination or other use of, or taking of any action in reliance upon, this information by persons
> > > or entities other than the intended recipient is prohibited. If you received this in error, please
> > > contact the sender and delete the material from any computer.
> > >
> > > --
> > > PHP Windows Mailing List (http://www.php.net/)
> > > To unsubscribe, visit: http://www.php.net/unsub.php
> > >
> >
> > --
> > Niel Archer
> > niel.archer (at) blueyonder.co.uk
> >
> >
> >
> > --
> > PHP Windows Mailing List (http://www.php.net/)
> > To unsubscribe, visit: http://www.php.net/unsub.php
> >
> >
> > --
> > The information transmitted is intended for the person or entity to which it is addressed
> > and may contain confidential and/or privileged material. Any review, retransmission, dissemination
> > or other use of, or taking of any action in reliance upon, this information by persons or entities
> > other than the intended recipient is prohibited. If you received this in error, please contact the
> > sender and delete the material from any computer.
> >
> > --
> > PHP Windows Mailing List (http://www.php.net/)
> > To unsubscribe, visit: http://www.php.net/unsub.php
> >
>
> --
> Niel Archer
>
>
>
> --
> PHP Windows Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
--
Niel Archer