День добрый. Возник вопрос , как мне сохранить несколько XML файлов. Из бд идет выборка и мне для каждого ID нужен отдельный XML. Что-то у меня не выходит. Может в DOM есть какой метод очистки переменных, он мне добавляет в первый файл нормально ,во 2 добавляет и 1 и 2 и т.д. Очень нужна подсказка. Спасибо. PHP: header ("Content-Type: text/html; charset=UTF-8"); $dbhost = "localhost"; $dbuser = "Admin"; $dbpass = "*********"; $dbname = "admin"; $link = mysql_connect($dbhost, $dbuser, $dbpass) or die('No'); mysql_select_db($dbname); mysql_query("SET NAMES utf8"); $res = mysql_query(' SELECT * FROM `m_el_s_faktura`,`m_docum_s_faktura` WHERE m_el_s_faktura.number = m_docum_s_faktura.number LIMIT 5'); $xml = new DOMDocument("1.0", "UTF-8"); $xml->FormatOutput = true; $string_value = $xml->saveXML(); //root $issuanse = $xml->createElement("issuanse"); $issuanse->setAttribute("xmlns","http://www.w3schools.com"); $issuanse->setAttribute("sender",12); $xml->appendChild($issuanse); while ($row = mysql_fetch_assoc($res)){ //first block $general = $xml->createElement("general"); $issuanse->appendChild($general); $number = $xml->createElement("number" , $row['number']); $general->appendChild($number); $dateIssuance = $xml->createElement("dateIssuance" , $row['d_issuance']); $general->appendChild($dateIssuance); $dateTransaction = $xml->createElement("dateTransaction" , $row['d_transact']); $general->appendChild($dateTransaction); $documentType = $xml->createElement("documentType" , $row['doc_type']); $general->appendChild($documentType); //next block $provider = $xml->createElement("provider"); $issuanse->appendChild($provider); $providerStatus = $xml->createElement("providerStatus" , $row['post_stat']); $provider->appendChild($providerStatus); $dependentPerson = $xml->createElement("dependentPerson" , $row['post_dep_p']); $provider->appendChild($dependentPerson); $residentsOfOffshore = $xml->createElement("residentsOfOffshore" , $row['post_resid']); $provider->appendChild($residentsOfOffshore); $specialDealGoods = $xml->createElement("specialDealGoods" , $row['post_spec']); $provider->appendChild($specialDealGoods); $bigCompany = $xml->createElement("bigCompany" , $row['post_big_c']); $provider->appendChild($bigCompany); $countryCode = $xml->createElement("countryCode" , $row['post_str']); $provider->appendChild($countryCode); $unp = $xml->createElement("unp" , $row['post_unp']); $provider->appendChild($unp); $name = $xml->createElement("name" , $row['post_name']); $provider->appendChild($name); $address = $xml->createElement("address" , $row['post_adr']); $provider->appendChild($address); //next block $recipient = $xml->createElement("recipient"); $issuanse->appendChild($recipient); $recipientStatus = $xml->createElement("recipientStatus" , $row['pol_stat']); $recipient->appendChild($recipientStatus); $dependentPerson = $xml->createElement("dependentPerson" , $row['pol_dep_p']); $recipient->appendChild($dependentPerson); $residentsOfOffshore = $xml->createElement("residentsOfOffshore" , $row['pol_resid']); $recipient->appendChild($residentsOfOffshore); $specialDealGoods = $xml->createElement("specialDealGoods" , $row['pol_spec']); $recipient->appendChild($specialDealGoods); $bigCompany = $xml->createElement("bigCompany" , $row['pol_big_c']); $recipient->appendChild($bigCompany); $countryCode = $xml->createElement("countryCode" , $row['pol_str']); $recipient->appendChild($countryCode); $unp = $xml->createElement("unp" , $row['pol_unp']); $recipient->appendChild($unp); $name = $xml->createElement("name" , $row['pol_name']); $recipient->appendChild($name); $address = $xml->createElement("address" , $row['pol_adr']); $recipient->appendChild($address); //next block $senderReceiver = $xml->createElement("senderReceiver"); $issuanse->appendChild($senderReceiver); $consignors = $xml->createElement("consignors"); $senderReceiver->appendChild($consignors); $consignor = $xml->createElement("consignor"); $consignors->appendChild($consignor); $countryCode = $xml->createElement("countryCode" , $row['gotpr_str']); $consignor->appendChild($countryCode); $unp = $xml->createElement("unp" , $row['gotpr_unp']); $consignor->appendChild($unp); $name = $xml->createElement("name" , $row['gotpr_name']); $consignor->appendChild($name); $address = $xml->createElement("address" , $row['gotpr_adr']); $consignor->appendChild($address); $consignees = $xml->createElement("consignees"); $senderReceiver->appendChild($consignees); $consignee = $xml->createElement("consignee"); $consignees->appendChild($consignee); $countryCode = $xml->createElement("countryCode" , $row['grpol_str']); $consignee->appendChild($countryCode); $unp = $xml->createElement("unp" , $row['grpol_unp']); $consignee->appendChild($unp); $name = $xml->createElement("name" , $row['grpol_name']); $consignee->appendChild($name); $address = $xml->createElement("address" , $row['grpol_adr']); $consignee->appendChild($address); $deliveryCondition = $xml->createElement("deliveryCondition"); $issuanse->appendChild($deliveryCondition); $contract = $xml->createElement("contract"); $deliveryCondition->appendChild($contract); $number = $xml->createElement("number" , $row['kontrakt']); $contract->appendChild($number); $date = $xml->createElement("date" , $row['kon_beg']); $contract->appendChild($date); $documents = $xml->createElement("documents"); $contract->appendChild($documents); $document = $xml->createElement("document"); $documents->appendChild($document); $docType = $xml->createElement("docType"); $document->appendChild($docType); $code = $xml->createElement("code" , $row['number']); $docType->appendChild($code); $value = $xml->createElement("value" , $row['name_doc']); $docType->appendChild($value); $date = $xml->createElement("date" , $row['data_doc']); $document->appendChild($date); $blankCode = $xml->createElement("blankCode" , $row['blank_cod']); $document->appendChild($blankCode); $seria = $xml->createElement("seria" , $row['seria']); $document->appendChild($seria); $number = $xml->createElement("number" , $row['numdoc']); $document->appendChild($number); $xml->save($row['number'].'.xml'); }
Все работает. Дело в том что при сохранении в файл переменный накапливают результаты предыдущих итераций. И запись в файле растет по прогрессии) Нормально сохраняет только в первый XML файл. А остальные содержат записи предыдущих + новое. --- Добавлено --- Вот например в первом файле после итерации сохраняется содержание--->{1} , после второй итерации сохраняется содержание--->{1,2} а должно только 2. --- Добавлено --- Знает кто нибудь?
Может в цикле после save() очистить $xml и опять инициализировать? PHP: $xml = new DOMDocument("1.0", "UTF-8");