[simple_html_dom](http://simplehtmldom.sourceforge.net/)是一個很好用的 x(h)tml解析器
不過在使用上有些麻煩
所以就自己寫了將simple_html_dom轉array的function
xml檔案
```xml
<orderdoc>
<dochead>
<docno>999XXXyyyymmdd99</docno>
<docdate>2009-02-01</docdate>
<parentid>999</parentid>
</dochead>
<doccontent>
<order>
<eshopid>001</eshopid>
<opmode>A</opmode>
<eshoporderno>200906010001</eshoporderno>
<eshoporderdate>2009-02-01</eshoporderdate>
<servicetype>1</servicetype>
<shoppername>張小明</shoppername>
<shopperphone></shopperphone>
<shoppermobilphone></shoppermobilphone>
<shopperemail></shopperemail>
<receivername>張小明</receivername>
<receiverphone></receiverphone>
<receivermobilphone></receivermobilphone>
<receiveremail></receiveremail>
<receiveridnumber></receiveridnumber>
<orderamount>2000</orderamount>
<orderdetail>
<productid></productid>
<productname></productname>
<quantity></quantity>
<unit></unit>
<unitprice></unitprice>
</orderdetail>
<shipmentdetail>
<shipmentno>20000001</shipmentno>
<shipdate>2009-02-01</shipdate>
<returndate>2009-02-9</returndate>
<lastshipment>Y</LastShipment>
<shipmentamount>2000</shipmentamount>
<storeid>886751</storeid>
<eshoptype>04</eshoptype>
</shipmentdetail>
</lastshipment>
</returndate>
</shipdate>
</shipmentno>
</shipmentdetail>
</order>
<order>
<eshopid>001</eshopid>
<opmode>A</opmode>
<eshoporderno>200906010002</eshoporderno>
<eshoporderdate>2009-02-01</eshoporderdate>
廠商上線說明書
This Document is 7-Eleven /PIC/ Presco Confidential Page: 17
<servicetype>1</servicetype>
<shoppername>林志玲</shoppername>
<shopperphone></shopperphone>
<shoppermobilphone></shoppermobilphone>
<shopperemail></shopperemail>
<receivername>林志玲</receivername>
<receiverphone></receiverphone>
<receivermobilphone></receivermobilphone>
<receiveremail></receiveremail>
<receiveridnumber></receiveridnumber>
<orderamount>1200</orderamount>
<orderdetail>
<productid></productid>
<productname></productname>
<quantity></quantity>
<unit></unit>
<unitprice></unitprice>
</orderdetail>
<shipmentdetail>
<shipmentno>20000002</shipmentno>
<shipdate>2009-02-01</shipdate>
<returndate>2009-02-9</returndate>
<lastshipment>N</LastShipment>
<shipmentamount>800</shipmentamount>
<storeid>886750</storeid>
<eshoptype>04</eshoptype>
</shipmentdetail>
<shipmentdetail>
<shipmentno>20000003</shipmentno>
<shipdate>2009-02-01</shipdate>
<returndate>2009-02-9</returndate>
<lastshipment>Y</LastShipment>
<shipmentamount>400</shipmentamount>
<storeid>886750</storeid>
<eshoptype>04</eshoptype>
<shipmentdetail>
</lastshipment>
</returndate>
</shipdate>
</shipmentno>
</shipmentdetail>
</lastshipment>
</returndate>
</shipdate>
</shipmentno>
</shipmentdetail>
</order>
</doccontent>
</orderdoc>
```
php檔案
```php
define("DOCROOT",dirname(__FILE__)."/");
require DOCROOT."simple_html_dom.php";
function domToArray($elements)
{
$results = array();
foreach ($elements as $element)
{
if (count($element->children) > 0)
{
$results[$element->tag][] = domToArray($element->children);
}
else
{
$results[$element->tag] = $element->text();
}
}
return $results;
}
$root = file_get_html(DOCROOT."test.xml");
$order_doms = domToArray($root->find("Order"));
print_r($order_doms);
```
沒有留言:
張貼留言