使用JMS序列化器的XML反序列化问题-我的嵌套对象为null

时间:2020-04-06 10:18:40

标签: xml serialization symfony-3.4 jms-serializer

在Symfony 3项目中使用JMS序列化器时遇到一些麻烦。 (我在ubuntu上工作) 当我使用以下代码行反序列化XML波纹管时:

$xmlContent = file_get_contents($filePath);
$order = $this->getContainer()->get('jms_serializer')->deserialize($xmlContent, 'AppBundle\xml\ORDER', 'xml');

我得到了这个对象(这是我的$ order对象的var_dump的结果):

object(AppBundle\xml\ORDER)#647 (5) {
  ["version":"AppBundle\xml\ORDER":private]=>
  string(3) "1.0"
  ["type":"AppBundle\xml\ORDER":private]=>
  string(8) "standard"
  ["oRDERHEADER":"AppBundle\xml\ORDER":private]=>
  NULL
  ["oRDERITEMLIST":"AppBundle\xml\ORDER":private]=>
  array(0) {
  }
  ["oRDERSUMMARY":"AppBundle\xml\ORDER":private]=>
  NULL
}

OrderHeader,OrderItemList和OrderSummary为NULL,而XML则不应该。

XML内容(在文件$ filePath中):

<?xml version="1.0" encoding="utf-8"?>
<ORDER version="1.0" type="standard" xlmns="http://www.opentrans.org/XMLSchema/1.0">
    <ORDER_HEADER>
        <CONTROL_INFO>
            <GENERATOR_INFO>created</GENERATOR_INFO>
            <GENERATION_DATE>2020-01-08T15:08:04+01:00</GENERATION_DATE>
        </CONTROL_INFO>
        <ORDER_INFO>
            <ORDER_ID>20-165686</ORDER_ID>
            <ORDER_DATE>2020-01-08T15:06:51+01:00</ORDER_DATE>
            <ORDER_PARTIES>
                <BUYER_PARTY>
                    <PARTY>
                        <ADDRESS>
                            <NAME>
                                <![CDATA[Lefrancais]]>
                            </NAME>
                            <NAME2>
                                <![CDATA[François]]>
                            </NAME2>
                            <NAME3>
                                <![CDATA[Rue Anselme]]>
                            </NAME3>
                            <STREET>
                                <![CDATA[Anselme]]>
                            </STREET>
                            <STREETNUMBER>
                                <![CDATA[12]]>
                            </STREETNUMBER>
                            <STREETAFFIX>
                                <![CDATA[]]>
                            </STREETAFFIX>
                            <ZIP>
                                <![CDATA[93400]]>
                            </ZIP>
                            <CITY>
                                <![CDATA[Saint-Ouen]]>
                            </CITY>
                            <COUNTRY>
                                <![CDATA[FR]]>
                            </COUNTRY>
                            <PHONE type="private">
                                <![CDATA[0411442255]]>
                            </PHONE>
                            <EMAIL>
                                <![CDATA[francois@gmail.com]]>
                            </EMAIL>
                        </ADDRESS>
                    </PARTY>
                </BUYER_PARTY>
                <INVOICE_PARTY>
                    <PARTY>
                        <ADDRESS>
                            <NAME>
                                <![CDATA[Lefrancais]]>
                            </NAME>
                            <NAME2>
                                <![CDATA[François]]>
                            </NAME2>
                            <NAME3>
                                <![CDATA[Rue Anselme]]>
                            </NAME3>
                            <STREET>
                                <![CDATA[Anselme]]>
                            </STREET>
                            <STREETNUMBER>
                                <![CDATA[12]]>
                            </STREETNUMBER>
                            <STREETAFFIX>
                                <![CDATA[]]>
                            </STREETAFFIX>
                            <ZIP>
                                <![CDATA[94555]]>
                            </ZIP>
                            <CITY>
                                <![CDATA[Saint-gilles]]>
                            </CITY>
                            <COUNTRY>
                                <![CDATA[FR]]>
                            </COUNTRY>
                            <PHONE type="private">
                                <![CDATA[0444112255]]>
                            </PHONE>
                            <EMAIL>
                                <![CDATA[francois@gmail.com]]>
                            </EMAIL>
                        </ADDRESS>
                    </PARTY>
                </INVOICE_PARTY>
                <SHIPMENT_PARTIES>
                    <DELIVERY_PARTY>
                        <PARTY>
                            <ADDRESS>
                                <NAME>
                                    <![CDATA[Lefrancais]]>
                                </NAME>
                                <NAME2>
                                    <![CDATA[François]]>
                                </NAME2>
                                <NAME3>
                                    <![CDATA[Rue Anselme]]>
                                </NAME3>
                                <STREET>
                                    <![CDATA[Anselme]]>
                                </STREET>
                                <STREETNUMBER>
                                    <![CDATA[12]]>
                                </STREETNUMBER>
                                <STREETAFFIX>
                                    <![CDATA[]]>
                                </STREETAFFIX>
                                <ZIP>
                                    <![CDATA[93555]]>
                                </ZIP>
                                <CITY>
                                    <![CDATA[Saint-gilles]]>
                                </CITY>
                                <COUNTRY>
                                    <![CDATA[FR]]>
                                </COUNTRY>
                                <PHONE type="private">
                                    <![CDATA[0411223366]]>
                                </PHONE>
                                <EMAIL>
                                    <![CDATA[francois@gmail.com]]>
                                </EMAIL>
                            </ADDRESS>
                        </PARTY>
                    </DELIVERY_PARTY>
                </SHIPMENT_PARTIES>
            </ORDER_PARTIES>
            <PRICE_CURRENCY>EUR</PRICE_CURRENCY>
        </ORDER_INFO>
        <ORDER_FEATURES>
            <FEATURE>
                <FNAME>
                    <![CDATA[PaymentType]]>
                </FNAME>
                <FVALUE>
                    <![CDATA[ADV]]>
                </FVALUE>
            </FEATURE>
            <FEATURE>
                <FNAME>
                    <![CDATA[DeliveryType]]>
                </FNAME>
                <FVALUE>
                    <![CDATA[PAKET]]>
                </FVALUE>
            </FEATURE>
            <FEATURE>
                <FNAME>
                    <![CDATA[PaymentStatus]]>
                </FNAME>
                <FVALUE>
                    <![CDATA[UNKNOWN]]>
                </FVALUE>
            </FEATURE>
            <FEATURE>
                <FNAME>
                    <![CDATA[PaymentReference]]>
                </FNAME>
                <FVALUE>
                    <![CDATA[UNKNOWN]]>
                </FVALUE>
            </FEATURE>
        </ORDER_FEATURES>
    </ORDER_HEADER>
    <ORDER_ITEM_LIST>
        <ORDER_ITEM>
            <LINE_ITEM_ID>2537642</LINE_ITEM_ID>
            <ARTICLE_ID>
                <SUPPLIER_AID/>
                <DESCRIPTION_SHORT>
                    <![CDATA[Mann-Filter Filtre d'urée]]>
                </DESCRIPTION_SHORT>
                <MANUFACTURER_INFO>
                    <MANUFACTURER_NAME>
                        <![CDATA[Mann-Filter]]>
                    </MANUFACTURER_NAME>
                    <MANUFACTURER_AID>
                        <![CDATA[U100210]]>
                    </MANUFACTURER_AID>
                </MANUFACTURER_INFO>
            </ARTICLE_ID>
            <QUANTITY>1</QUANTITY>
            <ORDER_UNIT>C62</ORDER_UNIT>
            <ARTICLE_PRICE type="gros_list">
                <PRICE_AMOUNT>0.68</PRICE_AMOUNT>
                <PRICE_LINE_AMOUNT>0.68</PRICE_LINE_AMOUNT>
                <TAX>0.19</TAX>
                <TAX_AMOUNT>0.10857142857143</TAX_AMOUNT>
            </ARTICLE_PRICE>
        </ORDER_ITEM>
        <ORDER_ITEM>
            <LINE_ITEM_ID>shipping</LINE_ITEM_ID>
            <ARTICLE_ID>
                <SUPPLIER_AID>-1</SUPPLIER_AID>
                <DESCRIPTION_SHORT>
                    <![CDATA[Lieferkosten (Paketdienst)]]>
                </DESCRIPTION_SHORT>
            </ARTICLE_ID>
            <QUANTITY>1</QUANTITY>
            <ORDER_UNIT>C62</ORDER_UNIT>
            <ARTICLE_PRICE type="gros_list">
                <PRICE_AMOUNT>0.01</PRICE_AMOUNT>
                <PRICE_LINE_AMOUNT>0.01</PRICE_LINE_AMOUNT>
                <TAX>0.19</TAX>
                <TAX_AMOUNT>0.0015966386554622</TAX_AMOUNT>
            </ARTICLE_PRICE>
        </ORDER_ITEM>
        <ORDER_ITEM>
            <LINE_ITEM_ID>payment</LINE_ITEM_ID>
            <ARTICLE_ID>
                <SUPPLIER_AID>-2</SUPPLIER_AID>
                <DESCRIPTION_SHORT>
                    <![CDATA[Zahlungskosten (Vorkasse per Überweisung)]]>
                </DESCRIPTION_SHORT>
            </ARTICLE_ID>
            <QUANTITY>1</QUANTITY>
            <ORDER_UNIT>C62</ORDER_UNIT>
            <ARTICLE_PRICE type="gros_list">
                <PRICE_AMOUNT>0</PRICE_AMOUNT>
                <PRICE_LINE_AMOUNT>0</PRICE_LINE_AMOUNT>
                <TAX>0</TAX>
                <TAX_AMOUNT>0.00</TAX_AMOUNT>
            </ARTICLE_PRICE>
        </ORDER_ITEM>
    </ORDER_ITEM_LIST>
    <ORDER_SUMMARY>
        <TOTAL_ITEM_NUM>3</TOTAL_ITEM_NUM>
        <TOTAL_AMOUNT>0.57983193277311</TOTAL_AMOUNT>
    </ORDER_SUMMARY>
</ORDER>

我指定使用此捆绑包从xsd生成PHP类和YML文件:https://github.com/goetas-webservices/xsd2php

我的 PHP类的相应位置位于: / var / www / symfony / tn / src / AppBundle / xml YML文件位于此处: / var / www / symfony / tn / src / AppBundle / xml / metadata

我为您提供了一个PHP类和此处生成的YML的示例:

/var/www/symfony/tn/src/AppBundle/xml/ORDER.php的内容

<?php

namespace AppBundle\xml;

/**
 * Class representing ORDER
 */
class ORDER
{

    /**
     * @var string $version
     */
    private $version = null;

    /**
     * @var string $type
     */
    private $type = null;

    /**
     * @var \AppBundle\xml\ORDERHEADER $oRDERHEADER
     */
    private $oRDERHEADER = null;

    /**
     * @var \AppBundle\xml\ORDERITEM[] $oRDERITEMLIST
     */
    private $oRDERITEMLIST = null;

    /**
     * @var \AppBundle\xml\ORDERSUMMARY $oRDERSUMMARY
     */
    private $oRDERSUMMARY = null;

    /**
     * Gets as version
     *
     * @return string
     */
    public function getVersion()
    {
        return $this->version;
    }

    /**
     * Sets a new version
     *
     * @param string $version
     * @return self
     */
    public function setVersion($version)
    {
        $this->version = $version;
        return $this;
    }

    /**
     * Gets as type
     *
     * @return string
     */
    public function getType()
    {
        return $this->type;
    }

    /**
     * Sets a new type
     *
     * @param string $type
     * @return self
     */
    public function setType($type)
    {
        $this->type = $type;
        return $this;
    }

    /**
     * Gets as oRDERHEADER
     *
     * @return \AppBundle\xml\ORDERHEADER
     */
    public function getORDERHEADER()
    {
        return $this->oRDERHEADER;
    }

    /**
     * Sets a new oRDERHEADER
     *
     * @param \AppBundle\xml\ORDERHEADER $oRDERHEADER
     * @return self
     */
    public function setORDERHEADER(\AppBundle\xml\ORDERHEADER $oRDERHEADER)
    {
        $this->oRDERHEADER = $oRDERHEADER;
        return $this;
    }

    /**
     * Adds as oRDERITEM
     *
     * @return self
     * @param \AppBundle\xml\ORDERITEM $oRDERITEM
     */
    public function addToORDERITEMLIST(\AppBundle\xml\ORDERITEM $oRDERITEM)
    {
        $this->oRDERITEMLIST[] = $oRDERITEM;
        return $this;
    }

    /**
     * isset oRDERITEMLIST
     *
     * @param int|string $index
     * @return bool
     */
    public function issetORDERITEMLIST($index)
    {
        return isset($this->oRDERITEMLIST[$index]);
    }

    /**
     * unset oRDERITEMLIST
     *
     * @param int|string $index
     * @return void
     */
    public function unsetORDERITEMLIST($index)
    {
        unset($this->oRDERITEMLIST[$index]);
    }

    /**
     * Gets as oRDERITEMLIST
     *
     * @return \AppBundle\xml\ORDERITEM[]
     */
    public function getORDERITEMLIST()
    {
        return $this->oRDERITEMLIST;
    }

    /**
     * Sets a new oRDERITEMLIST
     *
     * @param \AppBundle\xml\ORDERITEM[] $oRDERITEMLIST
     * @return self
     */
    public function setORDERITEMLIST(array $oRDERITEMLIST)
    {
        $this->oRDERITEMLIST = $oRDERITEMLIST;
        return $this;
    }

    /**
     * Gets as oRDERSUMMARY
     *
     * @return \AppBundle\xml\ORDERSUMMARY
     */
    public function getORDERSUMMARY()
    {
        return $this->oRDERSUMMARY;
    }

    /**
     * Sets a new oRDERSUMMARY
     *
     * @param \AppBundle\xml\ORDERSUMMARY $oRDERSUMMARY
     * @return self
     */
    public function setORDERSUMMARY(\AppBundle\xml\ORDERSUMMARY $oRDERSUMMARY)
    {
        $this->oRDERSUMMARY = $oRDERSUMMARY;
        return $this;
    }


}

/var/www/symfony/tn/src/AppBundle/xml/metadata/ORDER.yml的内容

AppBundle\xml\ORDER:
    xml_root_name: ORDER
    xml_root_namespace: 'http://www.opentrans.org/XMLSchema/1.0'
    properties:
        version:
            expose: false
            access_type: public_method
            serialized_name: version
            accessor:
                getter: getVersion
                setter: setVersion
            xml_attribute: true
            type: string
        type:
            expose: true
            access_type: public_method
            serialized_name: type
            accessor:
                getter: getType
                setter: setType
            xml_attribute: true
            type: string
        oRDERHEADER:
            expose: true
            access_type: public_method
            serialized_name: ORDER_HEADER
            xml_element:
                namespace: 'http://www.opentrans.org/XMLSchema/1.0'
            accessor:
                getter: getORDERHEADER
                setter: setORDERHEADER
            type: AppBundle\xml\ORDERHEADER
        oRDERITEMLIST:
            expose: true
            access_type: public_method
            serialized_name: ORDER_ITEM_LIST
            xml_element:
                namespace: 'http://www.opentrans.org/XMLSchema/1.0'
            accessor:
                getter: getORDERITEMLIST
                setter: setORDERITEMLIST
            type: array<AppBundle\xml\ORDERITEM>
            xml_list:
                inline: false
                entry_name: ORDER_ITEM
                skip_when_empty: false
                namespace: 'http://www.opentrans.org/XMLSchema/1.0'
        oRDERSUMMARY:
            expose: true
            access_type: public_method
            serialized_name: ORDER_SUMMARY
            xml_element:
                namespace: 'http://www.opentrans.org/XMLSchema/1.0'
            accessor:
                getter: getORDERSUMMARY
                setter: setORDERSUMMARY
            type: AppBundle\xml\ORDERSUMMARY

最后,我将 /var/www/symfony/tn/app/config/config.yml 文件的摘录粘贴到此处,与jms序列化程序配置相对应:

jms_serializer:
    handlers:
        datetime:
            default_format: "Y-m-d\\TH:i:sP" # ATOM
            default_timezone: "UTC" # defaults to whatever timezone set in php.ini or via date_default_timezone_set
        array_collection:
            initialize_excluded: false
    metadata:
        directories:
            AppBundle:
                namespace_prefix: "AppBundle\\xml"
                path: "%kernel.project_dir%/src/AppBundle/xml/metadata"

我认为我描述PHP类的YML文件未加载。为了进行检查,我更新了我的ORDER.yml文件,并将version属性暴露设置为false(请参见下面的波纹管代码),然后在$ order对象的var_dump中继续暴露“版本”字段,如您所见。 / p>

AppBundle\xml\ORDER:
    xml_root_name: ORDER
    xml_root_namespace: 'http://www.opentrans.org/XMLSchema/1.0'
    properties:
        version:
            expose: false
            access_type: public_method
            serialized_name: version
            accessor:
                getter: getVersion
                setter: setVersion
            xml_attribute: true
            type: string

我指定我显然对Web和JMS序列化器文档进行了大量研究,但我仍然坚持:s

非常感谢您抽出宝贵的时间阅读这篇文章并为我提供帮助!

0 个答案:

没有答案