Десериализация JMS со списком XML, xpath в SimpleXmlElement не работает

Я пытаюсь десериализовать этот XML

<s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <InformationDisclosureResponse xmlns="http://ASD.pl">
        <DisclosureReport>
            <Number>U/123</Number>
            <Created>2017-10-03T15:00:50+02:00</Created>
            <SearchCriterion>
                <NonConsumerIdentityNumber>
                    <TaxId>123123123</TaxId>
                </NonConsumerIdentityNumber>
                <AuthorizationDateSpecified>false</AuthorizationDateSpecified>
                <ActAuthorizationDateSpecified>false</ActAuthorizationDateSpecified>
                <SearchType>RegularSearch</SearchType>
                <SearchCriterionType>TaxId</SearchCriterionType>
            </SearchCriterion>
            <Requester>
                <LegalPerson>
                    <Name>Test</Name>
                    <IdentityNumber>
                        <TaxId>123123123</TaxId>
                    </IdentityNumber>
                    <SeatAddress>
                        <Line>asd 72</Line>
                    </SeatAddress>
                </LegalPerson>
                <LoginFullName>ASD</LoginFullName>
            </Requester>
            <Summary>
                <InformationCount>2</InformationCount>
                <TotalArrears>
                    <Amount>123321.52</Amount>
                    <Currency>PLN</Currency>
                </TotalArrears>
            </Summary>
            <PositiveInformationSummary>
                <ProvidersCount>0</ProvidersCount>
            </PositiveInformationSummary>
            <Report>
                <ObligationInformations>
                    <ObligationInformation category="0">
                        <Debtor>
                            <Entrepreneur>
                                <Name>ASDASD</Name>
                                <AddressForMail>
                                    <Line>ASD ASD</Line>
                                </AddressForMail>
                                <SeatAddress>
                                    <Line>ASD123</Line>
                                </SeatAddress>
                                <NonConsumerIdentityNumber>
                                    <TaxId>123123123</TaxId>
                                </NonConsumerIdentityNumber>
                                <FirstName>ASD</FirstName>
                                <Surname>ASD</Surname>
                            </Entrepreneur>
                        </Debtor>
                        <Provider>
                            <LegalPerson>
                                <Name>ASD</Name>
                                <IdentityNumber>
                                    <TaxId>123123123</TaxId>
                                </IdentityNumber>
                                <SeatAddress>
                                    <Line>Asd, asd</Line>
                                </SeatAddress>
                                <RegistrationNumber>123123</RegistrationNumber>
                                <RegistryName>ASD</RegistryName>
                                <Regon>123123</Regon>
                                <Ekd>123S/07</Ekd>
                            </LegalPerson>
                        </Provider>
                        <Title>Fd-dasd</Title>
                        <Type>Invoice</Type>
                        <PaymentDate>2017-04-20T00:00:00+02:00</PaymentDate>
                        <NoObjections>true</NoObjections>
                        <CallSent>2017-05-29T00:00:00+02:00</CallSent>
                        <Debt>
                            <Amount>123.80</Amount>
                            <Currency>PLN</Currency>
                        </Debt>
                        <Arrears>
                            <Amount>321.80</Amount>
                            <Currency>PLN</Currency>
                        </Arrears>
                    </ObligationInformation>
                </ObligationInformations>
            </Report>
        </DisclosureReport>
    </InformationDisclosureResponse>
</s:Body>

С классом

    class ReportResponse{
/**
 * @var DisclosureReport
 *
 * @JMS\Type("DisclosureReport")
 * @JMS\SerializedName("DisclosureReport")
 */
public $disclosureReport;
//geter
}

.

class DisclosureReport{
/**
 * @var string|null
 *
 * @JMS\Type("string")
 * @JMS\SerializedName("Number")
 */
public $number;

/**
 * @var string|null
 *
 * @JMS\Type("string")
 * @JMS\SerializedName("Created")
 */
public $created;

/**
 * @var SearchCriterion|null
 *
 * @JMS\Type("SearchCriterion")
 * @JMS\SerializedName("SearchCriterion")
 */
public $searchCriterion;

/**
 * @var Requester|null
 *
 * @JMS\Type("Requester")
 * @JMS\SerializedName("Requester")
 */
public $requester;

/**
 * @var Summary|null
 *
 * @JMS\Type("Summary")
 * @JMS\SerializedName("Summary")
 */
public $summary;

/**
 * @var PositiveInformationSummary|null
 *
 * @JMS\Type("PositiveInformationSummary")
 * @JMS\SerializedName("PositiveInformationSummary")
 */
public $positiveInformationSummary;

/**
 * @var Report|null
 *
 * @JMS\Type("Report")
 * @JMS\SerializedName("Report")
 */
public $report;
//geters
}

.

class Report{
/**
 * @var ArrayCollection|null
 *
 * @JMS\Type("ArrayCollection<ObligationInformation>")
 * @JMS\SerializedName("ObligationInformations")
 * @JMS\XmlList(entry="ObligationInformation")
 */
public $obligationInformations;

/**
 * @var ArrayCollection|null
 *
 * @JMS\Type("ArrayCollection<PaidObligationInformation>")
 * @JMS\SerializedName("PaidObligationInformations")
 * @JMS\XmlList(entry="PaidObligationInformation")
 */
public $paidObligationInformations;
}

И все анализируется нормально, но ArrayCollection с ObligationInformation - нет. Все еще получаю пустую коллекцию ArrayCollection, даже с типом array.

Попытка без категории атрибутов с другой версией JMS/Serializer-bundle (в настоящее время я использую 1.1.0 с jms/serializer 1.5.0) не увенчалась успехом.

При отладке я обнаружил, что здесь https://github.com/schmittjoh/serializer/blob/1.5.0/src/JMS/Serializer/XmlDeserializationVisitor.php#L158 Я получаю пустой массив. $entityName равно ObligationInformation, как здесь http://sandbox.onlinephpfunctions.com/code/9f6210c5937f8a1cc78e46c710 песочнице PHP, но в ней я получаю данные.

Когда делаю то, что находится в песочнице в моем приложении, результат такой же, как и в песочнице. Но в этом файле JMS с $data, как в песочнице $xml, я получаю пустой массив, почему?


person Bartłomiej Zając    schedule 04.10.2017    source источник


Ответы (2)


Ваша проблема в том, что у вас есть неправильное пространство имен XML, определенное в элементе <InformationDisclosureResponse xmlns="http://ASD.pl">. Попробуйте десериализовать без него, и это должно помочь.

person Krzysztof Siadul    schedule 05.10.2017
comment
Это пространство имен было изменено намеренно, но я забыл изменить его в классе. Я использую правильное пространство имен, так что это не решило мою проблему с Сиадулом. - person Bartłomiej Zając; 05.10.2017

Я нашел временное решение этой проблемы.

В теге JMS XmlList определите запись как child::*, теперь это выглядит так:

@JMS\XmlList(entry="child::*")

И это работает! Но это обходной путь.. Но все же..

person Bartłomiej Zając    schedule 06.10.2017