PHP服务器端隐式xml到json,结果具有不同的格式

时间:2018-07-30 14:20:20

标签: php json xml

我正在尝试将xml转换为json,我试图使用以下代码,

$xml = simplexml_load_string($response, 'SimpleXMLElement', LIBXML_NOCDATA);
$json = json_encode($xml);
$arr = json_decode($json,true);

此代码可以正常工作,但是有一个问题, XML文件中的某些内容有机会具有多个值或只有一个值,这些值的XML转换结果将有所不同,对于单个值,它将是与单个值相关的键,对于多个值,它将返回一个数组。我的问题是如何确定返回值是否一致。

以下是返回值的示例。

多值

"item": [
                    {
                        "product_id": "XL2",
                        "description": "222 For You",
                        "d_image": "D_NUL.png",
                        "m_image": "M_NUL.png",
                        "dm_image": "DM_NUL.png",
                        "whole_toppings": {
                            "topping": "PEPPERONI"
                        },
                        "left_toppings": {
                            "topping": "MUSHROOMS"
                        },
                        "right_toppings": {
                            "topping": "ANCHOVIES"
                        },
                        "crust": "THICK CRUST (REGULAR)",
                        "sauce": "REGULAR",
                        "cook": "REGULAR"
                    },
                    {
                        "product_id": "XLA",
                        "description": [],
                        "d_image": "D_NUL.png",
                        "m_image": "M_NUL.png",
                        "dm_image": "DM_NUL.png",
                        "whole_toppings": {
                            "topping": "PEPPERONI"
                        },
                        "left_toppings": {
                            "topping": "MUSHROOMS"
                        },
                        "right_toppings": {
                            "topping": "ANCHOVIES"
                        },
                        "crust": "THICK CRUST (REGULAR)",
                        "sauce": "REGULAR",
                        "cook": "REGULAR"
                    }
                ]
            },

单个值:

"item": {
                    "product_id": "MMED",
                    "description": "Mediterranean",
                    "d_image": "D_NUL.png",
                    "m_image": "M_NUL.png",
                    "dm_image": "DM_NUL.png",
                    "whole_toppings": {
                        "topping": [
                            "PEPPERONI",
                            "NO ONIONS",
                            "NO FETA CHEESE",
                            "NO SEASONED BEEF",
                            "NO TOMATOES"
                        ]
                    },

我的预期结果将是

"item": [{
                    "product_id": "MMED",
                    "description": "Mediterranean",
                    "d_image": "D_NUL.png",
                    "m_image": "M_NUL.png",
                    "dm_image": "DM_NUL.png",
                    "whole_toppings": {
                        "topping": [
                            "PEPPERONI",
                            "NO ONIONS",
                            "NO FETA CHEESE",
                            "NO SEASONED BEEF",
                            "NO TOMATOES"
                        ]
                    }],

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

您依赖于SimpleXML的自动对象序列化。这里没有可用信息表明XML元素MIGHT具有同名的兄弟姐妹。该信息不存在,因此没有自动转换可以创建所需的输出。 (并非没有提供该信息的东西。)

您将需要遍历XML树并生成您的特定输出。这是一个简化的示例:

[(+ 1), (+ 2), (+ 3)] <*> [5] == [6, 7, 8]

输出:

$xml = <<<'XML'
<items>
  <item>
    <product_id>1</product_id>
    <whole_toppings>
      <topping>PEPPERONI</topping>
    </whole_toppings>
  </item>
  <item>
    <product_id>2</product_id>
    <whole_toppings>
      <topping>PEPPERONI</topping>
      <topping>MUSHROOMS</topping>
    </whole_toppings>
  </item>
</items>
XML;

$itemsElement = new SimpleXMLElement($xml);

$items = [];
foreach ($itemsElement->item as $itemElement) {
  $item = [
    'product_id' => (string)$itemElement->product_id,
    'whole_toppings' => [] ,
    'left_toppings' => [] 
  ];
  if ($itemElement->whole_toppings) {
    foreach ($itemElement->whole_toppings->topping as $toppingElement) {
     $item['whole_toppings'][] = (string)$toppingElement;
    }
  }
  if ($itemElement->left_toppings) {
    foreach ($itemElement->left_toppings->topping as $toppingElement) {
       $item['left_toppings'][] = (string)$toppingElement;
    }
  }
  $items[] = $item;
}

echo json_encode($items, JSON_PRETTY_PRINT);

最初需要做更多的工作,但是它为您提供了对输出的更多控制。如您所见,JSON输出具有两个产品的[ { "product_id":"1", "whole_toppings":[ "PEPPERONI" ], "left_toppings":[ ] }, { "product_id":"2", "whole_toppings":[ "PEPPERONI", "MUSHROOMS" ], "left_toppings":[ ] } ] 数组和whole_toppings数组(但XML中没有此标签)。

要对其进行优化,可以将逻辑重构为辅助函数/方法。

left_toppings