doumengwei0138 2017-12-13 15:25
浏览 40
已采纳

使用PHP更新XML类

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;
</div>
  • 写回答

1条回答 默认 最新

  • douguluan5102 2017-12-13 15:33
    关注

    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;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 linux驱动,linux应用,多线程