add empty child to xml using php -


the basic situation writing xml file , there need attribute named , there no value in attribute, shows whereas want

the code below, basic example given in php.net

<?php  include 'example.php';  $sxe = new simplexmlelement($xmlstr); $sxe->addattribute('type', 'documentary');  $movie = $sxe->addchild('movie'); $movie->addchild('title', 'php2: more parser stories'); $movie->addchild('plot', 'this people make work.');  $characters = $movie->addchild('characters'); $character  = $characters->addchild('character'); $character->addchild('name', 'mr. parser'); $character->addchild('actor', 'john doe');  $rating = $movie->addchild('rating', '5'); $rating->addattribute('type', 'stars');  $movie->addchild('test');  echo $sxe->asxml();  ?> 

the result of

        <?xml version="1.0" standalone="yes"?> <movies type="documentary">  <movie>   <title>php: behind parser</title>   <characters>    <character>     <name>ms. coder</name>     <actor>onlivia actora</actor>    </character>    <character>     <name>mr. coder</name>     <actor>el act&#xd3;r</actor>    </character>   </characters>   <plot>    so, language. it's like, programming language. or    scripting language? revealed in thrilling horror spoof    of documentary.   </plot>   <great-lines>    <line>php solves web problems</line>   </great-lines>   <rating type="thumbs">7</rating>   <rating type="stars">5</rating>  </movie> <movie><title>php2: more parser stories</title><plot>this people make work.</plot><characters><character><name>mr. parser</name><actor>john doe</actor></character></characters><rating type="stars">5</rating><test/></movie></movies> 

but need

        <?xml version="1.0" standalone="yes"?> <movies type="documentary">  <movie>   <title>php: behind parser</title>   <characters>    <character>     <name>ms. coder</name>     <actor>onlivia actora</actor>    </character>    <character>     <name>mr. coder</name>     <actor>el act&#xd3;r</actor>    </character>   </characters>   <plot>    so, language. it's like, programming language. or    scripting language? revealed in thrilling horror spoof    of documentary.   </plot>   <great-lines>    <line>php solves web problems</line>   </great-lines>   <rating type="thumbs">7</rating>   <rating type="stars">5</rating>  </movie> <movie><title>php2: more parser stories</title><plot>this people make work.</plot><characters><character><name>mr. parser</name><actor>john doe</actor></character></characters><rating type="stars">5</rating>**<test></test>**</movie></movies> 

i have seen many other questions using domdocument, new xml unable figure out how can same in code. references have seen question 1

question 2

i totally stuck !!

if need empty node <node></node> this:

 $child = $sxe->addchild('test');  $child[0] = ''; 

now node empty shows <text></text>;

 print htmlentities($sxe->asxml()); 

Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

ios - Change Storyboard View using Seague -