2011-11-30 8 views
1

DOMDocumentを使用してデータをループしてXMLファイルを作成するときに問題があります。このスクリプトをバッチで実行するまでは、すべてうまくいきました。 XMLファイルに複数の開始タグがあり、各バッチに1つのように見えます。'<?xml version="1.0"?>'製品よりも多くの製品ノードが生成されています。誰も助けることができます。PHP DOMDocumentをバッチで使用してXMLファイルを構築する

//get products   
$productsObj = new Products($db,$shopKeeperID); 
       //find out how many products   
$countProds = $productsObj->countProducts();   

$productBatchLimit = 3; //keep low for testing 

//create new file  
$file = 'products/'. $products . '.xml';      
$fh = fopen($file, 'a');  

//create XML document object model (DOM)   
$doc = new DOMDocument(); 


$doc->formatOutput = true;  
$counter = 1;  
$r = $doc->createElement("products");   
$doc->appendChild($r); 

for ($i = 0; $i < $countProds; $i += $productBatchLimit) {    

$limit = $productBatchLimit*$counter; 

$products = $productsObj->getShopKeeperProducts($i, $limit);  

$prod = ''; 

//loop through each product to create well formed XML   
foreach($products as $product){   

$prod = $doc->createElement("offer"); 

$offerID = $doc->createElement("offerID"); 
$offerID->appendChild($doc->createTextNode($product['prod_id'])); 
$prod->appendChild($offerID);  
$productName = $doc->createElement("name"); 
$productName->appendChild($doc->createTextNode($product['productName'])); 
$prod->appendChild($productName);  

$r->appendChild($prod); 
$strxml = $doc->saveXML(); 
}   
fwrite($fh, $strxml);  
$counter++;   
}  
fclose($fh); 

答えて

1

私はちょっと前にこれを行いました。しかし、私はあなたが見るためのサイトのために構築した機能を提供することができます。
この関数は、正常に100%正常に動作しました。完全なXML文書を作成し、完全にフォーマットしました。これがあなたの問題を見つけるのに役立つことを願っています。

function create_xml_file() 
{ 
/* create a dom document with encoding utf8 */ 
$domtree = new DOMDocument('1.0', 'utf-8'); 

/* create the root element of the xml tree */ 
/* Data Node */ 
$xmlRoot = $domtree->createElement("data"); 

/* append it to the document created */ 
$xmlRoot = $domtree->appendChild($xmlRoot); 

/* Set our Prices in our <data> <config> node */ 
$config_node = $domtree->createElement("config"); 
$config_node = $xmlRoot->appendChild($config_node); 

// Add - node to config 
$config_node->appendChild($domtree->createElement('config_node', '123456')); 
$config_node->appendChild($domtree->createElement('some_other_data', '123456')); 

/* Create prices Node */ 
$price_node = $domtree->createElement('price'); 
$price_node = $config_node->appendChild($price_node); 

/* Black Price Node */ 
$black_node = $price_node->appendChild($domtree->createElement('black')); 
foreach ($p->List_all() as $item): 
    if ($item['color'] == 'black'): 
     $black_node->appendChild($domtree->createElement($item['type'], $item['price'])); 
    endif; 
endforeach; 

/* Create galleries Node */ 
$galleries_node = $domtree->createElement("galleries"); 
$galleries_node = $xmlRoot->appendChild($galleries_node); 
foreach ($i->List_all() as $image): 
    /* Our Individual Gallery Node */ 
    $gallery_node = $domtree->createElement("gallery"); 
    $gallery_node = $galleries_node->appendChild($gallery_node); 

    $gallery_node->appendChild($domtree->createElement('name', $image['name'])); 
    $gallery_node->appendChild($domtree->createElement('filepath', $image['filepath'])); 
    $gallery_node->appendChild($domtree->createElement('thumb', $image['thumb'])); 
endforeach; 

/* Format it so it is human readable */ 
$domtree->preserveWhiteSpace = false; 
$domtree->formatOutput = true; 

/* get the xml printed */ 
//echo $domtree->saveXML(); 
$file = 'xml/config.xml'; 
$domtree->save($file); 
} 

これは、あなたの答えを見つけるのに役立ちます。わかりやすくコメントしました。

+0

私は大量のデータを持っているため、ノードのバクテスを追加しようとしていて、XMLにファイルを追加するのではなく、ファイルを1つに保存するだけです。もしそうなら、私はどのように組み込むのですか?多くのありがとう – LeeTee

+0

私は追加ルートを試して、それを行うことができますが、私はまた何らかのエラーを抱えていました(私は覚えていません)。申し訳ありません – Anil

+0

私はあなたがこれを必要と思う:http://php.net/manual/en/domdocument.importnode.php – Anil

1

私は<?something?>のようなすべての文字列を消去するこのコマンドを使用しています。

  $text = preg_replace('/<\?[^\?>]*\?>/', ' ', $text); 

最初にすべてを消去し、最初に1つを入れます。

0

私は単に

$doc->save($file); 

$strxml = $doc->saveXML(); 

fwrite($fh, $strxml); 

を変更し、それが完璧に動作します。

関連する問題