PHP 将 xml 节点从一个文档复制到另一个文档

发布于 2025-01-06 22:51:25 字数 2366 浏览 0 评论 0原文

首先,我需要通过 xml 文档中的特定子节点值查找父节点;然后将一些特定的子节点从父节点复制到另一个 xml 文档。

例如:

DESTINATION FILE: ('destination.xml') 
<item>
    <offerStartDate>2012-15-02</offerStartDate>
    <offerEndDate>2012-19-02</offerEndDate>
    <title>Item Title</title> 
    <rrp>14.99</rrp>
    <offerPrice>9.99</offerPrice>
</item> 

SOURCE FILE: ('source.xml') 
<items> 
    <item> 
         <title>Item A</title> 
         <description>This is the description for Item A</description> 
         <id>1003</id>
         <price>
             <rrp>10.00</rrp>
             <offerPrice>4.99</offerPrice>
         </price>
         <offer>
             <deal>
                 <isLive>0</isLive>
             </deal>
         </offer>
    </item>
    <item> 
         <title>Item B</title> 
         <description>This is the description for Item B</description> 
         <id>1003</id>
         <price>
             <rrp>14.99</rrp>
             <offerPrice>9.99</offerPrice>
         </price>
         <offer>
             <deal>
                 <isLive>1</isLive>
             </deal>
         </offer>
    </item> 
    <item> 
         <title>Item C</title> 
         <description>This is the description for Item C</description> 
         <id>1003</id>
         <price>
             <rrp>9.99</rrp>
             <offerPrice>5.99</offerPrice>
         </price>
         <offer>
             <deal>
                 <isLive>0</isLive>
             </deal>
         </offer>
    </item> 

想找到其子节点 值设置为“1”的父节点 。然后将父节点的其他子节点复制到目标xml中。

例如,如果父节点 的子节点 设置为 1。复制 </code>, <code> <rrp></code> 和 <code><offerPrice></code> 节点及其值,并将它们作为子节点添加到目标文件,如上所示。

如果我没有正确使用它们,请原谅我的技术术语。

非常感谢大家的帮助!

First of all, I need to Find a parent node by a specific child node value in an xml doc; Then copy some specific children nodes from the parent node to another xml doc.

For instance:

DESTINATION FILE: ('destination.xml') 
<item>
    <offerStartDate>2012-15-02</offerStartDate>
    <offerEndDate>2012-19-02</offerEndDate>
    <title>Item Title</title> 
    <rrp>14.99</rrp>
    <offerPrice>9.99</offerPrice>
</item> 

and

SOURCE FILE: ('source.xml') 
<items> 
    <item> 
         <title>Item A</title> 
         <description>This is the description for Item A</description> 
         <id>1003</id>
         <price>
             <rrp>10.00</rrp>
             <offerPrice>4.99</offerPrice>
         </price>
         <offer>
             <deal>
                 <isLive>0</isLive>
             </deal>
         </offer>
    </item>
    <item> 
         <title>Item B</title> 
         <description>This is the description for Item B</description> 
         <id>1003</id>
         <price>
             <rrp>14.99</rrp>
             <offerPrice>9.99</offerPrice>
         </price>
         <offer>
             <deal>
                 <isLive>1</isLive>
             </deal>
         </offer>
    </item> 
    <item> 
         <title>Item C</title> 
         <description>This is the description for Item C</description> 
         <id>1003</id>
         <price>
             <rrp>9.99</rrp>
             <offerPrice>5.99</offerPrice>
         </price>
         <offer>
             <deal>
                 <isLive>0</isLive>
             </deal>
         </offer>
    </item> 

I want to find the parent node <item> that has it's child node <isLive> value set to "1". Then copy other children nodes of the parent node to the destination xml.

e.g. If parent node <item> has its child node <isLive> set to 1. Copy <title>, <rrp> and <offerPrice> nodes and their values and add them to the destination file as children nodes as shown above.

Pardon my technical lingo if I have not used them correctly.

Many thanks for the help guys!

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

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

发布评论

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

评论(1

守护在此方 2025-01-13 22:51:25

使用 SimpleXml (演示):

$dItems = simplexml_load_file('destination.xml');
$sItems = simplexml_load_file('source.xml');
foreach ($sItems->xpath('/items/item[offer/deal/isLive=1]') as $item) {
    $newItem = $dItems->addChild('item');
    $newItem->addChild('title', $item->title);
    $newItem->addChild('rrp', $item->price->rrp);
    $newItem->addChild('offerprice', $item->price->offerPrice);
}
echo $dItems->saveXML();

使用 DOM (演示):

$destination = new DOMDocument;
$destination->preserveWhiteSpace = false;
$destination->load('destination.xml');
$source = new DOMDocument;
$source->load('source.xml');
$xp = new DOMXPath($source);
foreach ($xp->query('/items/item[offer/deal/isLive=1]') as $item)
{
    $newItem = $destination->documentElement->appendChild(
        $destination->createElement('item')
    );
    foreach (array('title', 'rrp', 'offerPrice') as $elementName) {
        $newItem->appendChild(
            $destination->importNode(
                $item->getElementsByTagName($elementName)->item(0),
                true
            )
        );
    }
}
$destination->formatOutput = true;
echo $destination->saveXml();

With SimpleXml (demo):

$dItems = simplexml_load_file('destination.xml');
$sItems = simplexml_load_file('source.xml');
foreach ($sItems->xpath('/items/item[offer/deal/isLive=1]') as $item) {
    $newItem = $dItems->addChild('item');
    $newItem->addChild('title', $item->title);
    $newItem->addChild('rrp', $item->price->rrp);
    $newItem->addChild('offerprice', $item->price->offerPrice);
}
echo $dItems->saveXML();

With DOM (demo):

$destination = new DOMDocument;
$destination->preserveWhiteSpace = false;
$destination->load('destination.xml');
$source = new DOMDocument;
$source->load('source.xml');
$xp = new DOMXPath($source);
foreach ($xp->query('/items/item[offer/deal/isLive=1]') as $item)
{
    $newItem = $destination->documentElement->appendChild(
        $destination->createElement('item')
    );
    foreach (array('title', 'rrp', 'offerPrice') as $elementName) {
        $newItem->appendChild(
            $destination->importNode(
                $item->getElementsByTagName($elementName)->item(0),
                true
            )
        );
    }
}
$destination->formatOutput = true;
echo $destination->saveXml();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文