简体   繁体   English

PHP嵌套DOMDocument标签

[英]PHP nesting DOMDocument tags

DomDocument xml question: DomDocument xml问题:

want xml to look like this 希望xml看起来像这样

   <itunes:category text="Main Category">
       <itunes:category text="Sub category"/>
    </itunes:category>

but instead it looks like this 但是它看起来像这样

<itunes:category>text="Main Category"</itunes:category>
<itunes:category>text="Sub Category"</itunes:category>

here is the php: 这是PHP:

$chan3 = $chan->appendChild($xml->createElement('itunes:category','text="Main Category"'));
$chan3 = $chan->appendChild($xml->createElement('itunes:category', 'text="Sub Category"'));

Having trouble nesting these xml tags properly. 无法正确嵌套这些xml标记。 Don't want itunes:category to close the tag on the sub-category. 不需要itunes:category关闭子类别上的标签。 Do I use something other than createElement? 我是否使用了createElement以外的其他东西?

here the code 这里的代码

$xml= new DOMDocument();
$xml->formatOutput = true;
$chan = $xml->createElement('itunes:category');
$chan = $xml->appendChild($chan);
$chan->setAttribute('text','Main Category');
$chan->nodeValue = '<itunes:category text="Sub category"/>';
echo $xml->saveXML();

The result is ; 结果是;

<itunes:category text="Main Category">
       <itunes:category text="Sub category"/>
    </itunes:category>

Have a nice day. 祝你今天愉快。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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