简单 XML 附加多个文件


SimpleXML append multiple files

我正在尝试合并多个XML文件,如果可能的话,使用SimpleXML。我只是尝试将产品、子项和子项数据从文件 2 追加到文件 1 中。我不是在尝试合并元素,只是将文件 2 附加到文件 1 的底部,依此类推。(虽然我想这在技术上是合并跟单员元素?这些文件包含相同的架构,并且看起来都类似于下面的示例,唯一要更改的是实际文本。这只是两个不同产品的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>

通过一些 foreach 语句,我可以附加所有产品子项和属性,但这实际上并没有为我提供子项数据。

    $file1 = '35928_3210820_mp.xml';
$file2 = '39153_3210820_mp.xml';
$fileout = 'ukmerge.xml';
$xml1 = simplexml_load_file( $file1 );
$xml2 = simplexml_load_file( $file2 );  // loop through the product and add them and their attributes to xml1
$product = $xml2->product;
$prod = $xml2->merchandiser->header->product;
$category = $product->category;
$url = $product->URL;
$description = $product->description;
foreach( $xml2->children() as $child ) {
    $new = $xml1->addChild( $child->getName() , htmlspecialchars($child) );
    foreach( $child->attributes() as $key => $value ) {
        $new->addAttribute( $key, $value );
        }
    }   $fh = fopen( $fileout, 'w') or die ( "can't open file $fileout" );
fwrite( $fh, $xml1->asXML() );
fclose( $fh );

当我尝试从那里添加时,一切都搞砸了,一切都不再在正确的位置/顺序中。我还想把它放到一个函数中,因为我会经常这样做。非常感谢任何帮助,因为我已经为此苦苦挣扎了几天,并且已经搜索了几十个堆栈溢出和 php.net 线程。

让我感到困惑的一件事是每个文件开头的<merchandiser><header>标签。一旦跟单员标签结束,它就是文档的结尾,所以我只需要获取文件 2 的跟单员标签内的内容并将其附加到文件 1 的跟单员标签中。标题标签只是让我感到困惑,因为我不确定它是否妨碍了我。

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

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

<root>
    <product>
        <name>Product 1</name>
    </product>
    <product>
        <name>Product 2</name>
    </product>
</root>

而这个(文件2.xml):

<root>
    <product>
        <name>Product 3</name>
    </product>
    <product>
        <name>Product 4</name>
    </product>
</root>

您不想使用DOMDocument->importNode(),因为"它不断抛出很多错误"。

您可以将DOMDocumentSimpleXMLdom_import_simplexml()功能结合使用。

首先,准备目标 XML:使用 SimpleXML 加载文件,使用 dom_import_simplexml() 创建一个DOMDocument $parent并将变量设置为<root>元素:

$dst = simplexml_load_file( 'file1.xml' );
$dst = dom_import_simplexml( $dst )->ownerDocument;
$parent = $dst->getElementsByTagName( 'root' )->item(0);

然后,加载第二个文件SimpleXML

$src = simplexml_load_file( 'file2.xml' );

通过foreach()循环,将每个<product>元素从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>