Jump to content

[SOLVED] simpleXML problem


bobbinsbro

Recommended Posts

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 :P).

 

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?

Link to comment
Share on other sites

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

 

 

 

Link to comment
Share on other sites

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)?

Link to comment
Share on other sites

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; 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.