doubi4435 2014-09-23 19:46
浏览 36
已采纳

通过php读取XML

so I have this XML which I'm trying to read via SimpleXML class of php.

<ns0:ASN xmlns:ns0="http://schemas.microsoft.com/dynamics/2008/01/documents/ASN">
<CustPackingSlipJour xmlns="http://schemas.microsoft.com/dynamics/2008/01/documents/ASN" class="entity">
<BON_FileNameSeqNum>4</BON_FileNameSeqNum>
<!-- funktionell: Internt löpnr  -->
<BON_TotalNetAmount>726.00</BON_TotalNetAmount>
<!-- funktionell: Totalt belopp  -->
<BON_TotalTaxAmount>181.50</BON_TotalTaxAmount>
<!-- funktionell: Momsbelopp  -->
<InvoiceAccount>9001</InvoiceAccount>
<!-- funktionell:  -->
<LanguageId>SV</LanguageId>
<!-- funktionell: Språk  -->
<OrderAccount>9001</OrderAccount>
<!-- funktionell: Butiks nr för intern användning! -->
<Qty>4.00</Qty>
<!-- funktionell: Total lev kvant!  -->
<SalesId>24</SalesId>
<!-- funktionell:Försäljningsordernr!  -->
<CustPackingSlipTrans class="entity">
<BON_LineNetAmount>149.00</BON_LineNetAmount>
<!-- funktionell: Orderradsbelopp! -->
<BON_SalesPrice>0.00</BON_SalesPrice>
<!-- funktionell: Försäljningspris!  -->
<DeliveryDate>2014-09-09</DeliveryDate>
<!-- funktionell: Leveransdag! -->
<ItemId>10001</ItemId>
<!-- funktionell: Artikelnr!  -->
<Ordered>1.00</Ordered>
<!-- funktionell:Beställd kvantitet!  -->
<PackingSlipId>00000004_061</PackingSlipId>
<!-- funktionell:  -->
<Qty>1.00</Qty>
<!-- funktionell:Levererad kvantitet!  -->
<InventReportDimHistory class="entity">
<InventDim class="entity"/>
</InventReportDimHistory>
</CustPackingSlipTrans>

I'm trying to access it's element by the code :-

<?php
$asn=simplexml_load_file("ASN.xml");
echo $asn->CustPackingSlipTrans[0]->ItemId;
?>

But this doesn't seems to be working. I get a blank output. I've read some tutorials bout the Simplexml class and reached to this code. But I'm not sure what wrong I'm doing here. How would I access each and every element of the XML ?

All suggestions are appreciated.

  • 写回答

2条回答 默认 最新

  • dqvrlgi3247 2014-09-23 19:51
    关注

    If your XML was properly indented, you'd see it's:

    <CustPackingSlipJour ...>
        ...
        <CustPackingSlipTrans ...>
            ...
            <ItemId>10001</ItemId>
    

    So your code should be:

    echo $asn->CustPackingSlipJour[0]->CustPackingSlipTrans[0]->ItemId;
             ^^^^^^^^^^^^^^^^^^^^^^^^
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?