【问题标题】:Update XML with class with PHP使用 PHP 更新 XML 类
【发布时间】:2017-12-13 15:25:35
【问题描述】:

我正在尝试使用 PHP 更新 XML,并且我成功了,但是有一个小问题:我不知道如何获取 XML“类”并通过 PHP 进行编辑。 This example 很有帮助,这是我的表格:

<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>

这是我的 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');

?>

这是 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>

如您所见,在 XML 中我有这个:

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

问题是:我怎样才能得到这个“城市”组件?对于其他项目,我只是获得了一般标签链接,但是这个属性(格式 - 简单,名称 - 城市)我不知道如何通过 php 收集它。

我有这个作为基地,我怎样才能让它只聚集城市?

$info->listing->address->component = $postCidade;

【问题讨论】:

    标签: php html xml


    【解决方案1】:

    您可以使用 XPath 根据属性提取数据。因此,如果 $info 是您的 XML 文档...

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

    这会检索一个组件元素,该组件元素的属性“name”是“city”。由于-&gt;xpath() 返回匹配元素的列表,所以使用[0] 只获取第一个结果。

    如果你想坚持你现有的方法,你可以利用城市是第一个组成元素的事实并使用......

    $info->listing->address->component[0] = $postCidade;
    

    【讨论】:

    • 没用...你知道我应该如何处理这个字符串,以使所有路径保持相同的逻辑吗? $info->listing->address->component = $postCidade;
    • 当你说没有工作时——你得到一个错误还是没有价值? *您示例中的值只是一个空格,所以我在测试中放了一些东西以确保我可以检查它)。
    • 使用实际代码,它什么也不返回,XML 没有我在输入中填写的内容(适用于所有其他字段)。当我将您的想法付诸实践时也会发生同样的情况:它没有显示任何错误,但我在输出中看不到任何内容。以下是我的实现方式:prntscr.com/hn0jwk
    • 地区和国家必须预先填写,不能通过 html/php 编辑。这是 Facebook DARE - 房地产动态广告的设置。我们所有的报价都来自巴西,都来自南里奥格兰德州,但来自不同的城市。这就是为什么我不需要更改地区或国家,只需更改“城市”。
    • 添加了一段额外的代码,可能适合您的需求
    猜你喜欢
    • 2011-06-12
    • 2010-09-15
    • 1970-01-01
    • 2011-05-29
    • 2014-05-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多