使用php将空子添加到xml

The basic situation is that I am writing a xml file and there I need an attribute named as and there will no value in that attribute, so shows up as whereas I want

The code is as below, this is the 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 is all about the people who make it 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 this is

        <?xml version="1.0" standalone="yes"?>
<movies type="documentary">
 <movie>
  <title>PHP: Behind the 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, this language. It's like, a programming language. Or is it a
   scripting language? All is revealed in this thrilling horror spoof
   of a documentary.
  </plot>
  <great-lines>
   <line>PHP solves all my 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 is all about the people who make it work.</plot><characters><character><name>Mr. Parser</name><actor>John Doe</actor></character></characters><rating type="stars">5</rating><test/></movie></movies>

But I need it to be

        <?xml version="1.0" standalone="yes"?>
<movies type="documentary">
 <movie>
  <title>PHP: Behind the 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, this language. It's like, a programming language. Or is it a
   scripting language? All is revealed in this thrilling horror spoof
   of a documentary.
  </plot>
  <great-lines>
   <line>PHP solves all my 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 is all about the people who make it 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 but they are using DomDocument, I am new to xml so unable to figure out how I can do the same in my code. The references that I have seen are question 1

question 2

I am totally stuck !!

If you really need an empty node like <node></node> do this:

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

Now the node is empty but shows up like <text></text>;

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

In XML, <test></test> and <test/> are entirely equivalent. As far as I know, processors are free to rewrite one to the other at will.

As such, the operation for choosing between them doesn't happen when you're manipulating the tree representation of the document, but when you're writing it out to a string.

SimpleXML doesn't currently support flags when saving XML using ->asXML(), but the DOM equivalent (->save() or ->saveXML()) does. Luckily, DOM and SimpleXML use the same internal representation so you can convert from one to the other with no performance penalty.

The flag you want is listed on http://php.net/manual/en/libxml.constants.php:

LIBXML_NOEMPTYTAG Expand empty tags (e.g. <br/> to <br></br>)

To use it, you would write:

$dom = dom_import_simplexml($sxe);
echo $dom->saveXML($dom->documentElement, LIBXML_NOEMPTYTAG);

Although you might be able to trick the XML library into creating a tree structure which it serializes as <test></test> by default (such as a zero-length text node), this could easily be broken by a new version of the library optimising away your trick. The difference will also disappear if you ever save the XML as a string and parse it again.