bobbinsbro Posted February 7, 2009 Share Posted February 7, 2009 hi guyz. i'm having a very weird problem with simpleXML. here's the troublesome bit of code: $xmlStr = ''; foreach ($securityXML->$permission->children() as $child){ if ($user != strip_tags($child->asXML())) $xmlStr .= $child->asXML(); } unset($child); $securityXML->$permission = $xmlStr; my problem is that $xmlStr contains valid xml (e.g., '<some_node>some_value</some_node> etc.) but after being set as the contents of $permission, the xml string looks like it was passed through htmlentities() (all the '<' are turned to <, all '>' are turned to > etc.). i tried appending $xmlStr as a child of $permission with the asChild() method, with the same results (only the xml string wasn't where i wanted it ). the main xml object looks something like this before i add the $xmlStr: <security> <perm1> <user>user_id</user> </perm1> <perm2> </perm2> <perm3> </perm3> </security> after i add the $xmlStr it looks like this: <security> <perm1> <user>user_id</user> </perm1> <perm2> <user>user_id</user> </perm2> <perm3> </perm3> </security> does any1 have any idea what's going on? Quote Link to comment https://forums.phpfreaks.com/topic/144233-solved-simplexml-problem/ Share on other sites More sharing options...
RichardRotterdam Posted February 7, 2009 Share Posted February 7, 2009 before reading everything just one question should the dollar character be removed on permission? $securityXML->permission you are accessing a variable within the object so the $ character should not be there Quote Link to comment https://forums.phpfreaks.com/topic/144233-solved-simplexml-problem/#findComment-756937 Share on other sites More sharing options...
bobbinsbro Posted February 7, 2009 Author Share Posted February 7, 2009 the $ should be there - the $permission variable contains the name of the node i am trying to change. e.g.: $permission = 'perm1'; $securityXML->$permission == $securityXML->perm1; //true Quote Link to comment https://forums.phpfreaks.com/topic/144233-solved-simplexml-problem/#findComment-756953 Share on other sites More sharing options...
bobbinsbro Posted February 8, 2009 Author Share Posted February 8, 2009 bump Quote Link to comment https://forums.phpfreaks.com/topic/144233-solved-simplexml-problem/#findComment-757311 Share on other sites More sharing options...
RichardRotterdam Posted February 8, 2009 Share Posted February 8, 2009 I am kind of confused what it is you want to have as result xml. let's say you have the following start xml <security> <perm1> <user>user_id</user> </perm1> <perm2> </perm2> <perm3> </perm3> </security> Now on what conditions and what exactly has to change in this xml file? Can you show a samle of xml what the result could be and under which conditions? Also if it's just the tags output that is incorrect you could use html_entity_decode Quote Link to comment https://forums.phpfreaks.com/topic/144233-solved-simplexml-problem/#findComment-757386 Share on other sites More sharing options...
bobbinsbro Posted February 8, 2009 Author Share Posted February 8, 2009 ok. in more depth. this is a permissions editing thing. the permissions are stored as an xml string in a DB. i get said string from the db, load it into a simplXMLElement. in the bit of code i pasted, i'm changing a user's permissions. heres the logic: if the posted permission value is different than the user's current permission{ add the user to the new permission node; scan through the old permission node of the user, and save all users into an xml string (the string will look something like this: <user>2</user><user>5</user>) save the xml string, now not containing the current user being edited, back into the permission node. } that's what the code posted above does. an example: before i run the code <security> <perm1> <user>1</user> <user>2</user> </perm1> <perm2> </perm2> <perm3> </perm3> </security> now, i edit user 2, and move him to perm2 node. this is the result in the DB: <security> <perm1> <user>1</user> </perm1> <perm2> <user>2</user> </perm2> <perm3> </perm3> </security> i dumped the contents of the temporary xml string (the variable $xmlStr) and the '<' '>' characters are intact. then i dumped the simpleXMLElement object after i set $xmlStr into the permission node, and the '<' changed to '<'. as i said, i tried doing the same thing with addChild() with the same results. i should note that in a different place in the same code i use addChild() and the xml is created just fine. is there a way to prevent simpleXML from htmlentities()-ing my xml string (since i think that this is a security measure of the simpleXML class)? Quote Link to comment https://forums.phpfreaks.com/topic/144233-solved-simplexml-problem/#findComment-757394 Share on other sites More sharing options...
RichardRotterdam Posted February 8, 2009 Share Posted February 8, 2009 try the following <?php //xml file in string change this to your db fetch $xmlString=<<<xml <security> <perm1> <user>1</user> <user>2</user> <user>3</user> </perm1> <perm2> <user>4</user> <user>5</user> </perm2> <perm3> </perm3> </security> xml; //create DOM $dom = new DOMDocument(); $dom->loadXML($xmlString); function user_change_permission($userId,$permLvl){ global $xmlString; global $dom; //first remove the user from the old permission $x = new DomXPath($dom); $userList=$x->query("//security/*/user[text()='{$userId}']"); foreach ($userList as $domElement){ $domNode = $dom->importNode($domElement, true); $parent = $domNode->parentNode; $parent->removeChild($domNode); } //now place it in the new permission $x = new DomXPath($dom); $permissionList=$x->query("//security/perm{$permLvl}"); foreach ($permissionList as $domElement){ $permissionNode = $dom->importNode($domElement, true); foreach ($userList as $domElement){ $domNode = $dom->importNode($domElement, true); $permissionNode->appendChild($domNode); } } } user_change_permission(1,2); $newXML = $dom->saveXML(); echo $newXML; Quote Link to comment https://forums.phpfreaks.com/topic/144233-solved-simplexml-problem/#findComment-757530 Share on other sites More sharing options...
bobbinsbro Posted February 8, 2009 Author Share Posted February 8, 2009 i will definitely use that. i was using simpleXML because i thought that it had less of a memory footprint (since it's a so much smaller class), but i didn't test my theory, which was a bit daft of me. i just ran a test, and the difference is about 100 bytes... i feel kinda silly. thanx Dj Kat. Quote Link to comment https://forums.phpfreaks.com/topic/144233-solved-simplexml-problem/#findComment-757538 Share on other sites More sharing options...
RichardRotterdam Posted February 8, 2009 Share Posted February 8, 2009 glad it works enjoy Quote Link to comment https://forums.phpfreaks.com/topic/144233-solved-simplexml-problem/#findComment-757539 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.