bobdebower's avatar

converting array to xml

hey guys, iam using the libaray ArrayToXml. i want to be able to tell the converter IF THE ARRAY ELEMENT IS EMPTY DON'T CONVERT IT TO XML.

Example:

The 'password' is empty in this array

  {
    "firstName": "Chuck",
  "lastName": "Norris",
   "age": 75,
   "bio": "Roundhouse kicking asses since 1940",
    "password": ""
 }

and it convert to this

    <?xml version="1.0" encoding="UTF-8"?>
     <root>
     <firstName>Chuck</firstName>
     <lastName>Norris</lastName>
       <age>75</age>
    <bio>Roundhouse kicking asses since 1940</bio> 
    <password></password> 
  </root>

what i want is this. so the 'password element gone'

   <?xml version="1.0" encoding="UTF-8"?>
    <root>
     <firstName>Chuck</firstName>
     <lastName>Norris</lastName>
       <age>75</age>
    <bio>Roundhouse kicking asses since 1940</bio>  
 </root>

so i use the ArrayToXML package.

i can't figure it out. here is my convert function

public function show(Configuration $configuration)
   {
    $result = ArrayToXml::convert($configuration->data, 'root', true, 'UTF-8');
    return response($result)->header('Content-Type', 'text/xml');
   }

Maybe i have to do something in the ArrayToXml.php file. from: https://github.com/spatie/array-to-xml

0 likes
9 replies
MichalOravec's avatar
Level 75

You need to filter that array before you pass ti to the convert method.

public function show(Configuration $configuration)
{
    $result = ArrayToXml::convert(collect($configuration->data)->filter()->toArray(), 'root', true, 'UTF-8');
    
    return response($result)->header('Content-Type', 'text/xml');
}
2 likes
bobdebower's avatar

But it doesn't works with array attrubutes and hidden empty data ;{ any tips

Here is the array

{
  "data": {
  "_attributes": {
   "phone": "",
  "username": "",
  "password": ""
  }
 },
"fruits": {
"fruit": []
 }
}

Here is the Xml output.

 <root>
  <data phone="" password="" username=""/>
  <fruits>
    <fruit/>
  </fruits>

Any tips? Thanks!

MichalOravec's avatar

For the second level

$data = collect($configuration->data)->map(function ($value) {
    if (is_array($value)) {
        return collect($value)->filter()->toArray();
    }
    
    return $value;
})->filter()->toArray();
1 like
bobdebower's avatar

oh waaw, haha could you explain what this does please? How do i integrate this with my $result.

MichalOravec's avatar

I won't explain to you those methods, because everything you can find in the documentation

https://laravel.com/docs/8.x/collections

public function show(Configuration $configuration)
{
    $data = collect($configuration->data)->map(function ($value) {
        if (is_array($value)) {
            return collect($value)->filter()->toArray();
        }
        
        return $value;
    })->filter()->toArray();

    $result = ArrayToXml::convert($data, 'root', true, 'UTF-8');
    
    return response($result)->header('Content-Type', 'text/xml');
}
1 like
jeffreyvanrossum's avatar

You can unset empty values from the array before you convert to XML.

You could use the array_filter function for that.

Please or to participate in this conversation.