【发布时间】:2021-10-18 15:32:39
【问题描述】:
我想用php重写一个没有编码字符的xml文件。
我的 xml 文件如下所示:
<order>
<orderId>9132</orderId>
<statusId>3</statusId>
<adminUrl>www.floros24.gr/wp-admin/post.php?post=9132&action=edit</adminUrl>
<status>Ολοκληρωμένη</status>
<dateCreated>2021-06-26T22:24:56+03:00</dateCreated>
<dateCompleted>2021-06-28T10:56:05+03:00</dateCompleted>
<dateModified/>
<datePaid>2021-06-28T10:56:05+03:00</datePaid>
<paymentMethodId>1</paymentMethodId>
<paymentMethod>Αντικαταβολή</paymentMethod>
<userId>-1</userId>
<notes/>
<total>53.17</total>
<vatAmount>12.76</vatAmount>
<amount>65.93</amount>
<billing>
<firstName>Ευαγγελία</firstName>
<lastName>Τριανταφύλλου</lastName>
<address>25ης Μαρτίου 23</address>
<city>Σίνδος</city>
<postcode>57400</postcode>
<state>B</state>
<country>GR</country>
<phone>2310797958</phone>
<cellphone/>
<email>etrian@gmail.com</email>
<isInvoice>0</isInvoice>
<vat/>
<taxOffice/>
<activity/>
</billing>
如何解决编码字符的问题? 有什么办法可以用php重写文件吗?
谢谢。
编辑:
这是我的代码,它获取一个 xml 文件,然后根据基本 xml 文件中有多少不同的订单 ID 将其拆分为多个部分。上面的 XML 是这段代码处理的 XML 之一,为什么这段代码会返回那些带有编码字符的 xml 文件? (编码的字符是 html 实体。)。
代码如下:
<?php
// Prevent logging.
error_reporting(0);
$xmlFilepath = 'Final.xml';
function formatXML($xmlFilepath) {
$filename = 'info.txt';
$contents = file($filename);
$loadxml = simplexml_load_file($xmlFilepath);
//$key = $loadxml->order[2]->orderId;
$count = $loadxml->order;
// The [i] counter for $key(orderId + name of .xml files).
foreach($count as $plus){
$counter = $counter + 1;
}
for($i = 0; $i <= $counter; $i++){
$key = $loadxml->order[$i]->orderId;
$v = $i;
foreach($contents as $lines){
if ( preg_match_all( "/[^0-9]{$key}[^0-9]/", $lines ) ){
echo "{$key} found! <br>";
} elseif (preg_match_all( "/[^0-9]{$key}[^0-9]/", $lines) != true ) {
$x = false;
while($x == false) {
$orderset = $loadxml->order[$v];
// ORIGINAL
$formatxml = new SimpleXMLElement($orderset->saveXML());
$formatxml->saveXML("/home/floros24/public_html/farmaxml/orders/{$key}.xml"); // Save as {key.xml}.
// Open the file to get existing content
$current = file_get_contents($filename);
// Append a new person to the file
$current .= " {$key}";
// Write the contents back to the file
file_put_contents($filename, $current);
$x = true;
}
}
}
} // <--- Closing For[i].
} // <--- Closing of the Function.
formatXML($xmlFilepath);
?>
【问题讨论】: