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?