【问题标题】:Convert XML to JSON without attributes using PHP?使用 PHP 将 XML 转换为没有属性的 JSON?
【发布时间】:2011-06-18 23:25:19
【问题描述】:

我想用 PHP 将 XML 文件转换为 JSON。 XML 文件如下所示:

<content>
    <!-- other elements... -->
    <box id="1">
        <a>...</a>
        <b>...</b>
        <c>...</c>
    </box>
    <box id="2">
        <a>...</a>
        <b>...</b>
        <c>...</c>
    </box>
    <box id="3">
        <a>...</a>
        <b>...</b>
        <c>...</c>
    </box>
    <!-- more <box> elements... -->
    <!-- other elements... -->
</content>

我正在使用这个简单的 PHP 脚本:

// Open XML file with SimpleXML.
$xml = simplexml_load_file('file.xml');
// Convert XML content to JSON.
$json = json_encode($xml);
// Output JSON.
echo $json;

我将 XML 文件的完整内容作为 JSON 输出,但是我需要将脚本修改为:

  • 仅获取 &lt;box&gt; 的 JSON 元素,而不是完整的文件。
  • 获取不带元素属性的 JSON。

这是我想要输出的示例:

[{"a":"...","b":"...","c":"..."},
{"a":"...","b":"...","c":"..."},
{"a":"...","b":"...","c":"..."}]

请帮帮我,我该怎么做?最佳做法是什么?

提前致谢。

【问题讨论】:

  • 你需要遍历 $xml 提取所有单独的标签并将其放入一个新变量中,然后 json_encode() 它

标签: php xml json


【解决方案1】:

您可以尝试单独访问元素,例如:

$boxes = array();
// Loop through each box element.
foreach($xml->box as $box) {
    // Add an array with the a, b, and c children.
    $boxes[] = array('a' => $box-> a, 'b' => $box->b, 'c' => $box->c);
}
$json = json_encode($boxes);

这将遍历每个盒子元素,将 a、b 和 c 标签拉出到一个数组中,然后对数组而不是 SimpleXML 对象进行 JSON 编码。

【讨论】:

    【解决方案2】:

    如果节点名称(box)没有改变,你可以使用xpath:

    $xml = simplexml_load_file('test.xml');
    $arr = (array) $xml -> xpath('box');
    

    ...但是因为每个box都有一个id,这就导致了一种疯狂:

    $final = array();
    foreach ($arr as $box) {
        $box = (array) $box;
        unset($box['@attributes']);
        $final[] = $box;
    }
    

    我打算寻找更好的方法,但我开始看到一把漂浮的匕首,所以我放弃了。照原样,您只需要 json_encode $final 数组。神速。

    【讨论】:

    • "一个简单的将XML转换成JSON的方法!一个简单的把XML转换成JSON的方法!我的王国有一个简单的把XML转换成JSON的方法!"
    【解决方案3】:

    基本上,您必须按照其他受访者的建议自行设置 JSON 输出格式。

    这给了我我想要的 JSON - 与您想要的非常相似。它是递归的,因此适用于任何 XML。

    这个 XML:

        <?xml version="1.0" encoding="UTF-8"?>
        <lss>
          <image type="Profile Image" part_id="11017" media="Image1.jpg" base="Image1" ext="jpg" width="128" height="96"/>
          <image type="Profile Image" part_id="11016" media="Image2.jpg" base="Image2" ext="jpg" width="180" height="225"/>
        </lss>
    

    格式如下:

      {
         "image":[
            {
               "type":"Profile Image",
               "part_id":"11017",
               "media":"Image1.jpg",
               "base":"Image1",
               "ext":"jpg",
               "width":"128",
               "height":"96"
            },
            {
               "type":"Profile Image",
               "part_id":"11016",
               "media":"Image2.jpg",
               "base":"Image2",
               "ext":"jpg",
               "width":"180",
               "height":"225"
            }
         ]
      }
    

    这对您来说可能没问题,但如果不是,您只需稍微修改 $final_tree 即可在顶层获得裸“盒子”对象数组。

        function xml2json($xmlString)
        {   
            $start_tree = (array) simplexml_load_string(trim($xmlString));
    
            $final_tree = array();
    
            loopRecursivelyForAttributes($start_tree,$final_tree);
    
            return json_encode($final_tree);
        }   
    
        function loopRecursivelyForAttributes($start_tree,&$final_tree)
        {   
            foreach ($start_tree as $key1=>$row1)
            {   
                if(!array_key_exists($key1, $final_tree))
                {   
                    $final_tree[$key1] = array();
                }   
    
                // If there is only one sub node, then there will be one less
                // array - ie: $row1 will be an array which has an '@attributes' key
                if(array_key_exists('@attributes', $row1))
                {   
                    $row1 = (array) $row1;
    
                    getValues($start_tree,$final_tree, $key1, $row1);
                }
                else
                {   
                    foreach ($row1 as $row2)
                    {   
                        $row2 = (array) $row2;
    
                        getValues($start_tree,$final_tree, $key1, $row2);
                    }
                }
            }
        }
    
        function getValues($start_tree,&$final_tree, $key1, $row2)
        {   
            foreach ($row2 as $key3=>$val3)
            {   
                $val3 = (array) $val3;
    
                if($key3 == '@attributes')
                {   
                    $final_tree[$key1][] = $val3;
                }
                else
                {   
                    $temp_parent = array();
    
                    $temp_parent[$key3] = $val3;
    
                    loopRecursivelyForAttributes($temp_parent,$final_tree[$key1][count($final_tree[$key1])-1]);
                }
            }   
        }               
    

    【讨论】:

      猜你喜欢
      • 2021-10-26
      • 1970-01-01
      • 2016-08-09
      • 2012-02-02
      • 1970-01-01
      • 1970-01-01
      • 2016-09-21
      • 1970-01-01
      • 2020-03-12
      相关资源
      最近更新 更多