簡體   English   中英

SimpleXML附加多個文件

[英]SimpleXML append multiple files

我正在嘗試使用SimpleXML合並多個XML文件(如果可能)。 我只是想將文件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>標記。 merchandiser標記結束后,即為文檔的結尾,因此我只需要提取文件2的merchandiser標記內的內容並將其附加到文件1的merchandiser標記內。標頭標記只是使我感到困惑,因為我不確定是否它會妨礙您前進。

作為初步說明,您的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()因為“它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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM