0

I am using simplexml to load an XML file and encode the content in JSON format so that it can be consumed by another part of my application. Everything works fine but I noticed that simplexml generates singular names for "arrays", for instance, this piece of XML:

<employees>
    <employee>
        <name>John M.</name>
        <age>34</age>
    </employee>
    <employee>
        <name>Sarah J.</name>
        <age>31</age>
    </employee>
</employees>

Once that I load the XML code in a PHP object using:

$xml = simplexml_load_file("employees.xml");

I have to use the singular form for access the arrays of employees, for instance:

$xml->employee[1];

But I want to pluralize names of arrays. The major reason for doing this is because I want to generate JSON from that XML object directly using json_encode, rather than generating a new array/object with the respective name pluralized.

It is possible to change this behavior? If this cannot be accomplished within simplexml how would be the best approach to resolve this?

Giacomo1968
  • 25,759
  • 11
  • 71
  • 103
mevqz
  • 653
  • 3
  • 9
  • 19
  • 1
    Some example code can be found in an [answer to a related question (PHP convert XML to JSON group when there is one child)](http://stackoverflow.com/a/16938322/367456), coming from a different problem, there is also [Part III in a series of posts regarding SimpleXML and JSON](http://hakre.wordpress.com/2013/07/10/simplexml-and-json-encode-in-php-part-iii-and-end/) that shows a solution decorating SimpleXMLElement that is superior over extending it that is commented. – hakre Dec 29 '13 at 00:05

2 Answers2

2

It is possible to change this behavior?

Short answer: no.


You're going to want to "generate [something] with the respective name pluralized". In any case, I would advise against simply json_encode()-ing SimpleXMLElement objects anyway.

In the long run (and in your case, it seems, the short run) you'd be better off:

  • changing the XML document to have pluralised element names
  • or, doing some work yourself as an intermediate step between SimpleXML and json_encode()

As for "the best approach" (for serializing anything as JSON), I would advise going for your own class (perhaps extending SimpleXMLElement) that implements the JsonSerializable interface. Other options include changing the XML as mentioned above, or building your own array/object structure to JSON encode, to name a few.

salathe
  • 51,324
  • 12
  • 104
  • 132
0

When you say:

But I want to pluralize names of arrays.

It makes little sense. The XML parser in simplexml is just regurgitating what it sees. And in your XML structure:

<employees>
    <employee>
        <name>John M.</name>
        <age>34</age>
    </employee>
    <employee>
        <name>Sarah J.</name>
        <age>31</age>
    </employee>
</employees>

There is indeed an overarching <employees> hierarchy. But each employee record, the employee is stored as the singular <employee>. Which is returned in an array appropriately named $employee.

If it’s confusing to you, just ad a few more steps to the XML to JSON conversion to achieve your goal:

$xml = simplexml_load_file("employees.xml");
$xml2json = (array) $xml;
$json_employees = json_encode(array('employees' => $xml2json['employee']));

echo '<pre>';
print_r($json_employees);
echo '</pre>';

The JSON output would be:

{
    "employees": [
        {
            "name": "John M.",
            "age": "34"
        },
        {
            "name": "Sarah J.",
            "age": "31"
        }
    ]
}
Giacomo1968
  • 25,759
  • 11
  • 71
  • 103