繁体   English   中英

PHP和XML中的简单变量问题

[英]Simple variable question in PHP and XML

我想在以下代码内添加变量( $var ) ,但是出现错误...

$employee = $xml->addChild('XXXXXXX');
$employee->addChild('XXXXX', 'XXXXXX');

任何帮助都会很棒。 另外,如何修改第一行,以便可以向其中添加属性? 例如<book ID="XXXX">

谢谢!

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>

这会将属性添加到XML元素:

    $xml->addAttribute("id","1234");

暂无
暂无

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

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