Skip to content
Advertisement

How can I rewrite this xml file with the correct encoding using php?

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&amp;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>

How can I fix the issue with the encoded characters? Is there any way I could use php to rewrite the file?

thanks.

EDIT:

This Is my code that takes an xml file, and then splits it into piecies based on how many different order ids there are in the base xml file. The XML above is one of the XMLs that has been proccessed by this code, Why does this code returns those xml files with encoded characters? (The encoded characters are html entities.) .

Heres the CODE:

    <?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);

?>  

Advertisement

Answer

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) "etrian@gmail.com"
        }
        ["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) ""
        }
      }
    }
  }
}
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement