使用 SimpleXML 将节点从一个 XML 文档移动到另一个 XML 文档

发布于 2024-09-18 07:18:03 字数 681 浏览 6 评论 0原文

我意识到仅使用 SimpleXML 无法实现我的请求——我确实明白了这一点。这是我尝试过的:

$newXML = simplexml_load_file($filePath); 
$domNewXML = dom_import_simplexml($newXML);
$domItem = dom_import_simplexml($items[$itemQty]);  <-- item I want to move
$domNewItem = $domItem->cloneNode(true);
$newNode = $domNewXML->importNode($domNewItem, true);
$domNewXML->getElementsByTagName('list')->item(0)->appendChild($newNode);

我意识到第5行出现了代码错误,因为 importNode 是 dom 文档的函数,而不是 dom 元素,但是如何获取 dom 文档来执行此步骤?

我以正确的方式处理这件事吗?

在宏伟的计划中,我有一个至少包含 10 个节点的 XML 文件,每天都有一个 CRON 作业检查是否有超过 10 个节点,如果是,则应该将该节点从当前文件移动到存档文件中。我想我可以通过将节点复制到存档文件并将其从原始文件中删除来“移动”该节点。

感谢您的帮助!

I realize that my request is not possible using just SimpleXML -- that much I did figure out. Here is what I tried:

$newXML = simplexml_load_file($filePath); 
$domNewXML = dom_import_simplexml($newXML);
$domItem = dom_import_simplexml($items[$itemQty]);  <-- item I want to move
$domNewItem = $domItem->cloneNode(true);
$newNode = $domNewXML->importNode($domNewItem, true);
$domNewXML->getElementsByTagName('list')->item(0)->appendChild($newNode);

I realize the code errors out on line 5, because importNode is a function of the dom document, not the dom element, but how can I get the dom document to perform this step?

Am I going about this the right way?

In the grand scheme of things I have an XML file with at least 10 nodes, every day a CRON job checks to see if there are more than 10 nodes and if so, it's supposed to move the node from the current file into an archive file. I figured I would "move" the node by copying it to the archive file and deleting it from the original file.

Thanks for any help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

两相知 2024-09-25 07:18:03

您可以通过 $anyDOMNode->ownerDocument 获取所有者


文档无需克隆节点并将其插入到另一个文档中。如果将存档拆分为 a) 框架 xml 文档和 b) 作为外部实体包含到文档中的 xml 片段,只需将节点的 xml 字符串表示形式附加到片段文件的末尾就足够了。例如作为骨架

<?xml version="1.0"?>
<!DOCTYPE fooarchive [
  <!ENTITY entries SYSTEM "archive.fragment">
]>
<fooarchive>
  &entries;
</fooarchive>

,然后是 php 脚本

$doc = new SimpleXMLElement('<a>
  <b>0</b><b>1</b><b>2</b><b>3</b>
  <b>4</b><b>5</b><b>6</b><b>7</b>
  <b>8</b><b>9</b><b>X</b><b>Y</b>
</a>');

$move = '';
for($i=10; $i<count($doc->b); $i++) {
  $move .= $doc->b[$i]->asXML();
}
file_put_contents('archive.fragment', $move, FILE_APPEND);

for($i=count($doc->b)-1; $i>9; $i--) {
  unset($doc->b[$i]);
}
echo $doc->asXML('current.xml');

You can get the owner document via $anyDOMNode->ownerDocument


Maybe it's not necessary to clone and insert the nodes into another document. If you split the archive into a) a skeleton xml document and b) an xml fragment that is included as an external entity into the document it suffices if you just append the xml string representation of the node to the end of the fragment file. E.g. as the skeleton

<?xml version="1.0"?>
<!DOCTYPE fooarchive [
  <!ENTITY entries SYSTEM "archive.fragment">
]>
<fooarchive>
  &entries;
</fooarchive>

and then the php script

$doc = new SimpleXMLElement('<a>
  <b>0</b><b>1</b><b>2</b><b>3</b>
  <b>4</b><b>5</b><b>6</b><b>7</b>
  <b>8</b><b>9</b><b>X</b><b>Y</b>
</a>');

$move = '';
for($i=10; $i<count($doc->b); $i++) {
  $move .= $doc->b[$i]->asXML();
}
file_put_contents('archive.fragment', $move, FILE_APPEND);

for($i=count($doc->b)-1; $i>9; $i--) {
  unset($doc->b[$i]);
}
echo $doc->asXML('current.xml');
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文