如果能與具有空間的標籤,整齊是一個不錯的選擇,因爲它適用於XML的重命名活,太:
$xml = simplexml_load_string(
tidy_repair_string($string, ['input-xml' => 1])
);
echo "SimpleXML::asXML():\n", $xml->asXML(), "\n\n";
它重命名的標籤,並創建屬性:
SimpleXML::asXML():
<?xml version="1.0"?>
<Public Transport="" Rules="">
<PublicTransport id="0">
<Issued>null</Issued>
<Files><localfile> <location>Citybus</location>
<format>Events</format> </localfile></Files>
</PublicTransport>
</Public>
還有壓痕等,在這裏一個完整的例子更多的選擇:
<?php
/**
* How to parse XML files with errors using Simplexml in PHP?
*
* @link http://stackoverflow.com/q/15620492/367456
*/
$string = '<?xml version="1.0" ?>
<Public Transport Rules>
<PublicTransport id="0">
<Issued>null</Issued>
<Files><localfile>
<location>Citybus</location>
<format>Events</format>
</localfile>
</Files>
</PublicTransport>
</Public Transport Rules>';
echo "Broken:\n", $string, "\n\n";
$fixed = tidy_repair_string($string, ['input-xml' => 1, 'output-xml' => 1, 'indent' => 1]);
echo "Fixed:\n", $fixed, "\n\n";
$xml = simplexml_load_string(tidy_repair_string($string, ['input-xml' => 1]));
echo "SimpleXML::asXML():\n", $xml->asXML(), "\n\n";
輸出:因此
Broken:
<?xml version="1.0" ?>
<Public Transport Rules>
<PublicTransport id="0">
<Issued>null</Issued>
<Files><localfile>
<location>Citybus</location>
<format>Events</format>
</localfile>
</Files>
</PublicTransport>
</Public Transport Rules>
Fixed:
<?xml version="1.0"?>
<Public Transport="" Rules="">
<PublicTransport id="0">
<Issued>null</Issued>
<Files><localfile>
<location>Citybus</location>
<format>Events</format> </localfile></Files>
</PublicTransport>
</Public>
SimpleXML::asXML():
<?xml version="1.0"?>
<Public Transport="" Rules="">
<PublicTransport id="0">
<Issued>null</Issued>
<Files><localfile> <location>Citybus</location>
<format>Events</format> </localfile></Files>
</PublicTransport>
</Public>
一個XML文件中應遵循XML規範有效,100%有效。然而,你可以檢查goutte或symfony/domcrawler庫來獲取html內容。 – mpm 2013-03-25 17:01:03
所以我唯一的解決方案是改變每一個問題文件? – demenzia 2013-03-25 17:03:25