PHP dom - changing specific nodes

View previous topic View next topic Go down

Jack
Jack
Beta Tester
Beta Tester
Forum Posts : 8
Member Since : 2011-11-02

PostJack 29/7/2012, 9:12 pm

Hi,

I've created a PHP script that loads information from an XML file into an array and it works perfectly, but I am having problems trying to create a script that changes the values of a specific node. The part that I'm having trouble with is trying to distinguish which node is the right node to change.

The XML file stores data about users and has 3 elements in one group: id, name and pass, so I use two foreach loops to get the XML data, one for the tags and one for the three child nodes that each tag has.

The code that I've got so far is:
Code:

public function saveUserData($id,$field,$newValue) {
      $doc = new DOMDocument();
      $doc->load('users.xml');
      $x=0;
      $users = $doc->getElementsByTagName('user');
      foreach($users as $user) {
         foreach($user->childNodes as $i) {   
         }
      }
   }

Any help would be much appreciated Smile
LGforum
LGforum
Moderator
Forum Posts : 77
Member Since : 2011-11-12

PostLGforum 31/7/2012, 6:57 am

They have a nodeName property I believe, just like normal XML DOM.

Code:
if($i->nodeName && $i->nodeName === 'id' && $i->nodeValue == $id)
Each element, is a PHP DOMNode object, extended with DOMElement methods and properties too.
Jack
Jack
Beta Tester
Beta Tester
Forum Posts : 8
Member Since : 2011-11-02

PostJack 8/8/2012, 3:24 pm

It worked, thank you Smile
Sponsored content

PostSponsored content

View previous topic View next topic Back to top

Create an account or log in to leave a reply

You need to be a member in order to leave a reply.

Create an account

Join our community by creating a new account. It's easy!


Create a new account

Log in

Already have an account? No problem, log in here.


Log in

 
Permissions in this forum:
You cannot reply to topics in this forum