PHP - Parsing SimpleXMLElement Object

时间:2017-04-10 00:31:33

标签: php xml-parsing simplexml

Here's the SimpleXMLElement Object that is parsed from the API response which is stored in a $invoices variable:

SimpleXMLElement Object
(
    [Id] => 6e292df0-a09c-4bbb-9203-278e7d3fd585
    [Status] => OK
    [ProviderName] => fmAccounting Link (Xero Edition) Demo
    [DateTimeUTC] => 2017-04-10T00:04:25.5044126Z
    [Invoices] => SimpleXMLElement Object
        (
            [Invoice] => Array
                (
                    [0] => SimpleXMLElement Object
                        (
                            [Contact] => SimpleXMLElement Object
                                (
                                    [ContactID] => 0e72016e-7c60-4a19-b8d0-1d2c58cc0b49
                                    [Name] => Jerrys Tires
                                )

                            [Date] => 2017-03-02T00:00:00
                            [DueDate] => 2017-03-16T00:00:00
                            [Status] => AUTHORISED
                            [LineAmountTypes] => Exclusive
                            [LineItems] => SimpleXMLElement Object
                                (
                                    [LineItem] => SimpleXMLElement Object
                                        (
                                            [ItemCode] => PR1113
                                            [Description] => Acme Doodles
                                            [UnitAmount] => 645.00
                                            [TaxType] => OUTPUT
                                            [TaxAmount] => 64.50
                                            [LineAmount] => 645.00
                                            [AccountCode] => 200
                                            [Quantity] => 1.0000
                                            [LineItemID] => 5a3da2f4-271a-4bdb-aeb9-fca65c4faa90
                                        )

                                )

                            [SubTotal] => 645.00
                            [TotalTax] => 64.50
                            [Total] => 709.50
                            [UpdatedDateUTC] => 2017-03-01T20:45:01.5
                            [CurrencyCode] => AUD
                            [Type] => ACCREC
                            [InvoiceID] => 5202c206-30d7-4792-8a37-32d1d8bee79f
                            [InvoiceNumber] => IV1950
                            [Payments] => SimpleXMLElement Object
                                (
                                    [Payment] => SimpleXMLElement Object
                                        (
                                            [PaymentID] => b443039a-409e-47cf-a5d9-4cde1640269d
                                            [Date] => 2017-03-02T00:00:00
                                            [Amount] => 500.00
                                            [Reference] => test
                                            [CurrencyRate] => 1.000000
                                        )

                                )

                            [AmountDue] => 209.50
                            [AmountPaid] => 500.00
                            [AmountCredited] => 0.00
                            [SentToContact] => false
                            [CurrencyRate] => 1.000000
                            [HasAttachments] => false
                        )

                    [1] => SimpleXMLElement Object
                        (
                            [Contact] => SimpleXMLElement Object
                                (
                                    [ContactID] => 0e72016e-7c60-4a19-b8d0-1d2c58cc0b49
                                    [Name] => Jerrys Tires
                                )

                            [Date] => 2017-03-06T00:00:00
                            [DueDate] => 2017-03-20T00:00:00
                            [Status] => AUTHORISED
                            [LineAmountTypes] => NoTax
                            [LineItems] => SimpleXMLElement Object
                                (
                                    [LineItem] => SimpleXMLElement Object
                                        (
                                            [ItemCode] => PR1113
                                            [Description] => Acme Doodles
                                            [UnitAmount] => 645.00
                                            [TaxType] => NONE
                                            [TaxAmount] => 0.00
                                            [LineAmount] => 645.00
                                            [AccountCode] => 200
                                            [Quantity] => 1.0000
                                            [LineItemID] => bc3f2602-b712-49c8-8696-3af1ce589b05
                                        )

                                )

                            [SubTotal] => 645.00
                            [TotalTax] => 0.00
                            [Total] => 645.00
                            [UpdatedDateUTC] => 2017-03-05T19:10:52.57
                            [CurrencyCode] => AUD
                            [Type] => ACCREC
                            [InvoiceID] => 50f95c0b-5498-47b9-8034-9f1ed11d759e
                            [InvoiceNumber] => IV1951
                            [Payments] => SimpleXMLElement Object
                                (
                                    [Payment] => SimpleXMLElement Object
                                        (
                                            [PaymentID] => 3f5a4d70-20d2-47cc-a661-3a528958c801
                                            [Date] => 2017-03-06T00:00:00
                                            [Amount] => 380.00
                                            [Reference] => cash
                                            [CurrencyRate] => 1.000000
                                        )

                                )

                            [AmountDue] => 265.00
                            [AmountPaid] => 380.00
                            [AmountCredited] => 0.00
                            [SentToContact] => false
                            [CurrencyRate] => 1.000000
                            [HasAttachments] => false
                        )

                    [2] => SimpleXMLElement Object
                        (
                            [Contact] => SimpleXMLElement Object
                                (
                                    [ContactID] => 2842ce37-86e0-4e89-936b-e1138e79dcf7
                                    [Name] => Cons Fruits
                                )

                            [Date] => 2017-03-06T00:00:00
                            [DueDate] => 2017-03-20T00:00:00
                            [Status] => PAID
                            [LineAmountTypes] => NoTax
                            [LineItems] => SimpleXMLElement Object
                                (
                                    [LineItem] => Array
                                        (
                                            [0] => SimpleXMLElement Object
                                                (
                                                    [ItemCode] => DevD
                                                    [Description] => Development work - developer onsite per day
                                                    [UnitAmount] => 650.00
                                                    [TaxType] => NONE
                                                    [TaxAmount] => 0.00
                                                    [LineAmount] => 650.00
                                                    [AccountCode] => 200
                                                    [Quantity] => 1.0000
                                                    [LineItemID] => c413f22a-d1ff-460f-91e3-a4793eb7b963
                                                )

                                            [1] => SimpleXMLElement Object
                                                (
                                                    [ItemCode] => PR1113
                                                    [Description] => Acme Doodles
                                                    [UnitAmount] => 645.00
                                                    [TaxType] => NONE
                                                    [TaxAmount] => 0.00
                                                    [LineAmount] => 645.00
                                                    [AccountCode] => 200
                                                    [Quantity] => 1.0000
                                                    [LineItemID] => c624c314-d8d8-4e08-861b-eda40af4ae5b
                                                )

                                        )

                                )

                            [SubTotal] => 1295.00
                            [TotalTax] => 0.00
                            [Total] => 1295.00
                            [UpdatedDateUTC] => 2017-03-05T22:06:10.263
                            [CurrencyCode] => AUD
                            [FullyPaidOnDate] => 2017-03-06T00:00:00
                            [Type] => ACCREC
                            [InvoiceID] => bb22c090-45b6-4357-82d0-f39daf45fa21
                            [InvoiceNumber] => IV1952
                            [Payments] => SimpleXMLElement Object
                                (
                                    [Payment] => SimpleXMLElement Object
                                        (
                                            [PaymentID] => 8c0f5409-0fe4-4a42-85f7-056ec2e2f824
                                            [Date] => 2017-03-06T00:00:00
                                            [Amount] => 1295.00
                                            [Reference] => kged;oidgs
                                            [CurrencyRate] => 1.000000
                                        )

                                )

                            [AmountDue] => 0.00
                            [AmountPaid] => 1295.00
                            [AmountCredited] => 0.00
                            [SentToContact] => false
                            [CurrencyRate] => 1.000000
                            [HasAttachments] => false
                        )

                    [3] => SimpleXMLElement Object
                        (
                            [Contact] => SimpleXMLElement Object
                                (
                                    [ContactID] => e0413ee8-7b41-40e9-99c4-dd6b47c65564
                                    [ContactNumber] => CT1686
                                    [Name] => Acme Corp Inc
                                )

                            [Date] => 2017-03-15T00:00:00
                            [DueDate] => 2017-03-29T00:00:00
                            [BrandingThemeID] => 2ced98b8-3be9-42c4-ae79-fe3c8bca3490
                            [Status] => AUTHORISED
                            [LineAmountTypes] => Exclusive
                            [LineItems] => SimpleXMLElement Object
                                (
                                    [LineItem] => SimpleXMLElement Object
                                        (
                                            [ItemCode] => Support-M
                                            [Description] => Desktop/network support via email & phone.
Per month fixed fee for minimum 20 hours/month.
                                            [UnitAmount] => 500.00
                                            [TaxType] => OUTPUT
                                            [TaxAmount] => 50.00
                                            [LineAmount] => 500.00
                                            [AccountCode] => 200
                                            [Quantity] => 1.0000
                                            [LineItemID] => 46963aaf-cfb4-4825-aefc-98fdb720bb60
                                        )

                                )

                            [SubTotal] => 500.00
                            [TotalTax] => 50.00
                            [Total] => 550.00
                            [UpdatedDateUTC] => 2017-03-14T22:00:23.857
                            [CurrencyCode] => AUD
                            [Type] => ACCREC
                            [InvoiceID] => f9d3fa27-4ce6-44a3-be62-afb9baced1a5
                            [InvoiceNumber] => IV1947
                            [AmountDue] => 550.00
                            [AmountPaid] => 0.00
                            [AmountCredited] => 0.00
                            [SentToContact] => false
                            [CurrencyRate] => 1.000000
                            [HasAttachments] => false
                        )

                    [4] => SimpleXMLElement Object
                        (
                            [Contact] => SimpleXMLElement Object
                                (
                                    [ContactID] => e0413ee8-7b41-40e9-99c4-dd6b47c65564
                                    [ContactNumber] => CT1686
                                    [Name] => Acme Corp Inc
                                )

                            [Date] => 2017-03-15T00:00:00
                            [DueDate] => 2017-03-29T00:00:00
                            [BrandingThemeID] => 2ced98b8-3be9-42c4-ae79-fe3c8bca3490
                            [Status] => AUTHORISED
                            [LineAmountTypes] => Exclusive
                            [LineItems] => SimpleXMLElement Object
                                (
                                    [LineItem] => Array
                                        (
                                            [0] => SimpleXMLElement Object
                                                (
                                                    [ItemCode] => Support-M
                                                    [Description] => Desktop/network support via email & phone.
Per month fixed fee for minimum 20 hours/month.
                                                    [UnitAmount] => 500.00
                                                    [TaxType] => OUTPUT
                                                    [TaxAmount] => 150.00
                                                    [LineAmount] => 1500.00
                                                    [AccountCode] => 200
                                                    [Quantity] => 3.0000
                                                    [LineItemID] => a6464c91-f628-4060-8692-268bcd7a2d3d
                                                )

                                            [1] => SimpleXMLElement Object
                                                (
                                                    [ItemCode] => DevD
                                                    [Description] => Development work - developer onsite per day
                                                    [UnitAmount] => 650.00
                                                    [TaxType] => OUTPUT
                                                    [TaxAmount] => 325.00
                                                    [LineAmount] => 3250.00
                                                    [AccountCode] => 200
                                                    [Quantity] => 5.0000
                                                    [LineItemID] => 6ae30639-433f-40c9-8505-65f60f1fa0ff
                                                )

                                        )

                                )

                            [SubTotal] => 4750.00
                            [TotalTax] => 475.00
                            [Total] => 5225.00
                            [UpdatedDateUTC] => 2017-03-14T22:01:23.03
                            [CurrencyCode] => AUD
                            [Type] => ACCREC
                            [InvoiceID] => f00039cd-ad9f-403d-becc-d5508cca82ce
                            [InvoiceNumber] => IV1948
                            [AmountDue] => 5225.00
                            [AmountPaid] => 0.00
                            [AmountCredited] => 0.00
                            [SentToContact] => false
                            [CurrencyRate] => 1.000000
                            [HasAttachments] => false
                        )

                    [5] => SimpleXMLElement Object
                        (
                            [Contact] => SimpleXMLElement Object
                                (
                                    [ContactID] => 233265a4-d4e8-4bee-8110-1c5bb483283d
                                    [ContactNumber] => CT1598
                                    [Name] => JPV Gutters And Remodeling
                                )

                            [Date] => 2017-04-04T00:00:00
                            [DueDate] => 2017-04-18T00:00:00
                            [Status] => AUTHORISED
                            [LineAmountTypes] => Exclusive
                            [LineItems] => SimpleXMLElement Object
                                (
                                    [LineItem] => Array
                                        (
                                            [0] => SimpleXMLElement Object
                                                (
                                                    [ItemCode] => TSL - Black
                                                    [Description] => Company Branded T-Shirt Large Black.  Check out our website for other offers!
                                                    [UnitAmount] => 40.00
                                                    [TaxType] => OUTPUT
                                                    [TaxAmount] => 16.00
                                                    [LineAmount] => 160.00
                                                    [AccountCode] => 200
                                                    [Quantity] => 4.0000
                                                    [LineItemID] => 030bf68b-f75e-4280-b85f-c0de0946aeb9
                                                )

                                            [1] => SimpleXMLElement Object
                                                (
                                                    [ItemCode] => TSS - Black
                                                    [Description] => Company Branded T-Shirt Small Black.  Check out our website for other offers!
                                                    [UnitAmount] => 40.00
                                                    [TaxType] => OUTPUT
                                                    [TaxAmount] => 24.00
                                                    [LineAmount] => 240.00
                                                    [AccountCode] => 200
                                                    [Quantity] => 6.0000
                                                    [LineItemID] => 60783d08-a2dd-414a-b809-41a62e06b8d9
                                                )

                                        )

                                )

                            [SubTotal] => 400.00
                            [TotalTax] => 40.00
                            [Total] => 440.00
                            [UpdatedDateUTC] => 2017-04-03T22:58:04.85
                            [CurrencyCode] => AUD
                            [Type] => ACCREC
                            [InvoiceID] => 847a7ffa-7dfe-4de3-89d4-858731097875
                            [InvoiceNumber] => IV2023
                            [AmountDue] => 440.00
                            [AmountPaid] => 0.00
                            [AmountCredited] => 0.00
                            [SentToContact] => false
                            [CurrencyRate] => 1.000000
                            [HasAttachments] => false
                        )

                )

        )

)

I'm now trying to iterate through each of the returned Invoices and parse out some invoice data, starting with the InvoiceNumber. I'm using:

foreach ($invoices->Invoice as $invoice) {
    $invoiceNumber = $invoice["InvoiceNumber"];
    echo 'Invoice Number is: '.$invoiceNumber;

}   

but I'm not getting any output in the browser. This is my first time working with a SimpleXMLElement Object so not sure what I'm doing wrong here?

2 个答案:

答案 0 :(得分:1)

Your foreach statement needs to say

foreach ($invoices->Invoices->Invoice as $invoice) {
$invoiceNumber = $invoice["InvoiceNumber"];
echo 'Invoice Number is: '.$invoiceNumber;
}   

答案 1 :(得分:0)

首先,正如georgewoofbates所说,您在代码中缺少对<Invoices>节点的引用,因此您需要使用foreach ($invoices->Invoices->Invoice as $invoice) {...

其次,$invoice["InvoiceNumber"]会为您提供属性值,例如如果XML是<Invoice InvoiceNumber="42" />。但看起来你真正想要的是一个子元素,例如XML看起来像<Invoice><InvoiceNumber>42</InvoiceNumber></Invoice>,因此您需要使用$invoice->InvoiceNumber

有关详细信息,请参阅the examples in the manual