Php SimpleXML追加多个文件

Php SimpleXML追加多个文件,php,xml,simplexml,Php,Xml,Simplexml,我正在尝试组合多个XML文件,如果可能的话,使用SimpleXML。我只是尝试将文件2中的产品、子项和子项数据附加到文件1中。我没有尝试合并元素,只是将文件2附加到文件1的底部,依此类推。(虽然我猜这在技术上是在合并merchandiser元素?)这些文件包含相同的模式,并且看起来都类似于下面的示例,唯一要更改的是实际的文本。这只是两种不同产品的XML,我在产品之间添加了一个很大的空间,以便更容易看到它的结尾 <merchandiser xmlns:xsi="http://www.w3.o

我正在尝试组合多个XML文件,如果可能的话,使用SimpleXML。我只是尝试将文件2中的产品、子项和子项数据附加到文件1中。我没有尝试合并元素,只是将文件2附加到文件1的底部,依此类推。(虽然我猜这在技术上是在合并merchandiser元素?)这些文件包含相同的模式,并且看起来都类似于下面的示例,唯一要更改的是实际的文本。这只是两种不同产品的XML,我在产品之间添加了一个很大的空间,以便更容易看到它的结尾

<merchandiser xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="merchandiser.xsd">
<header>
<merchantId>35928</merchantId>
<merchantName>Sunspel Clothing</merchantName>
<createdOn>01/14/2016 02:03:31</createdOn>
</header>
<product product_id="14633" name="Cotton Socks" sku_number="1588/102">
<category>
<primary>Accessories</primary>
<secondary>Men's~~Socks</secondary>
</category>
<URL>
<product>
http://click.linksynergy.com/link?id=D*rqD2paIXY&offerid=191965.14633&type=15&murl=http%3A%2F%2Fwww.sunspel.com%2Fuk%2Fcotton-sock-black.html
</product>
<productImage>
http://www.sunspel.com/media/catalog/product/cache/1/image/9df78eab33525d08d6e5fb8d27136e95/1/5/1588-102-new.jpg
</productImage>
</URL>
<description>
<short>
Our new cotton socks are designed by Sunspel and crafted in an Italian factory steeped in years of experience, skill and heritage. They are made from the highest quality, extra-long staple Egyptian cotton yarn which, prior to knitting is combed, twisted and mercerised to enhance the comfort, shine and absorption of the fabric as well as its resistance to pilling and shrinking.
</short>
</description>
<discount currency="GBP">
<type>amount</type>
</discount>
<price currency="GBP">
<retail>15.00</retail>
</price>
<shipping>
<availability>in-stock</availability>
</shipping>
<pixel>
http://ad.linksynergy.com/fs-bin/show?id=D*rqD2paIXY&bids=191965.14633&type=15&subid=0
</pixel>
</product>
<product product_id="15115" name="Cotton Socks" sku_number="1589/236">
<category>
<primary>Accessories</primary>
<secondary>Men's~~Socks~~Men's</secondary>
</category>
<URL>
<product>
http://click.linksynergy.com/link?id=D*rqD2paIXY&offerid=191965.15115&type=15&murl=http%3A%2F%2Fwww.sunspel.com%2Fuk%2Fmens-cotton-socks-navy-stripes.html
</product>
<productImage>
http://www.sunspel.com/media/catalog/product/cache/1/image/9df78eab33525d08d6e5fb8d27136e95/1/5/1588-236-new.jpg
</productImage>
</URL>
<description>
<short>
Our new cotton socks are designed by Sunspel and crafted in an Italian factory steeped in years of experience, skill and heritage. They are made from the highest quality, extra-long staple Egyptian cotton yarn which, prior to knitting is combed, twisted and mercerised to enhance the comfort, shine and absorption of the fabric as well as its resistance to pilling and shrinking.
</short>
</description>
<discount currency="GBP">
<type>amount</type>
</discount>
<price currency="GBP">
<retail>17.00</retail>
</price>
<shipping>
<availability>in-stock</availability>
</shipping>
<pixel>
http://ad.linksynergy.com/fs-bin/show?id=D*rqD2paIXY&bids=191965.15115&type=15&subid=0
</pixel>
</product>



<product product_id="15116" name="Cotton Socks" sku_number="1589/711">
<category>
<primary>Accessories</primary>
<secondary>Men's~~Socks~~Men's</secondary>
</category>
<URL>
<product>
http://click.linksynergy.com/link?id=D*rqD2paIXY&offerid=191965.15116&type=15&murl=http%3A%2F%2Fwww.sunspel.com%2Fuk%2Fmens-cotton-socks-charcoal-melange-stripes.html
</product>
<productImage>
http://www.sunspel.com/media/catalog/product/cache/1/image/9df78eab33525d08d6e5fb8d27136e95/1/5/1588-711-new.jpg
</productImage>
</URL>
<description>
<short>
Our new cotton socks are designed by Sunspel and crafted in an Italian factory steeped in years of experience, skill and heritage. They are made from the highest quality, extra-long staple Egyptian cotton yarn which, prior to knitting is combed, twisted and mercerised to enhance the comfort, shine and absorption of the fabric as well as its resistance to pilling and shrinking.
</short>
</description>
<discount currency="GBP">
<type>amount</type>
</discount>
<price currency="GBP">
<retail>17.00</retail>
</price>
<shipping>
<availability>in-stock</availability>
</shipping>
<pixel>
http://ad.linksynergy.com/fs-bin/show?id=D*rqD2paIXY&bids=191965.15116&type=15&subid=0
</pixel>
</product>
当我尝试从那里添加时,一切都变得一团糟,没有任何东西在正确的位置/顺序了。我还想把它放到一个函数中,因为我会经常这样做。非常感谢您的任何帮助,因为我已经为此奋斗了几天,并且已经研究了几十个stackoverflow和php.net线程


让我困惑的是每个文件开头的
标记。一旦merchandiser标记结束,它就是文档的结束,因此我只需要获取文件2的merchandiser标记内的内容,并将其附加到文件1的merchandiser标记内。标题标签让我很困惑,因为我不确定它是否会妨碍我

作为初步说明,您的XML示例格式不正确。此外,它与您的代码不一致(即没有
->merchandiser->header->product

因此,在本例中,我将使用一个不同的示例,如以下示例(file1.xml):

然后,使用
SimpleXML
加载第二个文件:

$src = simplexml_load_file( 'file2.xml' );
通过
foreach()
循环,将每个
元素从
SimpleXML
导入
DOMDocument
并将其显示为
$parent
节点的子节点:

foreach( $src->product as $product )
{
    $node = dom_import_simplexml( $product );
    $node = $dst->importNode( $node, 1 );
    $parent->appendChild( $node );
}
现在,合并的XML已经准备好了。您可以使用
$dst->saveXML()
打印它

我无法生成正确缩进的XML。顺便说一句,要执行此操作,您可以重新加载它:

$final = new DOMDocument();
$final->loadXML( $dst->saveXML(), LIBXML_NOBLANKS );
$final->formatOutput = True;
echo $final->saveXML();
最终输出:

<?xml version="1.0"?>
<root>
  <product>
    <name>Product 1</name>
  </product>
  <product>
    <name>Product 2</name>
  </product>
  <product>
    <name>Product 3</name>
  </product>
  <product>
    <name>Product 4</name>
  </product>
</root>

产品1
产品2
产品3
产品4

DOM可以使用
importNode()
我有一个脚本,我以前尝试调整它,它使用了
importNode()
,但它一直抛出很多错误。我对编码非常陌生,所以小错误需要几天才能正确修复,所以我更愿意修改上面的代码,而不是从头开始。谢谢,我现在就试试这个。我不确定是否包含merchandiser,现在我添加了一个指向XML文件的链接,以便您可以更好地查看结构。我不知道为什么第一次没有正确显示。我要用“merchandiser”替换“root”吗?谢谢你的帮助!我能让它正常工作。从概念上讲,对我来说,什么是最好的循环方式,这样我就可以将十几个XML文件组合成一个?从概念上讲,我通过全局
foreach
:您修改原始文件,因此没有理由保留原始目标文件。谢谢!这将是我的下一次旅程。
$src = simplexml_load_file( 'file2.xml' );
foreach( $src->product as $product )
{
    $node = dom_import_simplexml( $product );
    $node = $dst->importNode( $node, 1 );
    $parent->appendChild( $node );
}
$final = new DOMDocument();
$final->loadXML( $dst->saveXML(), LIBXML_NOBLANKS );
$final->formatOutput = True;
echo $final->saveXML();
<?xml version="1.0"?>
<root>
  <product>
    <name>Product 1</name>
  </product>
  <product>
    <name>Product 2</name>
  </product>
  <product>
    <name>Product 3</name>
  </product>
  <product>
    <name>Product 4</name>
  </product>
</root>