IOS Swift使用SwiftyXMLParser解析XML数组时出错

时间:2016-09-05 04:48:02

标签: ios xml swift xml-parsing

您好我正在使用SwiftyXMLParser从服务器解析xml响应。我可以成功解析单个xml,但是数组值有问题。总是没事了

这是我的样本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>

我可以通过

获得单件商品
print(xml!["ItemTrackingDetailsResponse","ItemsTrackingDetailList","ItemTrackingDetail","ItemType"].text)

但是对于数组我无法获得细节。如何访问阵列成员?任何帮助都非常感激。感谢

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

1 个答案:

答案 0 :(得分:1)

您可以按如下方式访问Child Array内容:

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

"DeliveryStatusDetail",0表示您要访问第0个位置DeliveryStatusDetail元素。

枚举数组:

for DeliveryStatusDetail in xml["ItemTrackingDetailsResponse","ItemsTrackingDetailList","ItemTrackingDetail","DeliveryStatusDetails","DeliveryStatusDetail"] {
    print(DeliveryStatusDetail["Location"].text)
}