Usamos cookies para medir audiência e melhorar sua experiência. Você pode aceitar ou recusar a qualquer momento. Veja sobre o iMasters.
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");
?>>
1 minuto atrás, Jhess disse:
Aqui você cria a TAG **<feature></feature>** certo?
não, aqui eu inicio a tag **<feature></feature>**
// detalhes dos imóveis
$fet=$xml->createElement("Feature");
$fete=$xml->createElement("Feature", strip_tags($linhas['item_piscina']));
Aqui inicia ai vem varias na sequencia essa que você postou é a última,Não entendeu onde quis chegar, mas porque você apenas não verifica se os valores das variáveis linhas estão vindo vazios?
Exemplo:
$fet=$xml->createElement("Feature");
if($linhas['item_piscina'] != ""){
$fete=$xml->createElement("Feature", strip_tags($linhas['item_piscina']));
$fet->appendChild($fete);
}>
1 minuto atrás, Jhess disse:
Não entendeu onde quis chegar, mas porque você apenas não verifica se os valores das variáveis linhas estão vindo vazios?
Exemplo:
$fet=$xml->createElement("Feature");
if($linhas['item_piscina'] != ""){
$fete=$xml->createElement("Feature", strip_tags($linhas['item_piscina']));
$fet->appendChild($fete);
}
Desculpa não entendi da primeira vez, mas acredito que entendi agora. Mas ai teria que ter um loop, paa poder verificar todas, ou continuo verificando uma por uma? primeira vez que faço isso.Necessariamente não precisa de um loop, já que estas variáveis não são dinâmicas e são em pouca quantidade, mas caso queira, pode pesquisar por foreach
>
1 minuto atrás, Jhess disse:
Necessariamente não precisa de um loop, já que estas variáveis não são dinâmicas e são em pouca quantidade, mas caso queira, pode pesquisar por foreach
Entendi, eu vou fazer aqui e já volto ai para postar o resultado, obrigado por enquanto
>
12 minutos atrás, Rzorr disse:
Entendi, eu vou fazer aqui e já volto ai para postar o resultado, obrigado por enquanto
Fiz dessa forma, p´orém não escreveu nada não escreveu nem a tag para os que não tem e nem para os que tem.
// detalhes dos imóveis
$fet=$xml->createElement("Feature");
if($linhas['item_piscina'] != ""){
$fete=$xml->createElement("Feature", strip_tags($linhas['item_piscina']));
$fet->appendChild($fete);
}
$fet=$xml->createElement("Feature");
if($linhas['item_aquecida'] != ""){
$fete=$xml->createElement("Feature", strip_tags($linhas['item_aquecida']));
$fet->appendChild($fete);
}
//$fete=$xml->createElement("Feature", strip_tags($linhas['item_aquecida']));
//$fet->appendChild($fete);
$fet=$xml->createElement("Feature");
if($linhas['item_play'] != ""){
$fete=$xml->createElement("Feature", strip_tags($linhas['item_play']));
$fet->appendChild($fete);
}
$fet=$xml->createElement("Feature");
if($linhas['item_salao'] != ""){
$fete=$xml->createElement("Feature", strip_tags($linhas['item_salao']));
$fet->appendChild($fete);
}
$fet=$xml->createElement("Feature");
if($linhas['item_portaria24'] != ""){
$fete=$xml->createElement("Feature", strip_tags($linhas['item_portaria24']));
$fet->appendChild($fete);
}
$fet=$xml->createElement("Feature");
if($linhas['item_monitoramento'] != ""){
$fete=$xml->createElement("Feature", strip_tags($linhas['item_monitoramento']));
$fet->appendChild($fete);
}
$fet=$xml->createElement("Feature");
if($linhas['item_split'] != ""){
$fete=$xml->createElement("Feature", strip_tags($linhas['item_split']));
$fet->appendChild($fete);
}
$fet=$xml->createElement("Feature");
if($linhas['item_esporte'] != ""){
$fete=$xml->createElement("Feature", strip_tags($linhas['item_esporte']));
$fet->appendChild($fete);
}
$fet=$xml->createElement("Feature");
if($linhas['item_academia'] != ""){
$fete=$xml->createElement("Feature", strip_tags($linhas['item_academia']));
$fet->appendChild($fete);
}
$fet=$xml->createElement("Feature");
if($linhas['item_cinema'] != ""){
$fete=$xml->createElement("Feature", strip_tags($linhas['item_cinema']));
$fet->appendChild($fete);
}
$fet=$xml->createElement("Feature");
if($linhas['item_churrasqueira'] != ""){
$fete=$xml->createElement("Feature", strip_tags($linhas['item_churrasqueira']));
$fet->appendChild($fete);
}
$fet=$xml->createElement("Feature");
if($linhas['item_sauna'] != ""){
$fete=$xml->createElement("Feature", strip_tags($linhas['item_sauna']));
$fet->appendChild($fete);
}
$fet=$xml->createElement("Feature");
if($linhas['item_brinquedo'] != ""){
$fete=$xml->createElement("Feature", strip_tags($linhas['item_brinquedo']));
$fet->appendChild($fete);
}
$fet=$xml->createElement("Feature");
if($linhas['item_brinquedo'] != ""){
$fete=$xml->createElement("Feature", strip_tags($linhas['item_porcelanato']));
$fet->appendChild($fete);
}// detalhes dos imóveis
$fet=$xml->createElement("Feature");
if($linhas['item_piscina'] != ""){
$fete=$xml->createElement("Feature", strip_tags($linhas['item_piscina']));
$fet->appendChild($fete);
}
if($linhas['item_aquecida'] != ""){
$fete=$xml->createElement("Feature", strip_tags($linhas['item_aquecida']));
$fet->appendChild($fete);
}
if($linhas['item_play'] != ""){
$fete=$xml->createElement("Feature", strip_tags($linhas['item_play']));
$fet->appendChild($fete);
}
if($linhas['item_salao'] != ""){
$fete=$xml->createElement("Feature", strip_tags($linhas['item_salao']));
$fet->appendChild($fete);
}
if($linhas['item_portaria24'] != ""){
$fete=$xml->createElement("Feature", strip_tags($linhas['item_portaria24']));
$fet->appendChild($fete);
}
if($linhas['item_monitoramento'] != ""){
$fete=$xml->createElement("Feature", strip_tags($linhas['item_monitoramento']));
$fet->appendChild($fete);
}
if($linhas['item_split'] != ""){
$fete=$xml->createElement("Feature", strip_tags($linhas['item_split']));
$fet->appendChild($fete);
}
if($linhas['item_esporte'] != ""){
$fete=$xml->createElement("Feature", strip_tags($linhas['item_esporte']));
$fet->appendChild($fete);
}
if($linhas['item_academia'] != ""){
$fete=$xml->createElement("Feature", strip_tags($linhas['item_academia']));
$fet->appendChild($fete);
}
if($linhas['item_cinema'] != ""){
$fete=$xml->createElement("Feature", strip_tags($linhas['item_cinema']));
$fet->appendChild($fete);
}
if($linhas['item_churrasqueira'] != ""){
$fete=$xml->createElement("Feature", strip_tags($linhas['item_churrasqueira']));
$fet->appendChild($fete);
}
if($linhas['item_sauna'] != ""){
$fete=$xml->createElement("Feature", strip_tags($linhas['item_sauna']));
$fet->appendChild($fete);
}
if($linhas['item_brinquedo'] != ""){
$fete=$xml->createElement("Feature", strip_tags($linhas['item_brinquedo']));
$fet->appendChild($fete);
}
if($linhas['item_porcelanato'] != ""){
$fete=$xml->createElement("Feature", strip_tags($linhas['item_porcelanato']));
$fet->appendChild($fete);
}
Deixe assim.Entendi, erre no fechamento do IF, Cara muito obrigado pela ajuda era isso ´por hoje, muito obrigado mesmo.
Que bom que resolveu seu problema, caso tenha mais dúvidas, não hesite em postar no fórum. :D
>
2 minutos atrás, Rzorr disse: