0

Hi I'm parsing xml response from server using SwiftyXMLParser. I'm can successfully parse single xml but have problem with array value. always got nil

here is my sample xml

<ItemTrackingDetailsResponse xmlns="http://singpost.com/paw/ns" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
    <ItemsTrackingDetailList>
        <ItemTrackingDetail>
            <TrackingNumber>SMT0000000628</TrackingNumber>
            <TrackingNumberFound>true</TrackingNumberFound>
            <AlternativeTrackingNumber/>
            <TrackingNumberActive>true</TrackingNumberActive>
            <PostingDate/>
            <InsuredValue/>
            <InsuredSDR/>
            <Weight/>
            <ExpressInd/>
            <ItemCategory/>
            <Content/>
            <PreadviceDate/>
            <ReceptacleID/>
            <OriginalCountry>Singapore</OriginalCountry>
            <DestinationCountry>Singapore</DestinationCountry>
            <ItemType>Speedpost</ItemType>
            <DeliveryStatusDetails>
                <DeliveryStatusDetail>
                    <StatusDescription>Recovered item from POPStation for service recovery</StatusDescription>
                    <Location>SPEEDPOST SERVICE CENTRE</Location>
                    <BeatNo/>
                    <Date>2016-08-22T16:35:30</Date>
                    <AceDate>20160822 163530</AceDate>
                </DeliveryStatusDetail>
                <DeliveryStatusDetail>
                    <StatusDescription>Delivered to POPStation</StatusDescription>
                    <Location>POPStation@Yew Tee Square</Location>
                    <BeatNo/>
                    <Date>2016-08-16T17:27:48</Date>
                    <AceDate>20160816 172748</AceDate>
                </DeliveryStatusDetail>
                <DeliveryStatusDetail>
                    <StatusDescription>With Delivery Courier</StatusDescription>
                    <Location>SPEEDPOST SERVICE CENTRE</Location>
                    <BeatNo/>
                    <Date>2016-08-16T11:25:00</Date>
                    <AceDate>20160816 112500</AceDate>
                </DeliveryStatusDetail>
                <DeliveryStatusDetail>
                    <StatusDescription>Received at Processing Facility</StatusDescription>
                    <Location>SPEEDPOST SERVICE CENTRE</Location>
                    <BeatNo/>
                    <Date>2016-08-16T10:28:07</Date>
                    <AceDate>20160816 102807</AceDate>
                </DeliveryStatusDetail>
                <DeliveryStatusDetail>
                    <StatusDescription>Notification of shipment confirmation</StatusDescription>
                    <Location>SPEEDPOST SERVICE CENTRE</Location>
                    <BeatNo/>
                    <Date>2016-08-13T11:10:29</Date>
                    <AceDate>20160813 111029</AceDate>
                </DeliveryStatusDetail>
            </DeliveryStatusDetails>
        </ItemTrackingDetail>
    </ItemsTrackingDetailList>
    <Status>
        <ErrorCode>0</ErrorCode>
        <ErrorDesc>Success</ErrorDesc>
    </Status>
</ItemTrackingDetailsResponse>

I can get single item by

print(xml!["ItemTrackingDetailsResponse","ItemsTrackingDetailList","ItemTrackingDetail","ItemType"].text)

but for the array I can not get the detail. How to access array member? Any help is much appreciate. Thanks

    <DeliveryStatusDetails>
                    <DeliveryStatusDetail>
                        <StatusDescription>Recovered item from POPStation for service recovery</StatusDescription>
                        <Location>SPEEDPOST SERVICE CENTRE</Location>
                        <BeatNo/>
                        <Date>2016-08-22T16:35:30</Date>
                        <AceDate>20160822 163530</AceDate>
                    </DeliveryStatusDetail>
                    <DeliveryStatusDetail>
                        <StatusDescription>Delivered to POPStation</StatusDescription>
                        <Location>POPStation@Yew Tee Square</Location>
                        <BeatNo/>
                        <Date>2016-08-16T17:27:48</Date>
                        <AceDate>20160816 172748</AceDate>
                    </DeliveryStatusDetail>
                    <DeliveryStatusDetail>
                        <StatusDescription>With Delivery Courier</StatusDescription>
                        <Location>SPEEDPOST SERVICE CENTRE</Location>
                        <BeatNo/>
                        <Date>2016-08-16T11:25:00</Date>
                        <AceDate>20160816 112500</AceDate>
                    </DeliveryStatusDetail>
                    <DeliveryStatusDetail>
                        <StatusDescription>Received at Processing Facility</StatusDescription>
                        <Location>SPEEDPOST SERVICE CENTRE</Location>
                        <BeatNo/>
                        <Date>2016-08-16T10:28:07</Date>
                        <AceDate>20160816 102807</AceDate>
                    </DeliveryStatusDetail>
                    <DeliveryStatusDetail>
                        <StatusDescription>Notification of shipment confirmation</StatusDescription>
                        <Location>SPEEDPOST SERVICE CENTRE</Location>
                        <BeatNo/>
                        <Date>2016-08-13T11:10:29</Date>
                        <AceDate>20160813 111029</AceDate>
                    </DeliveryStatusDetail>
</DeliveryStatusDetails>
0

1 Answer 1

1

You can access the Child Array content as follows:

print(xml["ItemTrackingDetailsResponse","ItemsTrackingDetailList","ItemTrackingDetail","DeliveryStatusDetails","DeliveryStatusDetail",0,"Location"].text)

The "DeliveryStatusDetail",0 denotes that you want to access the 0th position DeliveryStatusDetail element.

To enumerate through the array:

for DeliveryStatusDetail in xml["ItemTrackingDetailsResponse","ItemsTrackingDetailList","ItemTrackingDetail","DeliveryStatusDetails","DeliveryStatusDetail"] {
    print(DeliveryStatusDetail["Location"].text)
}
Sign up to request clarification or add additional context in comments.

4 Comments

work perfectly. Thanks a lot!. But how can we loop through the array?
thanks, it easier to work with XML now. Anyway we can print the whole xml (to test out whether response is correct)?
like a whole xml as a string so we can see what is inside!
Yes, use this code: let datastring = String.init(data: xmlData, encoding: NSUTF8StringEncoding)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.