php - 如何使用 PHP 正确创建此 XML

标签 php xml

我正在尝试从以下格式的数组创建 XML

<root>
    <kyero>
        <feed_version>3</feed_version>
    </kyero>
    <property>
        <id>12367</id>
        <date>2013-09-27 12:00:10</date>
        <ref>V3TEST</ref>
        <price>250000</price>
        <currency>EUR</currency>
        <price_freq>sale</price_freq>
        <leasehold>0</leasehold>
        <type>villa</type>
        <town>almunecar</town>
        <province>granada</province>
        <country>spain</country>
        <location>
            <latitude>36.728807</latitude>
            <longitude>-3.693466</longitude>
        </location>
        <features>
            <feature>good rental potential</feature>
            <feature>near beach</feature>
            <feature>water possible</feature>
        </features>
        <notes>Private property notes</notes>
        <images>
            <image id="1">
                <url>http://images.kyero.com/12811577_large.jpg</url>
            </image>
            <image id="2">
                <url>http://images.kyero.com/12811578_large.jpg</url>
            </image>
            <image id="3">
                <url>http://images.kyero.com/12811579_large.jpg</url>
            </image>
            <image id="4">
                <url>http://images.kyero.com/12811581_large.jpg</url>
            </image>
            <image id="50">
                <url>http://images.kyero.com/12811582_large.jpg</url>
            </image>
        </images>
    </property>
</root>

重要的是嵌套数组,例如 <features><images> .

到目前为止我构建的数组的一个示例是:

Array
(
    [kyero] => Array
        (
            [feed_version] => 3
        )

    [0] => Array
        (
            [id] => 2024
            [ref] => jtl-2024-4202
            [date] => 2019-11-19 15:34:39
            [title] => A wonderful property
            [price] => 14828
            [price_freq] => month
            [country] => United Kingdom
            [location] => Array
                (
                    [latitude] => 55.311512
                    [longitude] => -2.9378154999999
                )

            [features] => Array
                (
                    [0] => Prominent Building
                    [1] => Great Location
                    [2] => Grade A Office Space
                    [3] => Air Conditioning
                    [4] => Generous Car Parking
                )

            [images] => Array
                (
                    [0] => Array
                        (
                            [url] => https://example.com/image1.jpg
                        )

                    [1] => Array
                        (
                            [url] => https://example.com/image2.jpg
                        )

                )

        )

)

以及将数组转换为 XML 的代码

$xml_data = new SimpleXMLElement('<?xml version="1.0"?><root></root>');
//$data = array as shown above 
array_to_xml($data, $xml_data);

具体问题在于该方法

function array_to_xml($data, &$xml_data, $isImg = FALSE, $isFeature = FALSE) {    
    $i = 1;
    foreach ($data as $key => $value) {
        if (is_numeric($key)) {
            $key = 'property'; //dealing with <0/>..<n/> issues
            if($isImg){
                $key = 'image';
            }
            if($isFeature){
                $key = 'feature';
            }

        }
        if (is_array($value)) {
            if($key == 'images'){
                $isImg = true;
            } elseif($key == 'features'){
                $isFeature = true;
            }
            $subnode = $xml_data->addChild($key);
            if($key == 'image'){
                $subnode->addAttribute('id', $i);
            }
            array_to_xml($value, $subnode, $isImg, $isFeature);
        } else {
            $xml_data->addChild("$key", htmlspecialchars("$value"));
        }
        $i++;
    }
}

除了少数部分之外,XML 的输出按预期输出,特别是诸如功能和图像之类的嵌套部分。请注意每个 <url> 的输出方式包裹在 <feature> 中节点。这应该是 <image>除了 id 之外,节点还像本问题开头所示的原始 XML 中所示。图像节点上的属性也是如此。 (<location> 没有遇到同样的问题)

<features>
    <feature>Prominent Building</feature>
    <feature>Great Location</feature>
    <feature>Grade A Office Space</feature>
    <feature>Air Conditioning</feature>
    <feature>Generous Car Parking</feature>
</features>
<images>
    <feature>
        <url>
        https://example.com/image1.jpg
        </url>
    </feature>
    <feature>
        <url>
        https://example.com/image2.jpg
        </url>
     </feature>
</images>

如果我注释掉

if($isFeature){
    $key = 'feature';
}

elseif($key == 'features'){
    $isFeature = true;
}

图像格式正确

<images>
    <image id="1">
        <url>
        https://example.com/image1.jpg
        </url>
    </image>
    <image id="2">
        <url>
        https://example.com/image2.jpg
        </url>
     </image>
</images>

但是这些功能不正确,而是变成了

<features>
    <property>Prominent Building</property>
    <property>Great Location</property>
    <property>Grade A Office Space</property>
    <property>Air Conditioning</property>
    <property>Generous Car Parking</property>
</features>

<property>节点应该是 <feature>节点代替。

你能帮我解决array_to_xml()吗?正确处理这些和其他嵌套数组的方法?

原始转换代码:https://stackoverflow.com/a/5965940/10884442

最佳答案

我同意 ThW 关于直接创建数据的观点,但我也喜欢简单地做事,SimpleXML 非常适合您想要做的事情。

由于我不知道您如何获取到目前为止的数据,因此我将使用该数组对其进行编码,但不是尝试执行通用的灵活版本,这表明使用 SimpleXML 创建整个结构是多么容易.

它所做的只是使用 SimpleXML 功能通过对象表示法引用元素,代码相当重复,但它所做的只是设置数组中数据的元素...

$xml_data = new SimpleXMLElement('<?xml version="1.0"?><root></root>');

$xml_data->kyero->feed_version = $data['kyero']['feed_version'];

$newProperty = $xml_data->addChild("property");
$newProperty->id = $data[0]['id'];
$newProperty->ref = $data[0]['ref'];
$newProperty->date = $data[0]['date'];
$newProperty->title = $data[0]['title'];
$newProperty->price = $data[0]['price'];
$newProperty->price_freq = $data[0]['price_freq'];
$newProperty->country = $data[0]['country'];
$newProperty->location->latitude = $data[0]['location']['latitude'];
$newProperty->location->longitude = $data[0]['location']['longitude'];

foreach ( $data[0]['features'] as $key => $feature )   {
    $newProperty->features->feature[$key] = $feature;
}
$images = $newProperty->addChild("images");
foreach ( $data[0]['images'] as $key => $image )   {
    $newImage = $images->addChild("image");
    $newImage['id']= $key+1;
    $newImage->url = $image['url'];
}

使用您的示例数据,这给出了...

<?xml version="1.0"?>
<root>
  <kyero>
    <feed_version>3</feed_version>
  </kyero>
  <property>
    <id>2024</id>
    <ref>jtl-2024-4202</ref>
    <date>2019-11-19 15:34:39</date>
    <title>A wonderful property</title>
    <price>14828</price>
    <price_freq>month</price_freq>
    <country>United Kingdom</country>
    <location>
      <latitude>55.311512</latitude>
      <longitude>-2.9378154999999</longitude>
    </location>
    <features>
      <feature>Prominent Building</feature>
      <feature>Great Location</feature>
      <feature>Grade A Office Space</feature>
      <feature>Air Conditioning</feature>
      <feature>Generous Car Parking</feature>
    </features>
    <images>
      <image id="1">
        <url>https://example.com/image1.jpg</url>
      </image>
      <image id="2">
        <url>https://example.com/image2.jpg</url>
      </image>
    </images>
  </property>
</root>

关于php - 如何使用 PHP 正确创建此 XML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59174220/

相关文章:

PHP:如何在生成器函数后进行清理

java - 在hadoop上解析xml文件...

java - 如何根据某些规则重构 XML 文件?

xml - 需要使用 Power Query 将 XML 导入 Excel 的帮助

java - Hibernate - 在 xml 中不相关对象上映射关联

php - 重命名一个表是另一个表中的外键

PHP 和 MYSQL AES_Encrypt 问题

php - mysql数据库转pdf?

java - 当我使用 xml 动态创建 MsWord 文档时,mso-page-orientation 不起作用

php - 使用全局定义变量时出现问题: 'Use of undefined constant'