2011-02-01 80 views
3

我想转换和XML文件到JSON与PHP。该XML文件是这样的:将XML转换为JSON而没有使用PHP的属性?

<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输出的全部内容,但是我需要修改脚本:

  • 仅获得JSON的<box> 元素,而不是完整的文件。
  • 获取没有元素属性的JSON。

这是什么,我想作为输出的例子:

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

请大家帮帮我,我该怎么办呢?最佳做法是什么?

在此先感谢。

+2

你会需要遍历$ XML拉出所有的个人标签,并把它变成一个新的变量,然后json_encode()它 – 2011-02-01 01:06:20

回答

1

如果节点名称(box)不会改变,你可以使用xpath

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

...但因为每个boxid,这导致了一种疯狂的:

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

我正在寻找更好的方法,但我开始看到一把漂浮的匕首,所以我放弃了。因为你只需要json_encode$final阵列。一帆风顺。

+2

“的简单方法XML转换成JSON!一个简单的方法将XML转换到JSON!我的王国将XML转换为JSON的简单方法!“ – sdleihssirhc 2011-02-01 02:09:51

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的标签拉出到一个数组,然后JSON编码阵列而不是SimpleXML对象。

1

基本上,您必须按照其他受访者所建议的格式输出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]); 
      } 
     } 
    }