将 SimpleXMLElement 子项复制到不同的对象
Copy SimpleXMLElement children to a different object
给定两个 SimpleXMLElement
结构如下(相同)的对象:
$obj1
object SimpleXMLElement {
["@attributes"] =>
array(0) {
}
["event"] =>
array(1) {
[0] =>
object(SimpleXMLElement) {
["@attributes"] =>
array(1) {
["url"] =>
string "example.com"
}
}
}
}
$obj2
object SimpleXMLElement {
["@attributes"] =>
array(0) {
}
["event"] =>
array(1) {
[0] =>
object(SimpleXMLElement) {
["@attributes"] =>
array(1) {
["url"] =>
string "another-example.com"
}
}
}
}
我正在尝试将所有子 event
项目从第二个对象复制到第一个对象:
foreach ($obj2->event as $event) {
$obj1->event[] = $event
}
它们移动了,但复制的对象现在是空的。
$obj1
object SimpleXMLElement {
["@attributes"] =>
array(0) {
}
["event"] =>
array(2) {
[0] =>
object(SimpleXMLElement) {
["@attributes"] =>
array(1) {
["url"] =>
string "example.com"
}
}
[1] =>
object(SimpleXMLElement) {
}
}
}
SimpleXML 的编辑功能相当有限,在这种情况下,您所做的赋值和 ->addChild
都只能设置新元素的文本内容,而不能设置其属性和子元素。
在这种情况下,您可能需要更复杂的功能 DOM API。幸运的是,您可以在 PHP 中混合使用两者,几乎没有任何损失,使用 dom_import_simplexml
and simplexml_import_dom
切换到另一个包装器。
获得 DOM 表示后,您可以在要编辑的 文档 上使用 the appendChild
method of DOMNode
to add a full node, but there's a catch - you can only add nodes "owned by" the same document. So you have to first call the importNode
method。
把它们放在一起,你会得到这样的东西:
// Set up the objects you're copying from and to
// These don't need to be complete documents, they could be any element
$obj1 = simplexml_load_string('<foo><event url="example.com" /></foo>');
$obj2 = simplexml_load_string('<foo><event url="another.example.com" /></foo>');
// Get a DOM representation of the root element we want to add things to
// Note that $obj1_dom will always be a DOMElement not a DOMDocument,
// because SimpleXML has no "document" object
$obj1_dom = dom_import_simplexml($obj1);
// Loop over the SimpleXML version of the source elements
foreach ($obj2->event as $event) {
// Get the DOM representation of the element we want to copy
$event_dom = dom_import_simplexml($event);
// Copy the element into the "owner document" of our target node
$event_dom_copy = $obj1_dom->ownerDocument->importNode($event_dom, true);
// Add the node as a new child
$obj1_dom->appendChild($event_dom_adopted);
}
// Check that we have the right output, not trusting var_dump or print_r
// Note that we don't need to convert back to SimpleXML
// - $obj1 and $obj1_dom refer to the same data internally
echo $obj1->asXML();
给定两个 SimpleXMLElement
结构如下(相同)的对象:
$obj1
object SimpleXMLElement {
["@attributes"] =>
array(0) {
}
["event"] =>
array(1) {
[0] =>
object(SimpleXMLElement) {
["@attributes"] =>
array(1) {
["url"] =>
string "example.com"
}
}
}
}
$obj2
object SimpleXMLElement {
["@attributes"] =>
array(0) {
}
["event"] =>
array(1) {
[0] =>
object(SimpleXMLElement) {
["@attributes"] =>
array(1) {
["url"] =>
string "another-example.com"
}
}
}
}
我正在尝试将所有子 event
项目从第二个对象复制到第一个对象:
foreach ($obj2->event as $event) {
$obj1->event[] = $event
}
它们移动了,但复制的对象现在是空的。
$obj1
object SimpleXMLElement {
["@attributes"] =>
array(0) {
}
["event"] =>
array(2) {
[0] =>
object(SimpleXMLElement) {
["@attributes"] =>
array(1) {
["url"] =>
string "example.com"
}
}
[1] =>
object(SimpleXMLElement) {
}
}
}
SimpleXML 的编辑功能相当有限,在这种情况下,您所做的赋值和 ->addChild
都只能设置新元素的文本内容,而不能设置其属性和子元素。
在这种情况下,您可能需要更复杂的功能 DOM API。幸运的是,您可以在 PHP 中混合使用两者,几乎没有任何损失,使用 dom_import_simplexml
and simplexml_import_dom
切换到另一个包装器。
获得 DOM 表示后,您可以在要编辑的 文档 上使用 the appendChild
method of DOMNode
to add a full node, but there's a catch - you can only add nodes "owned by" the same document. So you have to first call the importNode
method。
把它们放在一起,你会得到这样的东西:
// Set up the objects you're copying from and to
// These don't need to be complete documents, they could be any element
$obj1 = simplexml_load_string('<foo><event url="example.com" /></foo>');
$obj2 = simplexml_load_string('<foo><event url="another.example.com" /></foo>');
// Get a DOM representation of the root element we want to add things to
// Note that $obj1_dom will always be a DOMElement not a DOMDocument,
// because SimpleXML has no "document" object
$obj1_dom = dom_import_simplexml($obj1);
// Loop over the SimpleXML version of the source elements
foreach ($obj2->event as $event) {
// Get the DOM representation of the element we want to copy
$event_dom = dom_import_simplexml($event);
// Copy the element into the "owner document" of our target node
$event_dom_copy = $obj1_dom->ownerDocument->importNode($event_dom, true);
// Add the node as a new child
$obj1_dom->appendChild($event_dom_adopted);
}
// Check that we have the right output, not trusting var_dump or print_r
// Note that we don't need to convert back to SimpleXML
// - $obj1 and $obj1_dom refer to the same data internally
echo $obj1->asXML();