Simple and clean xml manipulation in PHP -


i'm trying search way modify xml in php. php documentation confusing regarding how manipulate xml. how simplexml allows finding tags/attributes, doesn't seem allow add child trees, or replace existing ones.

any suggestion on use?

my use case includes:

  • finding specific tag elements specific attributes.
  • replacing found element subtree.
  • using child trees generated xml text.

i use xpath , simplexml change file. little example...

the xml file:

<?xml version="1.0"?>  <forum uri="http://myforum.org/index.php">      <item id="1">         <title>first post!!!</title>         <link>http://myforum.org/index.php/m/1</link>         <description>hello i'm fabio</description>     </item>      <item id="2">         <title>re: second post!!!</title>         <link>http://myforum.org/index.php/m/2</link>         <description>2nd message.</description>     </item> </forum> 

and php handler:

<?php  $forum = simplexml_load_file('forum.xml');  /* xpath examples */    /* catch items in forum */ $result = $forum->xpath('/forum/item'); /* catch links */ $result = $forum->xpath('//link'); /* search "re:" in title , returns item's id */ $result = $forum->xpath('//item[contains(title, "re:")]/@id'); /* catch > 10 length items , returns item's title*/ $result = $forum->xpath('//item[string-length(description) > 10]/title');  $forum->item[1]->title['url']   = "http://goo.gl/";     /* add attribute */ $forum->item[0]->foo            = "newnode";            /* add content */ $forum->item[0]->foo['attrib']  = 10;                   /* add value */ $forum->addchild('element_name', 'value');              /* new element /*   /* delete value */ unset($forum->item[0]);   // xml rendering echo $forum->asxml(); 

Comments

Popular posts from this blog

Add email recipient to all new Trac tickets -

400 Bad Request on Apache/PHP AddHandler wrapper -

php - Change action and image src url's with jQuery -