Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用PHP将XML转换为不带属性的JSON?_Php_Xml_Json - Fatal编程技术网

使用PHP将XML转换为不带属性的JSON?

使用PHP将XML转换为不带属性的JSON?,php,xml,json,Php,Xml,Json,我想用PHP将XML文件转换成JSON文件。XML文件如下所示: <content> <!-- other elements... --> <box id="1"> <a>...</a> <b>...</b> <c>...</c> </box> <box id="2">

我想用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>
  {
     "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"
        }
     ]
  }
我将XML文件的全部内容作为JSON输出,但是我需要修改脚本以:

  • 仅获取
    元素,而不是完整的文件
  • 获取不带元素属性的JSON
这是我希望得到的输出的一个示例:

[{"a":"...","b":"...","c":"..."},
{"a":"...","b":"...","c":"..."},
{"a":"...","b":"...","c":"..."}]
请帮帮我,我该怎么做?最佳做法是什么


提前感谢。

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

$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);
这将循环遍历每个box元素,将a、b和c标记提取到一个数组中,然后用JSON编码数组而不是SimpleXML对象。

如果节点名(
box
)没有更改,可以使用
xpath

$xml = simplexml_load_file('test.xml');
$arr = (array) $xml -> xpath('box');
…但由于每个
都有一个
id
,这导致了一种疯狂:

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

我本想找一个更好的办法,但我开始看到一把浮动的匕首,所以我放弃了。实际上,您只需要对
$final
数组进行
json\u编码。Godspeed.

基本上,您必须按照其他受访者的建议,自己格式化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>
这对您来说可能很好,但如果不是这样,您只需稍微修改$final_tree即可在顶层获得裸露的“box”对象数组

    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]);
            }
        }   
    }               

您需要循环使用$xml,取出所有单独的标记并将其放入一个新变量,然后使用json_encode()将其转换为“一种将xml转换为json的简单方法!一种将xml转换为json的简单方法!我的王国是一种将xml转换为json的简单方法!”
    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]);
            }
        }   
    }