Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
通过php读取XML_Php_Xml - Fatal编程技术网

通过php读取XML

通过php读取XML,php,xml,Php,Xml,所以我有这个XML,我正试图通过php的SimpleXML类读取它 <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_FileNameS

所以我有这个XML,我正试图通过php的SimpleXML类读取它

<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>

4.
726
181.50
9001
SV
9001
4
24
149
0
2014-09-09
10001
1
00000004_061
1
我试图通过代码访问它的元素:-

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

但这似乎不起作用。我得到一个空白输出。我已经阅读了一些关于Simplexml类的教程,并了解了这段代码。但我不确定我到底做错了什么。 如何访问XML的每个元素


欢迎所有建议

如果XML正确缩进,您将看到:

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

您应始终尝试并遵循以下顺序:-

echo $objectname->parentname->childname->subchild
这总是有效的。但是,您应该始终缩进XML。这是最基本的要求。如果缩进,可以很容易地看到哪个是父元素,哪个是子元素

echo $objectname->parentname->childname->subchild