Re: Problem with DOM

From: Date: Tue, 12 May 2009 17:16:32 +0000
Subject: Re: Problem with DOM
References: 1  Groups: php.windows 
Request: Send a blank email to php-windows+get-29327@lists.php.net to get a copy of this message
> 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

« previous php.windows (#29327) next »