使用SimpleXML解析/读取XML

时间:2016-10-11 15:14:07

标签: php xml parsing

我尝试使用simplexml读取/解析XML。问题是它没有返回任何结果。我怀疑错误将出现在foreach声明的某处。有谁有想法吗?我将非常感激!

PHP:

$xml = file_get_contents("test.xml");
$feed = simplexml_load_string($xml);
$namespaces = $feed->getNamespaces(true);
print_r($namespaces);
foreach($feed->children($namespaces['ord']) as $child) {
    $orde = $child->orderHeader;
    $order = $orde->orderType;
    print_r($order);
}

XML:

<dat:dataPack version="2.0" id="Usr01" ico="25115308" application="Transformace" note="25115308" xmlns:dat="http://www.stormware.cz/schema/version_2/data.xsd">
<dat:dataPackItem version="2.0" id="Usr01 (001)">
    <ord:order version="2.0" xmlns:ord="http://www.stormware.cz/schema/version_2/order.xsd">
        <ord:orderHeader xmlns:rsp="http://www.stormware.cz/schema/version_2/response.xsd" xmlns:rdc="http://www.stormware.cz/schema/version_2/documentresponse.xsd" xmlns:typ="http://www.stormware.cz/schema/version_2/type.xsd" xmlns:lst="http://www.stormware.cz/schema/version_2/list.xsd" xmlns:lStk="http://www.stormware.cz/schema/version_2/list_stock.xsd" xmlns:lAdb="http://www.stormware.cz/schema/version_2/list_addBook.xsd" xmlns:acu="http://www.stormware.cz/schema/version_2/accountingunit.xsd" xmlns:inv="http://www.stormware.cz/schema/version_2/invoice.xsd" xmlns:vch="http://www.stormware.cz/schema/version_2/voucher.xsd" xmlns:int="http://www.stormware.cz/schema/version_2/intDoc.xsd" xmlns:stk="http://www.stormware.cz/schema/version_2/stock.xsd" xmlns:ofr="http://www.stormware.cz/schema/version_2/offer.xsd" xmlns:enq="http://www.stormware.cz/schema/version_2/enquiry.xsd" xmlns:vyd="http://www.stormware.cz/schema/version_2/vydejka.xsd" xmlns:pri="http://www.stormware.cz/schema/version_2/prijemka.xsd" xmlns:bal="http://www.stormware.cz/schema/version_2/balance.xsd" xmlns:pre="http://www.stormware.cz/schema/version_2/prevodka.xsd" xmlns:vyr="http://www.stormware.cz/schema/version_2/vyroba.xsd" xmlns:pro="http://www.stormware.cz/schema/version_2/prodejka.xsd" xmlns:con="http://www.stormware.cz/schema/version_2/contract.xsd" xmlns:adb="http://www.stormware.cz/schema/version_2/addressbook.xsd" xmlns:prm="http://www.stormware.cz/schema/version_2/parameter.xsd" xmlns:lCon="http://www.stormware.cz/schema/version_2/list_contract.xsd" xmlns:ctg="http://www.stormware.cz/schema/version_2/category.xsd" xmlns:ipm="http://www.stormware.cz/schema/version_2/intParam.xsd" xmlns:str="http://www.stormware.cz/schema/version_2/storage.xsd" xmlns:idp="http://www.stormware.cz/schema/version_2/individualPrice.xsd" xmlns:sup="http://www.stormware.cz/schema/version_2/supplier.xsd" xmlns:prn="http://www.stormware.cz/schema/version_2/print.xsd" xmlns:act="http://www.stormware.cz/schema/version_2/accountancy.xsd" xmlns:bnk="http://www.stormware.cz/schema/version_2/bank.xsd" xmlns:sto="http://www.stormware.cz/schema/version_2/store.xsd" xmlns:grs="http://www.stormware.cz/schema/version_2/groupStocks.xsd" xmlns:acp="http://www.stormware.cz/schema/version_2/actionPrice.xsd" xmlns:csh="http://www.stormware.cz/schema/version_2/cashRegister.xsd" xmlns:ftr="http://www.stormware.cz/schema/version_2/filter.xsd">
            <ord:orderType>receivedOrder</ord:orderType>
            <ord:number>
                <typ:ids>16BPO</typ:ids>
            </ord:number>
            <ord:date>2015-07-01</ord:date>
            <ord:partnerIdentity>
                <typ:address>
                    <typ:company>primy prodej</typ:company>
                    <typ:name>Restaurace LAVIČKA</typ:name>
                    <typ:city>Praha</typ:city>
                    <typ:street>Seifertova 303/77</typ:street>
                    <typ:zip>130 00</typ:zip>
                    <typ:ico>27419932</typ:ico>
                    <typ:dic>CZ27419932</typ:dic>
                </typ:address>
                <typ:shipToAddress>
                    <typ:company></typ:company>
                    <typ:name></typ:name>
                    <typ:city></typ:city>
                    <typ:street></typ:street>
                </typ:shipToAddress>
            </ord:partnerIdentity>
            <ord:myIdentity>
                <typ:address>
                    <typ:company>Front Line </typ:company>
                    <typ:surname>Dostál</typ:surname>
                    <typ:name>Richard</typ:name>
                    <typ:city>Praha 9</typ:city>
                    <typ:street>Kovářská  </typ:street>
                    <typ:number>4/939</typ:number>
                    <typ:zip>190 00</typ:zip>
                    <typ:ico>25115308</typ:ico>
                    <typ:dic>CZ25115308</typ:dic>
                    <typ:phone>+420326780469</typ:phone>
                    <typ:mobilPhone>+420737446403</typ:mobilPhone>
                    <typ:fax>+420326990087</typ:fax>
                    <typ:email>info@nuevoprogreso.cz</typ:email>
                    <typ:www>www.front-line.cz</typ:www>
                </typ:address>
            </ord:myIdentity>
            <ord:paymentType>
                <typ:ids>příkazem</typ:ids>
                <typ:paymentType>draft</typ:paymentType>
            </ord:paymentType>
            <ord:isExecuted>true</ord:isExecuted>
            <ord:isDelivered>true</ord:isDelivered>
            <ord:isReserved>false</ord:isReserved>
            <ord:permamentDocument>false</ord:permamentDocument>
            <ord:parameters>
                <typ:parameter>
                    <typ:name>VPrExpres</typ:name>
                    <typ:booleanValue>false</typ:booleanValue>
                </typ:parameter>
            </ord:parameters>
        </ord:orderHeader>
        <ord:orderDetail xmlns:rsp="http://www.stormware.cz/schema/version_2/response.xsd" xmlns:rdc="http://www.stormware.cz/schema/version_2/documentresponse.xsd" xmlns:typ="http://www.stormware.cz/schema/version_2/type.xsd" xmlns:lst="http://www.stormware.cz/schema/version_2/list.xsd" xmlns:lStk="http://www.stormware.cz/schema/version_2/list_stock.xsd" xmlns:lAdb="http://www.stormware.cz/schema/version_2/list_addBook.xsd" xmlns:acu="http://www.stormware.cz/schema/version_2/accountingunit.xsd" xmlns:inv="http://www.stormware.cz/schema/version_2/invoice.xsd" xmlns:vch="http://www.stormware.cz/schema/version_2/voucher.xsd" xmlns:int="http://www.stormware.cz/schema/version_2/intDoc.xsd" xmlns:stk="http://www.stormware.cz/schema/version_2/stock.xsd" xmlns:ofr="http://www.stormware.cz/schema/version_2/offer.xsd" xmlns:enq="http://www.stormware.cz/schema/version_2/enquiry.xsd" xmlns:vyd="http://www.stormware.cz/schema/version_2/vydejka.xsd" xmlns:pri="http://www.stormware.cz/schema/version_2/prijemka.xsd" xmlns:bal="http://www.stormware.cz/schema/version_2/balance.xsd" xmlns:pre="http://www.stormware.cz/schema/version_2/prevodka.xsd" xmlns:vyr="http://www.stormware.cz/schema/version_2/vyroba.xsd" xmlns:pro="http://www.stormware.cz/schema/version_2/prodejka.xsd" xmlns:con="http://www.stormware.cz/schema/version_2/contract.xsd" xmlns:adb="http://www.stormware.cz/schema/version_2/addressbook.xsd" xmlns:prm="http://www.stormware.cz/schema/version_2/parameter.xsd" xmlns:lCon="http://www.stormware.cz/schema/version_2/list_contract.xsd" xmlns:ctg="http://www.stormware.cz/schema/version_2/category.xsd" xmlns:ipm="http://www.stormware.cz/schema/version_2/intParam.xsd" xmlns:str="http://www.stormware.cz/schema/version_2/storage.xsd" xmlns:idp="http://www.stormware.cz/schema/version_2/individualPrice.xsd" xmlns:sup="http://www.stormware.cz/schema/version_2/supplier.xsd" xmlns:prn="http://www.stormware.cz/schema/version_2/print.xsd" xmlns:act="http://www.stormware.cz/schema/version_2/accountancy.xsd" xmlns:bnk="http://www.stormware.cz/schema/version_2/bank.xsd" xmlns:sto="http://www.stormware.cz/schema/version_2/store.xsd" xmlns:grs="http://www.stormware.cz/schema/version_2/groupStocks.xsd" xmlns:acp="http://www.stormware.cz/schema/version_2/actionPrice.xsd" xmlns:csh="http://www.stormware.cz/schema/version_2/cashRegister.xsd" xmlns:ftr="http://www.stormware.cz/schema/version_2/filter.xsd">
            <ord:orderItem>
                <ord:text>Western Guacamole 2 kg</ord:text>
                <ord:quantity>2.0</ord:quantity>
                <ord:delivered>2.0</ord:delivered>
                <ord:unit>ks</ord:unit>
                <ord:coefficient>1.0</ord:coefficient>
                <ord:payVAT>false</ord:payVAT>
                <ord:rateVAT>low</ord:rateVAT>
                <ord:discountPercentage>0.0</ord:discountPercentage>
                <ord:homeCurrency>
                    <typ:unitPrice>425.92</typ:unitPrice>
                    <typ:price>851.84</typ:price>
                    <typ:priceVAT>127.78</typ:priceVAT>
                    <typ:priceSum>979.62</typ:priceSum>
                </ord:homeCurrency>
                <ord:note>acompaňar</ord:note>
                <ord:code>900143</ord:code>
                <ord:stockItem>
                    <typ:store>
                        <typ:ids>MOCHOV</typ:ids>
                    </typ:store>
                    <typ:stockItem>
                        <typ:ids>900143</typ:ids>
                        <typ:EAN>8717154056709</typ:EAN>
                    </typ:stockItem>
                </ord:stockItem>
            </ord:orderItem>
            <ord:orderItem>
                <ord:text>Tomato Chili Wrap 10"</ord:text>
                <ord:quantity>100.0</ord:quantity>
                <ord:delivered>100.0</ord:delivered>
                <ord:unit>ks</ord:unit>
                <ord:coefficient>1.0</ord:coefficient>
                <ord:payVAT>false</ord:payVAT>
                <ord:rateVAT>low</ord:rateVAT>
                <ord:discountPercentage>0.0</ord:discountPercentage>
                <ord:homeCurrency>
                    <typ:unitPrice>5.22</typ:unitPrice>
                    <typ:price>522</typ:price>
                    <typ:priceVAT>78.3</typ:priceVAT>
                    <typ:priceSum>600.3</typ:priceSum>
                </ord:homeCurrency>
                <ord:note>tortillas</ord:note>
                <ord:code>F500230T</ord:code>
                <ord:stockItem>
                    <typ:store>
                        <typ:ids>MOCHOV</typ:ids>
                    </typ:store>
                    <typ:stockItem>
                        <typ:ids>F500230T</typ:ids>
                        <typ:EAN>8594053682457</typ:EAN>
                    </typ:stockItem>
                </ord:stockItem>
            </ord:orderItem>
        </ord:orderDetail>
        <ord:orderSummary xmlns:rsp="http://www.stormware.cz/schema/version_2/response.xsd" xmlns:rdc="http://www.stormware.cz/schema/version_2/documentresponse.xsd" xmlns:typ="http://www.stormware.cz/schema/version_2/type.xsd" xmlns:lst="http://www.stormware.cz/schema/version_2/list.xsd" xmlns:lStk="http://www.stormware.cz/schema/version_2/list_stock.xsd" xmlns:lAdb="http://www.stormware.cz/schema/version_2/list_addBook.xsd" xmlns:acu="http://www.stormware.cz/schema/version_2/accountingunit.xsd" xmlns:inv="http://www.stormware.cz/schema/version_2/invoice.xsd" xmlns:vch="http://www.stormware.cz/schema/version_2/voucher.xsd" xmlns:int="http://www.stormware.cz/schema/version_2/intDoc.xsd" xmlns:stk="http://www.stormware.cz/schema/version_2/stock.xsd" xmlns:ofr="http://www.stormware.cz/schema/version_2/offer.xsd" xmlns:enq="http://www.stormware.cz/schema/version_2/enquiry.xsd" xmlns:vyd="http://www.stormware.cz/schema/version_2/vydejka.xsd" xmlns:pri="http://www.stormware.cz/schema/version_2/prijemka.xsd" xmlns:bal="http://www.stormware.cz/schema/version_2/balance.xsd" xmlns:pre="http://www.stormware.cz/schema/version_2/prevodka.xsd" xmlns:vyr="http://www.stormware.cz/schema/version_2/vyroba.xsd" xmlns:pro="http://www.stormware.cz/schema/version_2/prodejka.xsd" xmlns:con="http://www.stormware.cz/schema/version_2/contract.xsd" xmlns:adb="http://www.stormware.cz/schema/version_2/addressbook.xsd" xmlns:prm="http://www.stormware.cz/schema/version_2/parameter.xsd" xmlns:lCon="http://www.stormware.cz/schema/version_2/list_contract.xsd" xmlns:ctg="http://www.stormware.cz/schema/version_2/category.xsd" xmlns:ipm="http://www.stormware.cz/schema/version_2/intParam.xsd" xmlns:str="http://www.stormware.cz/schema/version_2/storage.xsd" xmlns:idp="http://www.stormware.cz/schema/version_2/individualPrice.xsd" xmlns:sup="http://www.stormware.cz/schema/version_2/supplier.xsd" xmlns:prn="http://www.stormware.cz/schema/version_2/print.xsd" xmlns:act="http://www.stormware.cz/schema/version_2/accountancy.xsd" xmlns:bnk="http://www.stormware.cz/schema/version_2/bank.xsd" xmlns:sto="http://www.stormware.cz/schema/version_2/store.xsd" xmlns:grs="http://www.stormware.cz/schema/version_2/groupStocks.xsd" xmlns:acp="http://www.stormware.cz/schema/version_2/actionPrice.xsd" xmlns:csh="http://www.stormware.cz/schema/version_2/cashRegister.xsd" xmlns:ftr="http://www.stormware.cz/schema/version_2/filter.xsd">
            <ord:roundingDocument>math2one</ord:roundingDocument>
            <ord:roundingVAT>none</ord:roundingVAT>
            <ord:homeCurrency>
                <typ:priceNone>0</typ:priceNone>
                <typ:priceLow>1373.84</typ:priceLow>
                <typ:priceLowVAT>206.08</typ:priceLowVAT>
                <typ:priceLowSum>1579.92</typ:priceLowSum>
                <typ:priceHigh>0</typ:priceHigh>
                <typ:priceHighVAT>0</typ:priceHighVAT>
                <typ:priceHighSum>0</typ:priceHighSum>
                <typ:price3>0</typ:price3>
                <typ:price3VAT>0</typ:price3VAT>
                <typ:price3Sum>0</typ:price3Sum>
                <typ:round>
                    <typ:priceRound>0.08</typ:priceRound>
                </typ:round>
            </ord:homeCurrency>
        </ord:orderSummary>
    </ord:order>
</dat:dataPackItem>

1 个答案:

答案 0 :(得分:0)

1)你有一个缺失的数据:最后是dataPack。

2)您有多个名称空间,并试图像访问它一样进行访问。

修复结束标记后,删除dat:datapack和dat:datapackItem,它将起作用。

要了解如何让多个命名空间工作,请检查此&gt; parse an XML with SimpleXML which has multiple namespaces