Groovy - Serializing XML NodeChildren

时间:2018-12-03 13:18:50

标签: magento groovy

I am new to groovy and i'm trying to serialize a specific part of an XML like this (which is also a SalesOrderInfo response from Magento web service)

    <SOAP-ENV:Envelope SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="urn:Magento" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/">
   <SOAP-ENV:Body>
      <ns1:salesOrderInfoResponse>
         <result xsi:type="ns1:salesOrderEntity">
            <increment_id xsi:type="xsd:string">REDACTED</increment_id>
            <store_id xsi:type="xsd:string">1</store_id>
            <created_at xsi:type="xsd:string">2018-11-21 14:26:08</created_at>
            <updated_at xsi:type="xsd:string">2018-11-24 17:40:18</updated_at>
            <customer_id xsi:type="xsd:string">111111</customer_id>
            <tax_amount xsi:type="xsd:string">37.9830</tax_amount>
            <shipping_amount xsi:type="xsd:string">0.0000</shipping_amount>
            <discount_amount xsi:type="xsd:string">0.0000</discount_amount>
            <subtotal xsi:type="xsd:string">211.0170</subtotal>
            <grand_total xsi:type="xsd:string">249.0000</grand_total>
            <total_paid xsi:type="xsd:string">249.0000</total_paid>
            <total_qty_ordered xsi:type="xsd:string">2.0000</total_qty_ordered>
            <total_invoiced xsi:type="xsd:string">249.0000</total_invoiced>
            <base_tax_amount xsi:type="xsd:string">37.9830</base_tax_amount>
            <base_shipping_amount xsi:type="xsd:string">0.0000</base_shipping_amount>
            <base_discount_amount xsi:type="xsd:string">0.0000</base_discount_amount>
            <base_subtotal xsi:type="xsd:string">211.0170</base_subtotal>
            <base_grand_total xsi:type="xsd:string">249.0000</base_grand_total>
            <base_total_paid xsi:type="xsd:string">249.0000</base_total_paid>
            <base_total_invoiced xsi:type="xsd:string">249.0000</base_total_invoiced>
            <billing_address_id xsi:type="xsd:string">126249</billing_address_id>
            <shipping_address_id xsi:type="xsd:string">126250</shipping_address_id>
            <store_to_base_rate xsi:type="xsd:string">1.0000</store_to_base_rate>
            <store_to_order_rate xsi:type="xsd:string">1.0000</store_to_order_rate>
            <base_to_global_rate xsi:type="xsd:string">1.0000</base_to_global_rate>
            <base_to_order_rate xsi:type="xsd:string">1.0000</base_to_order_rate>
            <weight xsi:type="xsd:string">0.0000</weight>
            <store_name xsi:type="xsd:string">Main Website
Main Website Store
Default Store View</store_name>
            <remote_ip xsi:type="xsd:string">REDACTED</remote_ip>
            <status xsi:type="xsd:string">complete_delivered</status>
            <state xsi:type="xsd:string">complete</state>
            <applied_rule_ids xsi:type="xsd:string">6</applied_rule_ids>
            <global_currency_code xsi:type="xsd:string">TRY</global_currency_code>
            <base_currency_code xsi:type="xsd:string">TRY</base_currency_code>
            <store_currency_code xsi:type="xsd:string">TRY</store_currency_code>
            <order_currency_code xsi:type="xsd:string">TRY</order_currency_code>
            <shipping_method xsi:type="xsd:string">REDACTED</shipping_method>
            <shipping_description xsi:type="xsd:string">Standart Kargo</shipping_description>
            <customer_email xsi:type="xsd:string">REDACTED</customer_email>
            <customer_firstname xsi:type="xsd:string">REDACTED</customer_firstname>
            <customer_lastname xsi:type="xsd:string">REDACTED</customer_lastname>
            <quote_id xsi:type="xsd:string">263510</quote_id>
            <is_virtual xsi:type="xsd:string">0</is_virtual>
            <customer_group_id xsi:type="xsd:string">1</customer_group_id>
            <customer_note_notify xsi:type="xsd:string">1</customer_note_notify>
            <customer_is_guest xsi:type="xsd:string">0</customer_is_guest>
            <email_sent xsi:type="xsd:string">1</email_sent>
            <order_id xsi:type="xsd:string">111111</order_id>
            <status_history SOAP-ENC:arrayType="ns1:salesOrderStatusHistoryEntity[7]" xsi:type="ns1:salesOrderStatusHistoryEntityArray">
               <item xsi:type="ns1:salesOrderStatusHistoryEntity">
                  <parent_id xsi:type="xsd:string">63128</parent_id>
                  <created_at xsi:type="xsd:string">2018-11-24 17:40:18</created_at>
                  <is_customer_notified xsi:type="xsd:string">0</is_customer_notified>
                  <status xsi:type="xsd:string">complete_delivered</status>
                  <comment xsi:type="xsd:string">2018-11-24 20:40:18</comment>
               </item>
               <item xsi:type="ns1:salesOrderStatusHistoryEntity">
                  <parent_id xsi:type="xsd:string">63128</parent_id>
                  <created_at xsi:type="xsd:string">2018-11-23 12:40:34</created_at>
                  <is_customer_notified xsi:type="xsd:string">0</is_customer_notified>
                  <status xsi:type="xsd:string">complete_shipped</status>
                  <comment xsi:type="xsd:string">REDACTED</comment>
               </item>
               <item xsi:type="ns1:salesOrderStatusHistoryEntity">
                  <parent_id xsi:type="xsd:string">63128</parent_id>
                  <created_at xsi:type="xsd:string">2018-11-23 11:50:37</created_at>
                  <is_customer_notified xsi:type="xsd:string">0</is_customer_notified>
                  <status xsi:type="xsd:string">complete</status>
                  <comment xsi:type="xsd:string">2018-11-23 14:50:37</comment>
               </item>
               <item xsi:type="ns1:salesOrderStatusHistoryEntity">
                  <parent_id xsi:type="xsd:string">63128</parent_id>
                  <created_at xsi:type="xsd:string">2018-11-23 11:50:37</created_at>
                  <is_customer_notified xsi:type="xsd:string">1</is_customer_notified>
                  <status xsi:type="xsd:string">complete</status>
               </item>
               <item xsi:type="ns1:salesOrderStatusHistoryEntity">
                  <parent_id xsi:type="xsd:string">63128</parent_id>
                  <created_at xsi:type="xsd:string">2018-11-21 17:31:38</created_at>
                  <is_customer_notified xsi:type="xsd:string">0</is_customer_notified>
                  <status xsi:type="xsd:string">processing_started</status>
                  <comment xsi:type="xsd:string">2018-11-21 20:31:38</comment>
               </item>
               <item xsi:type="ns1:salesOrderStatusHistoryEntity">
                  <parent_id xsi:type="xsd:string">63128</parent_id>
                  <created_at xsi:type="xsd:string">2018-11-21 14:26:11</created_at>
                  <is_customer_notified xsi:type="xsd:string">1</is_customer_notified>
                  <status xsi:type="xsd:string">waiting_shipment</status>
                  <comment xsi:type="xsd:string">REDACTED</comment>
               </item>
               <item xsi:type="ns1:salesOrderStatusHistoryEntity">
                  <parent_id xsi:type="xsd:string">63128</parent_id>
                  <created_at xsi:type="xsd:string">2018-11-21 14:26:08</created_at>
                  <is_customer_notified xsi:type="xsd:string">1</is_customer_notified>
                  <status xsi:type="xsd:string">pending</status>
               </item>
            </status_history>
         </result>
      </ns1:salesOrderInfoResponse>
   </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

What i want to achieve is i just want to extract "status_history" list from the XML and serialize it. So the output i expect should look like this.

<status_history SOAP-ENC:arrayType="ns1:salesOrderStatusHistoryEntity[7]" xsi:type="ns1:salesOrderStatusHistoryEntityArray">
               <item xsi:type="ns1:salesOrderStatusHistoryEntity">
                  <parent_id xsi:type="xsd:string">63128</parent_id>
                  <created_at xsi:type="xsd:string">2018-11-24 17:40:18</created_at>
                  <is_customer_notified xsi:type="xsd:string">0</is_customer_notified>
                  <status xsi:type="xsd:string">complete_delivered</status>
                  <comment xsi:type="xsd:string">2018-11-24 20:40:18</comment>
               </item>
               <item xsi:type="ns1:salesOrderStatusHistoryEntity">
                  <parent_id xsi:type="xsd:string">63128</parent_id>
                  <created_at xsi:type="xsd:string">2018-11-23 12:40:34</created_at>
                  <is_customer_notified xsi:type="xsd:string">0</is_customer_notified>
                  <status xsi:type="xsd:string">complete_shipped</status>
                  <comment xsi:type="xsd:string">REDACTED</comment>
               </item>
               <item xsi:type="ns1:salesOrderStatusHistoryEntity">
                  <parent_id xsi:type="xsd:string">63128</parent_id>
                  <created_at xsi:type="xsd:string">2018-11-23 11:50:37</created_at>
                  <is_customer_notified xsi:type="xsd:string">0</is_customer_notified>
                  <status xsi:type="xsd:string">complete</status>
                  <comment xsi:type="xsd:string">2018-11-23 14:50:37</comment>
               </item>
               <item xsi:type="ns1:salesOrderStatusHistoryEntity">
                  <parent_id xsi:type="xsd:string">63128</parent_id>
                  <created_at xsi:type="xsd:string">2018-11-23 11:50:37</created_at>
                  <is_customer_notified xsi:type="xsd:string">1</is_customer_notified>
                  <status xsi:type="xsd:string">complete</status>
               </item>
               <item xsi:type="ns1:salesOrderStatusHistoryEntity">
                  <parent_id xsi:type="xsd:string">63128</parent_id>
                  <created_at xsi:type="xsd:string">2018-11-21 17:31:38</created_at>
                  <is_customer_notified xsi:type="xsd:string">0</is_customer_notified>
                  <status xsi:type="xsd:string">processing_started</status>
                  <comment xsi:type="xsd:string">2018-11-21 20:31:38</comment>
               </item>
               <item xsi:type="ns1:salesOrderStatusHistoryEntity">
                  <parent_id xsi:type="xsd:string">63128</parent_id>
                  <created_at xsi:type="xsd:string">2018-11-21 14:26:11</created_at>
                  <is_customer_notified xsi:type="xsd:string">1</is_customer_notified>
                  <status xsi:type="xsd:string">waiting_shipment</status>
                  <comment xsi:type="xsd:string">REDACTED</comment>
               </item>
               <item xsi:type="ns1:salesOrderStatusHistoryEntity">
                  <parent_id xsi:type="xsd:string">63128</parent_id>
                  <created_at xsi:type="xsd:string">2018-11-21 14:26:08</created_at>
                  <is_customer_notified xsi:type="xsd:string">1</is_customer_notified>
                  <status xsi:type="xsd:string">pending</status>
               </item>
            </status_history>

So the script i try to execute is looking like this

for( int i = 0; i < dataContext.getDataCount(); i++ ) {
    InputStream is = dataContext.getStream(i);
    Properties props = dataContext.getProperties(i);
    Logger logger = ExecutionUtil.getBaseLogger();
    def baseMessage = ExecutionUtil.getDynamicProcessProperty("p_crmLogMessage");
    def baseXML =  new XmlSlurper(false, false).parseText(baseMessage);
    def statusXML = baseXML.salesOrderInfoResponse.result.status_history;
    logger.info("Found Status History " + statusXML.toString());
    String sDummyXML = '''<magentoHistoryResponse></magentoHistoryResponse>''';
    def dummyXML = new XmlSlurper().parseText(sDummyXML);
    dummyXML.appendNode(statusXML.childNodes().next());
    logger.info("Parsed Status Text");
    logger.info("Status : " + XmlUtil.serialize(dummyXML));
    ExecutionUtil.setDynamicProcessProperty("p_crmLogMessage", XmlUtil.serialize(dummyXML), false);
    dataContext.storeStream(is, props);
}

What i found is that the status_history tag is type of NodeChildren and i tried to get a single child for testing purposes but i was unable to serialize the NodeChildren/NodeChild. How can this be achieved ? I use Groovy 2.4. I get the raw base bulk XML as String from this line

def baseMessage = ExecutionUtil.getDynamicProcessProperty("p_crmLogMessage");

0 个答案:

没有答案