开发者

Simple variable question in PHP and XML

开发者 https://www.devze.com 2023-02-03 08:23 出处:网络
I would like to add a variable ( $var ) inside the following code but I am having error... $employee = $xml->addChild(\'XXXXXXX\');

I would like to add a variable ( $var ) inside the following code but I am having error...

$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">

Thank you!


addChild takes a DOMNode as its argument, not text strings. 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>


This adds an attribute to the XML element:

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

精彩评论

暂无评论...
验证码 换一张
取 消