【问题标题】:JMS deserialize with XML List, xpath in SimpleXmlElement not workingJMS 使用 XML 列表反序列化,SimpleXmlElement 中的 xpath 不起作用
【发布时间】:2017-10-04 18:18:06
【问题描述】:

我正在尝试反序列化这个 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;
}

所有的东西都可以解析,但是带有 ObligationInformation 的 ArrayCollection 没有。即使使用array 类型,ArrayCollection 仍然为空。

尝试没有属性类别,使用不同版本的JMS/Serializer-bundle(当前我使用1.1.0 和jms/serializer 1.5.0)没有成功。

调试时我发现这里@9​​87654321@ 得到空数组。 $entityNameObligationInformation,就像这里的 http://sandbox.onlinephpfunctions.com/code/9f6213c5937f8e1cc78e46c710ae1a0f009338d5 在沙盒 PHP 中一样,但我在其中获取数据。

什么时候在我的应用程序中执行沙盒中的操作,结果与沙盒中的相同。但是在这个带有$data 的JMS 文件中,比如沙箱$xml,为什么我得到空数组?

【问题讨论】:

    标签: php xml symfony jmsserializerbundle jms-serializer


    【解决方案1】:

    您的问题是您在 &lt;InformationDisclosureResponse xmlns="http://ASD.pl"&gt; 中定义了错误的 XML 命名空间 元素。尝试在没有它的情况下进行反序列化,它应该可以解决问题。

    【讨论】:

    • 这个命名空间是故意改的,但是我忘记在课堂上改了。我使用了正确的命名空间,所以 Siadul 没有解决我的问题。
    【解决方案2】:

    我找到了这个问题的临时解决方案。

    在 JMS 标记 XmlList 中将 enrty 定义为 child::* 现在看起来像:

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

    而且它的工作原理!但它的工作..但仍然..

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-03-02
      • 1970-01-01
      • 2016-07-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多