简体   繁体   中英

How to close an xml element using PHP DOM?

I've been using PHP DOM to create and save an xml file (third party xml file). So far i've got the structure right, but have come across the problem of an element not closing with a closing tag ie

<FX>
 <Stompbox ID="1">
  <Module ID="0" POS="0" BypassState="1"/>
 </Stompbox>
</FX>

Should look like this - Module closing tag

<FX>
 <Stompbox ID="1">
  <Module ID="0" POS="0" BypassState="1"></Module>
 </Stompbox>
</FX>

Here's the code

$xmlRoot = $domtree->appendChild($xmlRoot); 
    /* Add FX node */
$fx = $domtree->createElement("FX");
$fx = $xmlRoot->appendChild($fx);
    /* Add Stompbox node */
$fx->appendChild($stompbox = $domtree->createElement('Stompbox')); 

$attr_mod = new DOMAttr('ID', "1");
$stompbox->setAttributeNode($attr_mod);
$stompbox->appendChild($module = $domtree->createElement('Module'));

$attr_mod = new DOMAttr('ID', "0");
$module->setAttributeNode($attr_mod);
$attr_pos = new DOMAttr('POS', '0'); 
$module->setAttributeNode($attr_pos); 
$attr_bypass_state = new DOMAttr('BypassState', '1'); 
$module->setAttributeNode($attr_bypass_state);

Thanks for your help.

The element is closed. There are two ways of closing an element in XML:

by the self-closing:

 <someelement someattr="somevalue" />

Or with an empty innner:

 <someelement someattr="somevalue"></someelement>

They are semantically identical and many implementations choose for the former, because it results in smaller and more readable files.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM