Jump to content

simplexml, saving < and >


Aimar

Recommended Posts

Hi there,

 

I have small script like this:

$lines->section[4]->page[2]->texts->pageText[1] = "<![CDATA[$new_value]]>";
file_put_contents('../25257_main.xml', $lines->asXml()); 

 

Problem is - it's not saving <![CDATA... - it converts "<" and ">" to entities ">"

Is there any wayaround? I need it to be saved with < >

 

Thanks in advance,

Aimar

Link to comment
Share on other sites

I do not think it would be valid XML if you were to do the actual < and >. As it would break syntax.

 

Acutally, it doesn't break syntax, so it's not a problem.

 

Instead of trying to get it to be the actual < why not just decode the entitiy on retrieval / display using html_entity_decode()?

 

Can't do that - .xml file is being loaded by flash website (.swf) - so best solution for it would be to save it with < > in .xml before flash reads it.

 

Maybe there's another function in php which could save it without dropping < > chars?

Or any other way around?

 

Thanks for any hint.

Link to comment
Share on other sites

OK, problem solved with this class:

 

class SimpleXMLExtended extends SimpleXMLElement 
{ 
  public function addCData($cdata_text) 
  { 
    $node= dom_import_simplexml($this); 
    $no = $node->ownerDocument; 
    $node->appendChild($no->createCDATASection($cdata_text)); 
  }
}

 

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.