[英]Simple variable question in PHP and XML
I would like to add a variable ( $var )
inside the following code but I am having error... 我想在以下代码内添加变量
( $var )
,但是出现错误...
$employee = $xml->addChild('XXXXXXX');
$employee->addChild('XXXXX', 'XXXXXX');
Any help would be great. 任何帮助都会很棒。 In addition how can i modify the first line so that I can add an attribute to it?
另外,如何修改第一行,以便可以向其中添加属性? For example
<book ID="XXXX">
例如
<book ID="XXXX">
Thank you! 谢谢!
addChild
takes a DOMNode as its argument, not text strings. addChild
将DOMNode作为其参数,而不是文本字符串。 You need to create the nodes and append them... for example: 您需要创建节点并追加它们...例如:
//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>
这会将属性添加到XML元素:
$xml->addAttribute("id","1234");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.