0

I'm trying to update a XML with PHP, and i'm sucessful, but there is a little problem: I don't know how to get XML "class" and edit via PHP. This example was very helpful, here is my form:

<form name="form1" action="converte.php" method="post">

  <input type="text" name="referencia" id="referencia" placeholder="Insira a referência">
  <input type="text" name="titulo" id="titulo" placeholder="Escreva o título">
  <input type="text" name="descricao" id="descricao" placeholder="Insira a descrição">
  <input type="text" name="cidade" id="cidade" placeholder="Insira a cidade">
  <input type="text" name="bairro" id="bairro" placeholder="Bairro">
  <input type="text" name="imagem" id="imagem" placeholder="LINK DA IMAGEM (.jpg)">
  <input type="text" name="banheiros" id="banheiros" placeholder="Banheiros">
  <input type="text" name="quartos" id="quartos" placeholder="Quartos">
  <input type="text" name="preco" id="preco" placeholder="R$ 0.000.000">
  <input type="text" name="tipo" id="tipo" placeholder="Tipo do imóvel (apartamento, casa, comercial....)">
  <input type="text" name="direcionamento" id="direcionamento" placeholder="Link do imóvel no site, para direcionar o clique">
  <input type="submit" value="Gerar XML">
</form>

Here is my PHP (converte.php):

<?php 

    $postReferencia = $_POST['referencia'];
 $postName = $_POST['titulo'];
 $postDescricao = $_POST['descricao'];
 $postCidade = $_POST['cidade'];
 $postBairro = $_POST['bairro'];
 $postImagem = $_POST['imagem'];
 $postBanheiros = $_POST['banheiros'];
 $postQuartos = $_POST['quartos'];
 $postPreco = $_POST['preco'];
 $postTipo = $_POST['tipo'];
 $postDirecionamento = $_POST['direcionamento'];
 

    // load the document
// the root node is <info/> so we load it into $info
$info = simplexml_load_file('facebook.xml');

// update
$info->listing->home_listing_id = $postReferencia;
$info->listing->content_ids = $postReferencia;
$info->listing->name = $postName;
$info->listing->description = $postDescricao;
$info->listing->address->component = $postCidade;
$info->listing->neighborhood = $postBairro;
$info->listing->image->url = $postImagem;
$info->listing->num_baths = $postBanheiros;
$info->listing->num_beds = $postQuartos;
$info->listing->price = $postPreco;
$info->listing->property_type = $postTipo;
$info->listing->url = $postDirecionamento;


// save the updated document
$info->asXML('facebook.xml');

header('Location: index.php');

?>

And this is the XML (facebook.xml)

<?xml version="1.0" encoding="UTF-8"?>
<listings>
    <title>Feed imóvel novo</title>
    <link rel="self" href="http://www.onecia.com.br"/>
    <listing>
        <home_listing_id></home_listing_id>
  <content_ids></content_ids>
        <name></name>
        <description></description>
        <address format="simple">
            <component name="city"> </component>
            <component name="region">Rio Grande do Sul</component>
            <component name="country">Brasil</component>
        </address>
        <neighborhood></neighborhood>
        <image>
            <url></url>
        </image>
        <num_baths></num_baths>
        <num_beds></num_beds>
        <price></price>
        <property_type></property_type>
        <url></url>
    </listing>
</listings>

As you can see, in the XML i have this:

<address format="simple">
        <component name="city"> </component>
        <component name="region">Rio Grande do Sul</component>
        <component name="country">Brasil</component>
    </address>

The question is: How can i get this "city" component? For the other items, i just get the general tag link, but this atributes (format - simple, name - city) i don't know how to gather it by php.

I have this for base, how can i adapt it to gather only the city?

$info->listing->address->component = $postCidade;
Pedro Gusmão
  • 17
  • 1
  • 7

1 Answers1

1

You can extract data based on the attributes by using XPath. So if $info is your XML document...

$city = $info->xpath("//component[@name='city']")[0];
echo $city;

This retrieves a component element which has an attribute 'name' which is 'city'. As ->xpath() returns a list of matching elements, use [0] to just fetch the first result.

If you want to stick to your existing method, you can use the fact that the city is the first component element and use...

$info->listing->address->component[0] = $postCidade;
Nigel Ren
  • 56,122
  • 11
  • 43
  • 55
  • Didn't work... Do you know how i should do it on this string, to keep all the paths on the same logic? $info->listing->address->component = $postCidade; – Pedro Gusmão Dec 13 '17 at 15:39
  • When you say didn't work - do you get an error or just no value? *the value in your example is just a space, so I put something in my test just to ensure I could check it ). – Nigel Ren Dec 13 '17 at 15:40
  • With the actual code, it just returns nothing, the XML comes without the content i've filled on the input (what works for all the other fields). The same happen when i put your idea to work: It doesn't show any error, but i don't see nothing on the output. Here is how i've implemented it: http://prntscr.com/hn0jwk – Pedro Gusmão Dec 13 '17 at 15:53
  • The region and country must be pre-filled, not editable via html/php. This is a setup for Facebook DARE - Dynamic Ads for Real Estate. All our offers are from Brasil, All from Rio Grande do Sul, but from diferent cities. That's why i don't need to change region or country, just "city". – Pedro Gusmão Dec 13 '17 at 16:00
  • Added an extra bit of code which may fit what your after – Nigel Ren Dec 13 '17 at 16:21