I want to rewrite an xml file without the encoded characters using php.
My xml file looks like this:
<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>[email protected]</email>
<isInvoice>0</isInvoice>
<vat/>
<taxOffice/>
<activity/>
</billing>
How can I fix the issue with the encoded characters? Is there any way I could use php to rewrite the file?
thanks.
CodePudding user response:
Didn't test it properly but this might work. Didn't know what kind of result you're looking for so made it as an simple associative array
<?php
$content = 'your xml content';
$decode = static function(SimpleXMLElement $node) use (&$decode) {
if($node->children()->count() > 0) {
$result = [];
foreach($node->children() as $child) {
$result[$child->getName()] = $decode($child);
}
} else {
$result = html_entity_decode((string) $node);
}
return [$node->getName() => $result];
};
$xml = simplexml_load_string(
$content,
SimpleXMLElement::class,
LIBXML_NOERROR | LIBXML_ERR_NONE,
);
var_dump($decode($xml));
Output:
array(1) {
["order"]=>
array(16) {
["orderId"]=>
array(1) {
["orderId"]=>
string(4) "9132"
}
["statusId"]=>
array(1) {
["statusId"]=>
string(1) "3"
}
["adminUrl"]=>
array(1) {
["adminUrl"]=>
string(55) "www.floros24.gr/wp-admin/post.php?post=9132&action=edit"
}
["status"]=>
array(1) {
["status"]=>
string(24) "Ολοκληρωμένη"
}
["dateCreated"]=>
array(1) {
["dateCreated"]=>
string(25) "2021-06-26T22:24:56 03:00"
}
["dateCompleted"]=>
array(1) {
["dateCompleted"]=>
string(25) "2021-06-28T10:56:05 03:00"
}
["dateModified"]=>
array(1) {
["dateModified"]=>
string(0) ""
}
["datePaid"]=>
array(1) {
["datePaid"]=>
string(25) "2021-06-28T10:56:05 03:00"
}
["paymentMethodId"]=>
array(1) {
["paymentMethodId"]=>
string(1) "1"
}
["paymentMethod"]=>
array(1) {
["paymentMethod"]=>
string(24) "Αντικαταβολή"
}
["userId"]=>
array(1) {
["userId"]=>
string(2) "-1"
}
["notes"]=>
array(1) {
["notes"]=>
string(0) ""
}
["total"]=>
array(1) {
["total"]=>
string(5) "53.17"
}
["vatAmount"]=>
array(1) {
["vatAmount"]=>
string(5) "12.76"
}
["amount"]=>
array(1) {
["amount"]=>
string(5) "65.93"
}
["billing"]=>
array(1) {
["billing"]=>
array(14) {
["firstName"]=>
array(1) {
["firstName"]=>
string(18) "Ευαγγελία"
}
["lastName"]=>
array(1) {
["lastName"]=>
string(26) "Τριανταφύλλου"
}
["address"]=>
array(1) {
["address"]=>
string(24) "25ης Μαρτίου 23"
}
["city"]=>
array(1) {
["city"]=>
string(12) "Σίνδος"
}
["postcode"]=>
array(1) {
["postcode"]=>
string(5) "57400"
}
["state"]=>
array(1) {
["state"]=>
string(1) "B"
}
["country"]=>
array(1) {
["country"]=>
string(2) "GR"
}
["phone"]=>
array(1) {
["phone"]=>
string(10) "2310797958"
}
["cellphone"]=>
array(1) {
["cellphone"]=>
string(0) ""
}
["email"]=>
array(1) {
["email"]=>
string(16) "[email protected]"
}
["isInvoice"]=>
array(1) {
["isInvoice"]=>
string(1) "0"
}
["vat"]=>
array(1) {
["vat"]=>
string(0) ""
}
["taxOffice"]=>
array(1) {
["taxOffice"]=>
string(0) ""
}
["activity"]=>
array(1) {
["activity"]=>
string(0) ""
}
}
}
}
}