Json转换为XML DData

时间:2018-02-06 09:24:53

标签: java json xml rest

INput Json:
<html>

    {username=XXXXXXXXXX
    &auth_string=YYYYYYYYY
    &created_from='2014-01-01 00:00:00' (optional)
    &created_to='2014-04-01 00:00:00'(optional)
    &modified_from='2014-01-01 00:00:00' (optional)
    &modified_to='2014-04-01 00:00:00'(optional)
    &fetched_from='2014-01-01 00:00:00' (optional, NOTE: This date represents the time at which the order was fetched into Browntape.)
    &fetched_to='2014-04-01 00:00:00' (optional, NOTE: This date represents the time at which the order was fetched into Browntape.)
    &channel_id[] = 3 (optional, multiple values accepted)
    &order_id[] = 12345 (optional, multiple values accepted)
    &fulfillment_status_id[]= courier assigned/delivered/cancelled/delivered/lost in transit/manifested/on hold/order accepted/outfordelivery/packed/packing/pending acceptance/pickedup
                            /pendingpickup/ready to ship/processing/reschedule pickup/return not expected/return initiated/rto return pending/rto return received/return received
                            /return pending/return requested/upcoming/waitingforpickup(optional , multiple values may be passed) 
    &financial_status_id= cod/declined/cancelled/paid/processing/refunded/reversed/unpaid(optional , multiple values may be passed)
    &order_reference=XYZ (optional)
    &order_reference2=PQR (optional)
    &warehouse_id=123 (optional)
    &convert_currency=1 (optional,DEFAULT = 0,  Note: When this setting is set to 1, all currency values in this api will be converted to the currency specified in browtape settings) 
    &search_query=abc (optional, Note: If this param is set, all other params are ignored)
    &page=1
    &limit = 200 (optional. Default - 200 , Max - 200)
********************/</html>

Response Json:
<html>
>{"success":"true",
 >  "result_count":1,
  > "results_per_page":20,
   "page":"1",
   "data":[  
      {  
         "Order":{  
            "id":"107391769",
            "is_cod":"0",
            "order_type":null,
            "created":"2015-06-02 00:00:00",
            "modified":"2015-06-03 17:20:39",
            "gross_value":"521",
            "fees_value":null,
            "channel_id":"1703",
            "parent_id":null,
            "shipping_value":"0",
            "customer_id":"81259975",
            "shipping_address_id":"2716909",
            "order_status_id":"2440737",
            "financial_status_id":"2",
            "fulfillment_status_id":"3",
            "marketplace_status": null,
            "shipping_batch_id":"22549",
            "manifest_id":null,
            "currency_id":"63",
            "executed_shipping_method":"DTDC",
            "tracking_number":"B19152417",
            "stock_adjusted":"1",
            "order_reference":"301151513",
            "selected_shipping_method":null,
            "uuid":"584770205556e897c666a270219",
            "unread":"1",
            "dispatch_date":"2015-06-03 11:50:23",
            "bt_fulfillment_status_id":"3",
            "bt_order_status_id":null,
            "bt_financial_status_id":null,
            "remittance_date":null,
            "remittance_note":null,
            "remitted_value":null,
            "invoice_prefix":null,
            "invoice_number":null,
            "courier_company_id":"9",
            "ship_label_path":null,
            "courier_status_id":null,
            "courier_status_raw":null,
            "courier_tracking_attempts":"0",
            "courier_tracked_on":null,
            "warehouse_id":"1",
            "tax_rule_id":null,
            "weight_gm":null,
            "length_mm":null,
            "breadth_mm":null,
            "height_mm":null,
            "problem_reason":null,
            "replacement_order_id":null,
            "replacement_pending":null,
            "created_on":null,
            "vat_forms":"",
            "marketplace_fulfilled":true,
            "is_cancellable_on_channel":false,
            "vatlinks": ""
         },
         "Channel":{  
            "id":"1703",
            "email":null,
            "email_from_name":null,
            "email_bcc":null,
            "invoice_template":null,
            "title":"pepper fry",
            "stock_sync":"1",
            "ChannelType":{  
               "favicon_path":"\/img\/icons\/icon_offline_store.png",
               "standard_tag":"offline_store"
            }
         },
         "Customer":{  
            "id":"81259975",
            "salutation":null,
            "first_name":"zubin mistry",
            "last_name":"",
            "email":null,
            "nickname":null,
            "created":"0000-00-00 00:00:00",
            "modified":"0000-00-00 00:00:00",
            "customer_reference":null,
            "channel_id":"1703",
            "full_name":"zubin mistry ",
            "company_name":null
         },
         "ShippingAddress":{  
            "id":"2716909",
            "address_line1":"202  new cd baug . next to city center shopping mall  in sai baba enclave compound",
            "address_line2":"Goregaon West",
            "address_line3":"",
            "city":"Mumbai",
            "state":"Maharashtra",
            "country":"India",
            "country_code":null,
            "zip":"400062",
            "address_name":"zubin mistry ",
            "phone":"9870770880"            
         },
         "Currency":{  
            "id":"63",
            "abbreviation":"INR",
            "name":"Indian Rupee",
            "symbol":"\u20b9",
            "created":"0000-00-00 00:00:00",
            "modified":"2015-09-15 09:24:56",
            "rate":"66.3212",
            "priority":"0"
         },
         "OrderStatus":{  
            "id":"2440737",
            "title":"open"
         },
         "FinancialStatus":{  
            "id":"2",
            "title":"paid"
         },
         "FulfillmentStatus":{  
            "id":"3",
            "title":"shipped"
         },
         "Manifest":{  
            "id":null,
            "manifest_reference":null,
            "created":null,
            "courier_company":null,
            "picked_up_at":null,
            "courier_boy_name":null,
            "courier_boy_phone":null,
            "company_id":null,
            "schedule_pickup_token":null
         },
         "CourierCompany":{  
            "id":"9",
            "title":"Dtdc",
            "accepted_names":"[\"dtdc\",\"DTDC\",\"Dtdc\",\"dtdc\\\/dtdc\"]",
            "standard_tag":"dtdc",
            "manifest_report_type_id":"7"
         },
         "CourierStatus":{  
            "id":null,
            "title":null
         },
         "Warehouse":{  
            "id":"1",
            "title":"default warehouse",
            "created":"0000-00-00 00:00:00",
            "modified":"2014-08-14 09:44:34",
            "company_id":"2",
            "is_default":"1",
            "warehousing_account_id":null,
            "last_polled":"0000-00-00 00:00:00",
            "external_warehouse_code":null,
            "restricted_to_channel_id":null
         },
         "TaxRule":{  
            "id":null,
            "title":null,
            "company_id":null,
            "condition_field":null,
            "condition_value":null,
            "tax_percentage":null,
            "created":null,
            "is_default":null
         },
         "item_titles_orders":{  
            "id":"109427330",
            "order_id":"107391769",
            "quantity":"1",
            "tracking_ref":null,
            "item_title_id":"18355001",
            "gross_value":"521",
            "item_url":null,
            "number":null,
            "item_options":null,
            "tax_rule_id":"1",
            "shipping_batch_id":null,
            "manifest_id":null,
            "packer_id":null,
            "executed_shipping_method":null,
            "tracking_number":null,
            "courier_routing_code":null,
            "selected_shipping_method":null,
            "dispatch_date":"0000-00-00 00:00:00", "courier_status_id":null,
            "courier_company_id":null,
            "courier_tracked_on":"0000-00-00 00:00:00",
            "courier_status_raw":null,
            "courier_tracking_attempts":"0",
            "ship_label_path":null,
            "fulfillment_status_id":null,
            "shipping_value":null,
            "sub_order_reference":null,
            "warehouse_id":null,
            "stock_flag":"1",
            "dispatch_by_date":"0000-00-00 00:00:00"
         },
         "item_titles":{  
            "id":"18355001",
            "title":"Light Fish Emerald Green Glass Cone Candle Holder",
            "sku_id":null,
            "sku_code_from_channel":"LFCM05",
            "company_id":"2",
            "number":null
         },
         "skus":{  
            "id":null,
            "readable_name":null,
            "custom_code":null,
            "image_path":null,
            "image_thumb_path":null,
            "input_image_path":null,
            "company_id":null,
            "_in_stock_quantity":null,
            "awaiting_dispatch_quantity":null,
            "length_mm":null,
            "breadth_mm":null,
            "height_mm":null,
            "weight_gm":null,
            "bundle_id":null,
            "is_disabled":null,
            "is_bundle":null,
            "default_cost_price":null,
            "least_selling_price":null,
            "created":null,
            "modified":null,
            "currency_id":null,
            "is_low_stock":null,
            "low_stock_level":null,
            "product_id":null,
            "stock_sync":null,
            "product_category_id":null,
            "tax_rule_id":null,
            "style_code":null
         },
         "O":{  
            "ProductId":null
         },
         "Transaction":[  
         ],"ItemTitle":[  
            {  
               "id":"18355001",
               "title":"Light Fish Emerald Green Glass Cone Candle Holder",
               "sku_id":null,
               "sku_code_from_channel":"LFCM05",
               "company_id":"2",
               "number":null,
               "ItemTitlesOrder":{  
                  "id":"109427330",
                  "order_id":"107391769",
                  "quantity":"1",
                  "tracking_ref":null,
                  "item_title_id":"18355001",
                  "gross_value":"521",
                  "item_url":null,
                  "number":null,
                  "item_options":null,
                  "tax_rule_id":"1",
                  "shipping_batch_id":null,
                  "manifest_id":null,
                  "packer_id":null,
                  "executed_shipping_method":null,
                  "tracking_number":null,
                  "courier_routing_code":null,
                  "selected_shipping_method":null,
                  "dispatch_date":"0000-00-00 00:00:00",
                  "courier_status_id":null,
                  "courier_company_id":null,
                  "courier_tracked_on":"0000-00-00 00:00:00",
                  "courier_status_raw":null,
                  "courier_tracking_attempts":"0",
                  "ship_label_path":null,
                  "fulfillment_status_id":null,
                  "shipping_value":null,
                  "sub_order_reference":null,
                  "warehouse_id":null,
                  "stock_flag":"1",
                  "dispatch_by_date":"0000-00-00 00:00:00",
                  "TaxRule":{  
                     "id":"1",
                     "title":"VAT 5%",
                     "tax_percentage":"5"
                  }
               },
               "Sku":{  
                  "id":null,
                  "custom_code":null,
                  "style_code":null,
                  "readable_name":null,
                  "length_mm":null,
                  "breadth_mm":null,
                  "height_mm":null,
                  "weight_gm":null,
                  "default_cost_price":null
               },
               "Product":{  
                  "title":null
               },
               "brand": "Raymond"
            }
         ],
         "Message":{  
         }}}

</html>
<html>


    public static void main(String[] args) throws Exception {
        InputStream input = JSON2XML1.class.getResourceAsStream("/input.json");
        OutputStream output = System.out;
        System.out.println(input);
        // String name = (String) input.get("Name");
        JsonXMLConfig config =
            new JsonXMLConfigBuilder().multiplePI(false).build();
        XMLInputFactory factory =
            new JsonXMLInputFactory(new JsonXMLConfigBuilder().virtualRoot("assetStore").build());

        try {
            /*
             * Create source (JSON).
             */
            XMLStreamReader reader =
                new JsonXMLInputFactory(config).createXMLStreamReader(input);
            Source source = new StAXSource(reader);
            System.out.println("_______________________________________________");
            System.out.println(reader);
            System.out.println("_______________________________________________");
            /*
             * Create result (XML).
             */

            reader = factory.createXMLStreamReader(input);


            XMLStreamWriter writer =
                XMLOutputFactory.newInstance().createXMLStreamWriter(output);
            Result result =
                new StAXResult(new PrettyXMLStreamWriter(writer)); // format output
            System.out.println("_______________________________________________");
            System.out.println(output);
            System.out.println("_______________________________________________");

            /*
             * Copy source to result via "identity transform".
             */
            TransformerFactory.newInstance().newTransformer().transform(source,
                                                                        result);
            System.out.println("_______________________________________________");
            System.out.println(result);
            System.out.println("_______________________________________________");
        } finally {
            /*
             * As per StAX specification, XMLStreamReader/Writer.close() doesn't close
             * the underlying stream.
             */
            output.close();
            input.close();
        }
    }
}
</html>

我正在尝试捕获响应Json并转换为XML。只有第一个标签被转换。并非Json Response中的所有标记。 对于每个循环,我无法做到。任何帮助都会很好。 我使用API​​如下 此外,链接到孤立的答案可能还不够;如果能够链接到整个讨论的特定修订版(快照类型),即具有特定日期/时间的所有答案和评论的问题,那就更好了。

1 个答案:

答案 0 :(得分:0)

您可以使用第三方库将json转换为xml json简单库

我已将您的json内容放入json文件中。

请参阅以下代码段

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;

import org.json.JSONObject;
import org.json.XML;

public class JsonToXml {

    public static void main(String[] args) throws IOException {
        String content = new String(Files.readAllBytes(Paths.get("D://input.json")));
        JSONObject json = new JSONObject(content);
        String xml = XML.toString(json);
        System.out.println(xml);

    }
}

输出:

  <Order><vat_forms/><order_reference>301151513</order_reference><shipping_value>0</shipping_value><order_status_id>2440737</order_status_id><marketplace_fulfilled>true</marketplace_fulfilled><uuid>584770205556e897c666a270219</uuid><courier_tracking_attempts>0</courier_tracking_attempts><is_cancellable_on_channel>false</is_cancellable_on_channel><selected_shipping_method>null</selected_shipping_method><fees_value>null</fees_value><modified>2015-06-03 17:20:39</modified><dispatch_date>2015-06-03 11:50:23</dispatch_date><bt_financial_status_id>null</bt_financial_status_id><id>107391769</id><order_type>null</order_type><invoice_number>null</invoice_number><remitted_value>null</remitted_value><shipping_address_id>2716909</shipping_address_id><invoice_prefix>null</invoice_prefix><created>2015-06-02 00:00:00</created><vatlinks/><stock_adjusted>1</stock_adjusted><shipping_batch_id>22549</shipping_batch_id><breadth_mm>null</breadth_mm><created_on>null</created_on><parent_id>null</parent_id><remittance_note>null</remittance_note><bt_order_status_id>null</bt_order_status_id><channel_id>1703</channel_id><manifest_id>null</manifest_id><financial_status_id>2</financial_status_id><fulfillment_status_id>3</fulfillment_status_id><unread>1</unread><weight_gm>null</weight_gm><is_cod>0</is_cod><tax_rule_id>null</tax_rule_id><courier_status_raw>null</courier_status_raw><ship_label_path>null</ship_label_path><tracking_number>B19152417</tracking_number><replacement_pending>null</replacement_pending><length_mm>null</length_mm><courier_company_id>9</courier_company_id><problem_reason>null</problem_reason><courier_tracked_on>null</courier_tracked_on><executed_shipping_method>DTDC</executed_shipping_method><bt_fulfillment_status_id>3</bt_fulfillment_status_id><remittance_date>null</remittance_date><replacement_order_id>null</replacement_order_id><marketplace_status>null</marketplace_status><courier_status_id>null</courier_status_id><gross_value>521</gross_value><customer_id>81259975</customer_id><height_mm>null</height_mm><currency_id>63</currency_id><warehouse_id>1</warehouse_id></Order><Warehouse><company_id>2</company_id><last_polled>0000-00-00 00:00:00</last_polled><warehousing_account_id>null</warehousing_account_id><created>0000-00-00 00:00:00</created><external_warehouse_code>null</external_warehouse_code><modified>2014-08-14 09:44:34</modified><id>1</id><title>default warehouse</title><is_default>1</is_default><restricted_to_channel_id>null</restricted_to_channel_id></Warehouse><skus><weight_gm>null</weight_gm><tax_rule_id>null</tax_rule_id><_in_stock_quantity>null</_in_stock_quantity><product_category_id>null</product_category_id><stock_sync>null</stock_sync><product_id>null</product_id><default_cost_price>null</default_cost_price><modified>null</modified><id>null</id><is_low_stock>null</is_low_stock><length_mm>null</length_mm><least_selling_price>null</least_selling_price><company_id>null</company_id><created>null</created><is_disabled>null</is_disabled><style_code>null</style_code><readable_name>null</readable_name><is_bundle>null</is_bundle><breadth_mm>null</breadth_mm><image_path>null</image_path><image_thumb_path>null</image_thumb_path><awaiting_dispatch_quantity>null</awaiting_dispatch_quantity><bundle_id>null</bundle_id><input_image_path>null</input_image_path><height_mm>null</height_mm><low_stock_level>null</low_stock_level><custom_code>null</custom_code><currency_id>null</currency_id></skus><Customer><full_name>zubin mistry </full_name><created>0000-00-00 00:00:00</created><customer_reference>null</customer_reference><company_name>null</company_name><nickname>null</nickname><last_name/><modified>0000-00-00 00:00:00</modified><id>81259975</id><salutation>null</salutation><first_name>zubin mistry</first_name><channel_id>1703</channel_id><email>null</email></Customer><Channel><invoice_template>null</invoice_template><stock_sync>1</stock_sync><email_from_name>null</email_from_name><email_bcc>null</email_bcc><id>1703</id><title>pepper fry</title><ChannelType><favicon_path>/img/icons/icon_offline_store.png</favicon_path><standard_tag>offline_store</standard_tag></ChannelType><email>null</email></Channel><Manifest><courier_company>null</courier_company><courier_boy_name>null</courier_boy_name><company_id>null</company_id><created>null</created><picked_up_at>null</picked_up_at><id>null</id><manifest_reference>null</manifest_reference><schedule_pickup_token>null</schedule_pickup_token><courier_boy_phone>null</courier_boy_phone></Manifest><FinancialStatus><id>2</id><title>paid</title></FinancialStatus><O><ProductId>null</ProductId></O><OrderStatus><id>2440737</id><title>open</title></OrderStatus><FulfillmentStatus><id>3</id><title>shipped</title></FulfillmentStatus><CourierStatus><id>null</id><title>null</title></CourierStatus><item_titles><number>null</number><company_id>2</company_id><sku_code_from_channel>LFCM05</sku_code_from_channel><sku_id>null</sku_id><id>18355001</id><title>Light Fish Emerald Green Glass Cone Candle Holder</title></item_titles><TaxRule><condition_field>null</condition_field><company_id>null</company_id><created>null</created><tax_percentage>null</tax_percentage><condition_value>null</condition_value><id>null</id><title>null</title><is_default>null</is_default></TaxRule><ShippingAddress><zip>400062</zip><country>India</country><country_code>null</country_code><address_line3/><address_line2>Goregaon West</address_line2><address_line1>202  new cd baug . next to city center shopping mall  in sai baba enclave compound</address_line1><city>Mumbai</city><phone>9870770880</phone><id>2716909</id><state>Maharashtra</state><address_name>zubin mistry </address_name></ShippingAddress><Currency><symbol>?</symbol><rate>66.3212</rate><created>0000-00-00 00:00:00</created><name>Indian Rupee</name><modified>2015-09-15 09:24:56</modified><id>63</id><abbreviation>INR</abbreviation><priority>0</priority></Currency><ItemTitle><number>null</number><ItemTitlesOrder><item_options>null</item_options><fulfillment_status_id>null</fulfillment_status_id><courier_routing_code>null</courier_routing_code><shipping_value>null</shipping_value><tax_rule_id>1</tax_rule_id><courier_tracking_attempts>0</courier_tracking_attempts><selected_shipping_method>null</selected_shipping_method><courier_status_raw>null</courier_status_raw><number>null</number><packer_id>null</packer_id><ship_label_path>null</ship_label_path><tracking_number>null</tracking_number><dispatch_date>0000-00-00 00:00:00</dispatch_date><stock_flag>1</stock_flag><id>109427330</id><courier_company_id>null</courier_company_id><courier_tracked_on>0000-00-00 00:00:00</courier_tracked_on><quantity>1</quantity><executed_shipping_method>null</executed_shipping_method><item_title_id>18355001</item_title_id><sub_order_reference>null</sub_order_reference><tracking_ref>null</tracking_ref><shipping_batch_id>null</shipping_batch_id><TaxRule><tax_percentage>5</tax_percentage><id>1</id><title>VAT 5%</title></TaxRule><courier_status_id>null</courier_status_id><gross_value>521</gross_value><item_url>null</item_url><dispatch_by_date>0000-00-00 00:00:00</dispatch_by_date><order_id>107391769</order_id><manifest_id>null</manifest_id><warehouse_id>null</warehouse_id></ItemTitlesOrder><company_id>2</company_id><sku_code_from_channel>LFCM05</sku_code_from_channel><Product><title>null</title></Product><sku_id>null</sku_id><id>18355001</id><title>Light Fish Emerald Green Glass Cone Candle Holder</title><Sku><length_mm>null</length_mm><breadth_mm>null</breadth_mm><style_code>null</style_code><default_cost_price>null</default_cost_price><readable_name>null</readable_name><weight_gm>null</weight_gm><id>null</id><height_mm>null</height_mm><custom_code>null</custom_code></Sku><brand>Raymond</brand></ItemTitle><CourierCompany><accepted_names>[&quot;dtdc&quot;,&quot;DTDC&quot;,&quot;Dtdc&quot;,&quot;dtdc\/dtdc&quot;]</accepted_names><manifest_report_type_id>7</manifest_report_type_id><standard_tag>dtdc</standard_tag><id>9</id><title>Dtdc</title></CourierCompany><item_titles_orders><item_options>null</item_options><fulfillment_status_id>null</fulfillment_status_id><courier_routing_code>null</courier_routing_code><shipping_value>null</shipping_value><tax_rule_id>1</tax_rule_id><courier_tracking_attempts>0</courier_tracking_attempts><selected_shipping_method>null</selected_shipping_method><courier_status_raw>null</courier_status_raw><number>null</number><packer_id>null</packer_id><ship_label_path>null</ship_label_path><tracking_number>null</tracking_number><dispatch_date>0000-00-00 00:00:00</dispatch_date><stock_flag>1</stock_flag><id>109427330</id><courier_company_id>null</courier_company_id><courier_tracked_on>0000-00-00 00:00:00</courier_tracked_on><quantity>1</quantity><executed_shipping_method>null</executed_shipping_method><item_title_id>18355001</item_title_id><sub_order_reference>null</sub_order_reference><tracking_ref>null</tracking_ref><shipping_batch_id>null</shipping_batch_id><courier_status_id>null</courier_status_id><gross_value>521</gross_value><item_url>null</item_url><dispatch_by_date>0000-00-00 00:00:00</dispatch_by_date><order_id>107391769</order_id><manifest_id>null</manifest_id><warehouse_id>null</warehouse_id></item_titles_orders>

希望这会对你有所帮助。