我正在使用php编写xml。我想用元素写属性。我的代码如下
$writer = new XMLWriter();
$writer->openURI('php://output');
$writer->startDocument('1.0','UTF-8');
$writer->setIndent(4);
$writer->startElement('Response');
$writer->writeElement("Dial","+111111111");
$writer->endElement();
$writer->endDocument();
$writer->flush();
这会产生此输出
<response>
<dial>+111111</dial>
</response>
但我想要这样的东西
<response>
<dial action="myaction">+111111</dial>
</response>
我试过这个
$writer->writeAttribute('action', 'myaction');
但是这只添加了startElement而不是writeElement。 感谢
答案 0 :(得分:5)
而不是行$writer->writeElement("Dial","+111111111");
您应该在某些步骤中编写元素以添加属性
$writer->startElement("Dial");
$writer->writeAttribute('action', "myaction");
$writer->text("+111111111");
$writer->endElement();
<强> demo on eval.in 强>