【问题标题】:How can I rewrite this xml file with the correct encoding using php?如何使用 php 用正确的编码重写这个 xml 文件?
【发布时间】: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&amp;action=edit</adminUrl>
<status>&#x39F;&#x3BB;&#x3BF;&#x3BA;&#x3BB;&#x3B7;&#x3C1;&#x3C9;&#x3BC;&#x3AD;&#x3BD;&#x3B7;</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>&#x391;&#x3BD;&#x3C4;&#x3B9;&#x3BA;&#x3B1;&#x3C4;&#x3B1;&#x3B2;&#x3BF;&#x3BB;&#x3AE;</paymentMethod>
<userId>-1</userId>
<notes/>
<total>53.17</total>
<vatAmount>12.76</vatAmount>
<amount>65.93</amount>
<billing>
  <firstName>&#x395;&#x3C5;&#x3B1;&#x3B3;&#x3B3;&#x3B5;&#x3BB;&#x3AF;&#x3B1;</firstName>
  <lastName>&#x3A4;&#x3C1;&#x3B9;&#x3B1;&#x3BD;&#x3C4;&#x3B1;&#x3C6;&#x3CD;&#x3BB;&#x3BB;&#x3BF;&#x3C5;</lastName>
  <address>25&#x3B7;&#x3C2; &#x39C;&#x3B1;&#x3C1;&#x3C4;&#x3AF;&#x3BF;&#x3C5; 23</address>
  <city>&#x3A3;&#x3AF;&#x3BD;&#x3B4;&#x3BF;&#x3C2;</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);

?>  

【问题讨论】:

    标签: php xml


    【解决方案1】:

    没有正确测试它,但这可能有效。不知道你要找什么样的结果,所以把它做成一个简单的关联数组

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

    输出:

    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) ""
            }
          }
        }
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-14
      • 1970-01-01
      • 2018-06-19
      • 2014-08-21
      • 2011-09-17
      • 1970-01-01
      相关资源
      最近更新 更多