簡體   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