Convert XML to JSON without attributes using PHP?

I want to convert and XML file to JSON using PHP. The XML file is as follows:

<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>

I am using this simple php script:

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

I get the full contents of the XML file as JSON output, but I need to change the script to:

  • Get only JSON for <box> elements, not the complete file.
  • Get JSON without element attributes.

This is an example of what I want to get as output:

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

Please help me, how can I do this? What is the best practice?

Thanks in advance.

+3
source share
3 answers

If the name of node ( box) does not change, you can use 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. Godspeed.

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

Source: https://habr.com/ru/post/1789133/


All Articles