使用 PHP 将属性添加到子节点 XML
Add attribute to child node XML using PHP
我正在努力在 XML 文件中附加一个具有静态唯一 ID 的子节点。
我正在使用的 XML 提要托管在别处的服务器上,只能通过其 URL.
访问
所述提要遵循这种模式:
<?xml version="1.0" encoding="UTF-8"?>
<properties>
<property>
<title>Some Sunny Place</title>
<address>Some Building, Somewhere, Really Nice</address>
</property>
<property>
<title>Some Rainy PLace Place</title>
<address>Some Gutter, Somewhere, Not So Nice</address>
</property>
</properties>
我想要实现的是使用提要中的 URL 将唯一 ID 添加到 'property' 节点并在备用 [=] 输出 XML 提要39=].
例如example.com/proeprty-feed 包含没有 ID 的提要。使用 PHP 添加 ID 并将提要输出到某物。com/property-feed
<?xml version="1.0" encoding="UTF-8"?>
<properties>
<property upid=123456>
<title>Some Sunny Place</title>
<address>Some Building, Somewhere, Really Nice</address>
</property>
<property upid=abcdef>
<title>Some Rainy PLace Place</title>
<address>Some Gutter, Somewhere, Not So Nice</address>
</property>
</properties>
我试过的是input.php
<?php
$xmlstr = <<<XML
<?xml version="1.0" encoding="UTF-8"?>
<properties>
<property>
<title>Some Sunny Place</title>
<address>Some Building, Somewhere, Really Nice</address>
</property>
<property>
<title>Some Rainy PLace Place</title>
<address>Some Gutter, Somewhere, Not So Nice</address>
</property>
</properties>
XML;
?>
和output.php
<?php
include 'input.php';
$sxe = new SimpleXMLElement($xmlstr);
$sxe->addAttribute('upid', uniqid('prop-'));
echo $sxe->asXML();
?>
但是这个输出:
<properties upid="prop-5ac7c06a39ddd">
<property>
<title>Some Sunny Place</title>
<address>Some Building, Somewhere, Really Nice</address>
</property>
<property>
<title>Some Rainy PLace Place</title>
<address>Some Gutter, Somewhere, Not So Nice</address>
</property>
</properties>
大致如下:
<?php
$string = '<?xml version="1.0" encoding="UTF-8"?>
<properties>
<property>
<title>Some Sunny Place</title>
<address>Some Building, Somewhere, Really Nice</address>
</property>
<property>
<title>Some Rainy PLace Place</title>
<address>Some Gutter, Somewhere, Not So Nice</address>
</property>
</properties>';
$xml = new SimpleXMLElement($string);
for($i = 0; $i < count($xml -> property); $i++) {
$xml -> property[$i] -> addAttribute('upid', uniqid());
}
header('Content-Type: text/xml');
print $xml -> asXml();
<?php
$xml = '<?xml version="1.0" encoding="UTF-8"?>
<properties>
<property>
<title>Some Sunny Place</title>
<address>Some Building, Somewhere, Really Nice</address>
</property>
<property>
<title>Some Rainy PLace Place</title>
<address>Some Gutter, Somewhere, Not So Nice</address>
</property>
</properties>';
$length = strlen('<property>');
while ($pos = strpos($xml, '<property>')) {
$xml = substr_replace($xml, '<property upid=' . uniqid() . '>', $pos, $length);
usleep(10); // Necessary so uniqid() is unique every iteration, it's kind of a hack not sure if it's the best solution
}
var_dump($xml);
我正在努力在 XML 文件中附加一个具有静态唯一 ID 的子节点。
我正在使用的 XML 提要托管在别处的服务器上,只能通过其 URL.
访问所述提要遵循这种模式:
<?xml version="1.0" encoding="UTF-8"?>
<properties>
<property>
<title>Some Sunny Place</title>
<address>Some Building, Somewhere, Really Nice</address>
</property>
<property>
<title>Some Rainy PLace Place</title>
<address>Some Gutter, Somewhere, Not So Nice</address>
</property>
</properties>
我想要实现的是使用提要中的 URL 将唯一 ID 添加到 'property' 节点并在备用 [=] 输出 XML 提要39=].
例如example.com/proeprty-feed 包含没有 ID 的提要。使用 PHP 添加 ID 并将提要输出到某物。com/property-feed
<?xml version="1.0" encoding="UTF-8"?>
<properties>
<property upid=123456>
<title>Some Sunny Place</title>
<address>Some Building, Somewhere, Really Nice</address>
</property>
<property upid=abcdef>
<title>Some Rainy PLace Place</title>
<address>Some Gutter, Somewhere, Not So Nice</address>
</property>
</properties>
我试过的是input.php
<?php
$xmlstr = <<<XML
<?xml version="1.0" encoding="UTF-8"?>
<properties>
<property>
<title>Some Sunny Place</title>
<address>Some Building, Somewhere, Really Nice</address>
</property>
<property>
<title>Some Rainy PLace Place</title>
<address>Some Gutter, Somewhere, Not So Nice</address>
</property>
</properties>
XML;
?>
和output.php
<?php
include 'input.php';
$sxe = new SimpleXMLElement($xmlstr);
$sxe->addAttribute('upid', uniqid('prop-'));
echo $sxe->asXML();
?>
但是这个输出:
<properties upid="prop-5ac7c06a39ddd">
<property>
<title>Some Sunny Place</title>
<address>Some Building, Somewhere, Really Nice</address>
</property>
<property>
<title>Some Rainy PLace Place</title>
<address>Some Gutter, Somewhere, Not So Nice</address>
</property>
</properties>
大致如下:
<?php
$string = '<?xml version="1.0" encoding="UTF-8"?>
<properties>
<property>
<title>Some Sunny Place</title>
<address>Some Building, Somewhere, Really Nice</address>
</property>
<property>
<title>Some Rainy PLace Place</title>
<address>Some Gutter, Somewhere, Not So Nice</address>
</property>
</properties>';
$xml = new SimpleXMLElement($string);
for($i = 0; $i < count($xml -> property); $i++) {
$xml -> property[$i] -> addAttribute('upid', uniqid());
}
header('Content-Type: text/xml');
print $xml -> asXml();
<?php
$xml = '<?xml version="1.0" encoding="UTF-8"?>
<properties>
<property>
<title>Some Sunny Place</title>
<address>Some Building, Somewhere, Really Nice</address>
</property>
<property>
<title>Some Rainy PLace Place</title>
<address>Some Gutter, Somewhere, Not So Nice</address>
</property>
</properties>';
$length = strlen('<property>');
while ($pos = strpos($xml, '<property>')) {
$xml = substr_replace($xml, '<property upid=' . uniqid() . '>', $pos, $length);
usleep(10); // Necessary so uniqid() is unique every iteration, it's kind of a hack not sure if it's the best solution
}
var_dump($xml);