php - cast simplexmlelement to string to get inner content but keep htmlspecialchars escaped -


i have xmlfile:

$xml = <<<eod <?xml version="1.0" encoding="utf-8"?> <metadata xmlns="http://www.test.com/" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation="test"> <qkc6b1hh0k9>testdata&amp;more</qkc6b1hh0k9> </metadata> eod; 

now loaded simplexmlobject , later on wanted inner of "qkc6b1hh0k9"-node

$xmlrootelem = simplexml_load_string( $xml ); $xmlrootelem->registerxpathnamespace( 'xmlns', "http://www.test.com/" );  // ...  $xpathelems = $xmlrootelem->xpath( './'."xmlns:qkc6b1hh0k9" ); $var = (string)($xpathelems[0]); var_dump($var); 

i expected string

testdata&amp;more 

... got

testdata&more 
  • why __tostring() method of simplexmlobject converting escaped specialchars normal chars? can deactivate behaviour?
  • i came temp-solution, consider dirty, say?

    (strip_tags($xpathelems[0]->asxml()))

  • may domdocument alternative?

thanks on questions!

edit

problem solved, problem not in __tostring method of simplexml, later on when using string addchild

the behaviour described above totaly fine , has expected can see in answers...

problems came up, when value added xml-document via "addchild". since addchild doesn't escape ampersand (http://www.php.net/manual/de/simplexmlelement.addchild.php#103587) 1 has manually.

if create xml tag, sane method, , set contain string "testdata&more", escaped testdata&amp;more. therefore logical extracting string content out reverses escaping procedure give text put in.

the question is, why want xml-escaped representation? if want content of element intended author, __tostring() doing right thing; there more 1 way of representing string in xml, data being represented should care about.

if reason need details of how xml constructed in particular instance, use more complex parsing framework such dom, separate testdata&amp;more text node (containing "testdata"), entity node (with name "amp"), , text node (containing "more").

if, on other hand, want put xml (or html) document, let simplexml unescaping properly, , re-escape @ appropriate time.


Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -