Gerar XML com PHP
Boa Tarde,
Estou tentando gerar um XML com php, já consegui progresso, consegui gerar o XML certinho até o momento, acontece que nesse XML tenho várias tags <feature></feature> e o que e eu preciso fazer é se existir valor ele escreve essa tag, se não houver valor ele não escreve a tag no XML. abaixo os códigos. Eu estou buscando uma por uma e escrevendo uma por uma, mas a exigência da vivareal é que não havendo feature, não mostre a tag vazia.
obrigado
<?php
require_once("conexao.php");
$pdo = conectar();
$busca_menu=$pdo->prepare(" SELECT * FROM item");
$busca_menu->execute();
$xml =new DOMDocument("1.0" , "utf-8");
$xml->formatOutput=true;
$listdatafeed=$xml->createElement("ListingDataFeed");
$lis=$xml->createAttribute("xmlns");
$lis->value='http://www.vivareal.com/schemas/1.0/VRSync';
$liss=$xml->createAttribute("xmlns:xsi");
$listdatafeed->appendChild($lis);
$liss->value= 'http://www.w3.org/2001/XMLSchema-instance';
$listdatafeed->appendChild($liss);
$lisss=$xml->createAttribute("xsi:schemaLocation");
$lisss->value= 'http://www.vivareal.com/schemas/1.0/VRSync http://xml.vivareal.com/vrsync.xsd';
$listdatafeed->appendChild($lisss);
$head=$xml->createElement("Header");
$provider=$xml->createElement("Provider","Gera XML");
$head->appendChild($provider);
$mail=$xml->createElement("Email","contato@contato.com.br");
$head->appendChild($mail);
$contato=$xml->createElement("ContactName","Teste TESTE");
$head->appendChild($contato);
$publi=$xml->createElement("PublishDate","2009-08-10T11:17:14");
$head->appendChild($publi);
$Logo=$xml->createElement("Logo","www.fornecedor.com.br/Logo.jpg");
$head->appendChild($Logo);
$fone=$xml->createElement("Telephone","(47)00000000");
$head->appendChild($fone);
$listdatafeed->appendChild($head);
$listings=$xml->createElement("Listings");
while($linhas = @$busca_menu->fetch(PDO::FETCH_ASSOC)){
if ($linhas['item_finalidade'] == 1){
$transa = "For Sale";
}elseif($linhas['item_finalidade'] == 2){
$transa = "For Rent";
}elseif($linhas['item_finalidade'] == 3){
$transa = "Sale/Rent";
}
if($linhas['item_tipo'] == 1){
$tipo = "Commercial / Industrial ";
}else if($linhas['item_tipo'] == 2){
$tipo = "Residential / Land Lot";
}else if($linhas['item_tipo'] == 3){
$tipo = "Residential / Sobrado";
}else if($linhas['item_tipo'] == 4){
$tipo = "Commercial / Office";
}else if($linhas['item_tipo'] == 5){
$tipo = "Residential / Farm Ranch";
}else if($linhas['item_tipo'] == 6){
$tipo = "Commercial / Agricultural";
}else if($linhas['item_tipo'] == 7){
$tipo = "Commercial / Agricultural";
}else if($linhas['item_tipo'] == 8){
$tipo = "Residential / Apartment";
}else if($linhas['item_tipo'] == 9){
$tipo = " Residential / Home";
}else if($linhas['item_tipo'] == 10){
$tipo = "Commercial / Business";
}else if($linhas['item_tipo'] == 9){
$tipo = "Residential / Land Lot ";
}else if($linhas['item_tipo'] == 11){
$tipo = "Residential / Land Lot ";
}else if($linhas['item_tipo'] == 13){
$tipo = "Residential / Land Lot";
}else if($linhas['item_tipo'] == 15){
$tipo = "Commercial / Building";
}else if($linhas['item_tipo'] == 16){
$tipo = "Residential / Condo";
}else if($linhas['item_tipo'] == 17){
$tipo = "Residential / Flat";
}else if($linhas['item_tipo'] == 18){
$tipo = "Commercial / Loja";
}else if($linhas['item_tipo'] == 19){
$tipo = "Commercial / Industrial";
}else if($linhas['item_tipo'] == 20){
$tipo = "Commercial / Residential Income";
}else if($linhas['item_tipo'] == 21){
$tipo = "Commercial / Residential Income";
}
$id = $linhas["item_id"];
$listing=$xml->createElement("Listing");
$listings->appendChild($listing);
$item_id=$xml->createElement("ListingID", $linhas['item_id']);
$title=$xml->createElement("Title");
$til=$xml->createCDATASection($linhas['item_busca']);
$title->appendChild($til);
$listing->appendChild($title);
$listing->appendChild($item_id);
$transaction=$xml->createElement("TransactionType", $transa);
$listing->appendChild($transaction);
$Featured=$xml->createElement("Featured", true);
$listing->appendChild($Featured);
$data=$xml->createElement("ListDate");
$listing->appendChild($data);
$up=$xml->createElement("ListDate");
$listing->appendChild($up);
$url=$xml->createElement("DetailViewUrl");
$listing->appendChild($url);
$busca_foto=$pdo->prepare(" SELECT * FROM foto WHERE foto_item = '$id' ");
$busca_foto->execute();
$media=$xml->createElement("Media");
$itemv=$xml->createElement("Item" , "teste");
$medi=$xml->createAttribute("medium");
$medi->value = 'video';
$itemv->appendChild($medi);
$media->appendChild($itemv);
while($linha = @$busca_foto->fetch(PDO::FETCH_ASSOC)){
$itemf=$xml->createElement("Item", 'http://www.imoveis.com.br/thumb/'. $linha['foto_url']);
$med=$xml->createAttribute('medium');
$med->value = 'image';
$itemf->appendChild($med);
$media->appendChild($itemf);
}
$listing->appendChild($media);
// inicio detalhes
$detalhe=$xml->createElement("Details");
$porperty=$xml->createElement("PropertyType", $tipo);
$detalhe->appendChild($porperty);
// descrição
$limpo = strip_tags($linhas['item_desc']);
$description=$xml->createElement("Description");
$des=$xml->createCDATASection($limpo);
$description->appendChild($des);
$detalhe->appendChild($description);
//valor de venda
$preco=$xml->createElement("ListPrice", $linhas['item_preco']);
$price=$xml->createAttribute('currency');
$price->value = 'BRL';
$preco->appendChild($price);
$detalhe->appendChild($preco);
// valor de Aluguel
$aluguel=$xml->createElement("RentalPrice", $linhas['item_preco_locacao']);
$price=$xml->createAttribute('currency');
$price->value = 'BRL';
$aluguel->appendChild($price);
$detalhe->appendChild($aluguel);
//condominio
$condominio=$xml->createElement("PropertyAdministrationFee", $linhas['item_preco_condominio']);
$price=$xml->createAttribute('currency');
$price->value = 'BRL';
$condominio->appendChild($price);
$detalhe->appendChild($condominio);
// valor iptu
$iptu=$xml->createElement("YearlyTax", $linhas['item_preco_iptu']);
$price=$xml->createAttribute('currency');
$price->value = 'BRL';
$iptu->appendChild($price);
$detalhe->appendChild($iptu);
// area
$area=$xml->createElement("LivingArea", $linhas['item_area']);
$metros=$xml->createAttribute('unit');
$metros->value = 'squere meters';
$area->appendChild($metros);
$detalhe->appendChild($area);
// dormitórios
$dorm=$xml->createElement("Bedrooms", $linhas['item_dorm']);
$detalhe->appendChild($dorm);
//banheiros
$banheiro=$xml->createElement("Bathrooms", $linhas['item_wc']);
$detalhe->appendChild($banheiro);
// suites
$suite=$xml->createElement("Suites", $linhas['item_suite']);
$detalhe->appendChild($suite);
//vagas
$vaga=$xml->createElement("Garage", $linhas['item_suite']);
$gar=$xml->createAttribute("Type");
$gar->value = "Parking Space";
$vaga->appendChild($gar);
$detalhe->appendChild($vaga);
// detalhes dos imóveis
$fet=$xml->createElement("Feature");
$fete=$xml->createElement("Feature", strip_tags($linhas['item_piscina']));
$fet->appendChild($fete);
$fete=$xml->createElement("Feature", strip_tags($linhas['item_aquecida']));
$fet->appendChild($fete);
$fete=$xml->createElement("Feature", strip_tags($linhas['item_play']));
$fet->appendChild($fete);
$fete=$xml->createElement("Feature", strip_tags($linhas['item_salao']));
$fet->appendChild($fete);
$fete=$xml->createElement("Feature", strip_tags($linhas['item_portaria24']));
$fet->appendChild($fete);
$fete=$xml->createElement("Feature", strip_tags($linhas['item_monitoramento']));
$fet->appendChild($fete);
$fete=$xml->createElement("Feature", strip_tags($linhas['item_split']));
$fet->appendChild($fete);
$fete=$xml->createElement("Feature", strip_tags($linhas['item_esporte']));
$fet->appendChild($fete);
$fete=$xml->createElement("Feature", strip_tags($linhas['item_academia']));
$fet->appendChild($fete);
$fete=$xml->createElement("Feature", strip_tags($linhas['item_cinema']));
$fet->appendChild($fete);
$fete=$xml->createElement("Feature", strip_tags($linhas['item_churrasqueira']));
$fet->appendChild($fete);
$fete=$xml->createElement("Feature", strip_tags($linhas['item_sauna']));
$fet->appendChild($fete);
$fete=$xml->createElement("Feature", strip_tags($linhas['item_brinquedo']));
$fet->appendChild($fete);
$fete=$xml->createElement("Feature", strip_tags($linhas['item_porcelanato']));
$fet->appendChild($fete);
$detalhe->appendChild($fet);
$listing->appendChild($detalhe);
// fecha detalhes
// abre localização
$location=$xml->createElement("Location");
$loc=$xml->createAttribute("displayAddress");
$loc->value = "All";
$location->appendChild($loc);
//abreviação país
$pais=$xml->createElement("Contry", "Brasil");
$abre=$xml->createAttribute("abbreviation");
$abre->value = "BR";
$pais->appendChild($abre);
$location->appendChild($pais);
// estado
$state=$xml->createElement("State", "Santa Catarina");
$abre=$xml->createAttribute("abbreviation");
$abre->value = "SC";
$state->appendChild($abre);
$location->appendChild($state);
//cidade
$city=$xml->createElement("City", "Balneário Camboriú");
$location->appendChild($city);
//bairro
$neighborhood=$xml->createElement("Neighborhood", "Centro");
$location->appendChild($neighborhood);
$listing->appendChild($location);
//fecha localização
//abre contato
$contact=$xml->createElement("ContactInfo");
$nome=$xml->createElement("Name", "SC WEB IMÓVEIS");
$contact->appendChild($nome);
$mailf=$xml->createElement("Email", "contato@contato.com.br");
$contact->appendChild($mailf);
$site=$xml->createElement("Website", "www.scwebimoveis.com.br");
$contact->appendChild($site);
$office=$xml->createElement("OfficeName", "SC WEB IMÓVEIS");
$contact->appendChild($office);
$phone=$xml->createElement("Telephone", "(47)000000");
$contact->appendChild($phone);
$listing->appendChild($contact);
}
$listdatafeed->appendChild($listings);
$xml->appendChild($listdatafeed);
echo "<xmp>".$xml->saveXML()."</xmp>";
$xml->save("vivareal.xml");
?>Discussão (10)
Carregando comentários...