SimpleXML附加多个文件

时间:2016-03-11 20:35:09

标签: php xml simplexml

我正在尝试组合多个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 );

当我尝试从那里添加时,一切都搞砸了,没有任何东西在正确的位置/顺序。我也想把它放到一个函数中,因为我将经常这样做。任何帮助都非常感激,因为我已经在这几天苦苦挣扎,并且已经浏览了几十个stackoverflow和php.net线程。

令我困惑的一件事是每个文件都以<merchandiser><header>标记开头。一旦商品标签结束,它就是文件的末尾,所以我只需要拿出文件2的商品标签内的内容并将其附加到文件1的商品标签内。标头标签只会让我感到困惑,因为我不确定是否它会妨碍你。

1 个答案:

答案 0 :(得分:0)

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

所以,在这个例子中,我将使用不同的样本,例如这个样本( file1.xml ):

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

和这个( file2.xml ):

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

您不想使用DOMDocument->importNode()因为“它会导致很多错误”。

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

首先,准备目标XML:使用SimpleXML加载文件,使用DOMDocument创建dom_import_simplexml()并将$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>
相关问题