我想在以下代码中添加变量( $var )
,但我遇到错误...
$employee = $xml->addChild('XXXXXXX');
$employee->addChild('XXXXX', 'XXXXXX');
任何帮助都会很棒。
另外,我如何修改第一行,以便我可以添加属性?
例如<book ID="XXXX">
谢谢!
答案 0 :(得分:0)
addChild
将DOMNode作为其参数,而不是文本字符串。您需要创建节点并附加它们...例如:
//assuming $xml is a DOMDoucment
// <employee>XXX</employee>
$employee = $xml->createElement('employee', 'XXX');
// create an attribute node and a text nod to use for its value
$idNode = $xml->createAttribute('id');
$idValue = $xml->createTextNode('some-id');
// set the value of the attribute node
$idNode->appendChild($idValue);
// add the attribute to the element
$employee->appendChild($idNode);
// or more easily add an attribute
$employee->setAttribute('another', 'some-value');
// append the element to the document
$xml->appendChild($employee);
// final result:
// <root><employee id="some-id" another="some-value">XXX</employee></root>
答案 1 :(得分:0)
这为XML元素添加了一个属性:
$xml->addAttribute("id","1234");