You actually don't need to load the SimpleXML object into that json_encode/decode.

You can use that object already and parse whatever values you needed. Having to encode/decode as an array and to have to access values and then converting it into SimpleXML is too much.

$xml = '<?xml version="1.0"?>
<step number="9">
  <s_name>test</s_name>
  <b_sel>12345</b_sel>
  <b_ind>7</b_ind>
</step>';

$xml = simplexml_load_string(step = $xml->xpath('//step');

echo $step[0]->attributes()->number; // 9

echo $xml->s_name, '<br/>', $xml->s_name, '<br/>', $xml->b_ind;

Sample Output

With DOMDocument alone:

$dom = new DOMDocument;
$dom->loadXML(xpath = new DOMXpath($dom);

echo $xpath->evaluate('string(//step/@number)');

Just keep it simple:

$xml = '<?xml version="1.0"?>
<step number="9">
    <s_name>test</s_name>
    <b_sel>12345</b_sel>
    <b_ind>7</b_ind>
</step>';

function convertStepInformationToArray($stepInformation) {
    $dom = new DOMDocument();
    $dom->loadXML($stepInformation);
    $domx = new DOMXPath(entries = $domx->evaluate("//step");
    return $entries;
}

print_r(convertStepInformationToArray($xml));
Answer from Kevin on Stack Overflow
๐ŸŒ
PHP
php.net โ€บ manual โ€บ en โ€บ function.xml-parse-into-struct.php
PHP: xml_parse_into_struct - Manual
Example #3 parsemoldb.php - parses moldb.xml into an array of molecular objects
Discussions

Serialize Object to XML with PHP - PHP - SitePoint Forums | Web Development & Design Community
Hi, I was hoping someone knew of a good XML serializer (not including the Pear one) for PHP. I would like to take an object and serialize it into XML to make a large webservice more maintainable. Any other thoughts on the best way to manage a large xml structure for a webservice are welcome as well. More on sitepoint.com
๐ŸŒ sitepoint.com
0
January 30, 2011
Unit testing for converting xml file to php classes - Need Help - Cake Software Foundation, Inc.
Hi there, I need help. I convert a xml file to php classes and Iโ€™d like to unit test this task. Thanks More on discourse.cakephp.org
๐ŸŒ discourse.cakephp.org
0
March 19, 2019
SOAP Response Object Tree from XML String : r/PHPhelp
๐ŸŒ r/PHPhelp
Generate XML file based on a XSD file.
You can produce a solution by yourself by creating a XSD parser with Dissect , for example, or even JMS Serializer and then create the XML file based on the definition you parsed, using the Faker library while waliking the structure of the schema. I know some old XSD parsers exists in PHP but I don't know where to find them. More on reddit.com
๐ŸŒ r/PHP
4
1
December 17, 2013
๐ŸŒ
DaniWeb
daniweb.com โ€บ programming โ€บ web-development โ€บ threads โ€บ 364465 โ€บ convert-php-object-to-xml
Convert PHP Object to XML | DaniWeb
If your data is a nested stdClass/array, you can serialize it into a SimpleXMLElement and then run XPath. The key is to recurse through objects/arrays, sanitize element names (XML names cannot start with a digit or contain spaces), and repeat element names for numerically indexed arrays.
๐ŸŒ
W3Schools
w3schools.com โ€บ php โ€บ func_simplexml_load_string.asp
PHP simplexml_load_string() Function
Well organized and easy to understand Web building tutorials with lots of examples of how to use HTML, CSS, JavaScript, SQL, Python, PHP, Bootstrap, Java, XML and more.
๐ŸŒ
W3Schools
w3schools.com โ€บ php โ€บ php_xml_simplexml_read.asp
PHP SimpleXML Parser
This parser provides an easy way to access elements, attributes and textual content of an XML document. Tip: The SimpleXML Parser is a part of the PHP core.
Find elsewhere
๐ŸŒ
SitePoint
sitepoint.com โ€บ php
Serialize Object to XML with PHP - PHP - SitePoint Forums | Web Development & Design Community
January 30, 2011 - Hi, I was hoping someone knew of a good XML serializer (not including the Pear one) for PHP. I would like to take an object and serialize it into XML to make a large webservice more maintainable. Any other thoughts on โ€ฆ
๐ŸŒ
SitePoint
sitepoint.com โ€บ blog โ€บ php โ€บ parsing xml with simplexml
PHP Master | Parsing XML With SimpleXML
November 7, 2024 - SimpleXML is a PHP extension that allows us to easily manipulate and get XML data. It converts the structure of an XML document into an object that can be processed with normal property selectors and array iterators. This makes it easier to read, parse, and manipulate XML files.
๐ŸŒ
GitHub
gist.github.com โ€บ rotexdegba โ€บ 1c50627b16afd94f9793
How to Quickly Convert Xml Read into a SimpleXMLElement object into PHP Objects ยท GitHub
How to Quickly Convert Xml Read into a SimpleXMLElement object into PHP Objects - quickly-convert-xml-to-php-objects.php
๐ŸŒ
PHPBuilder
board.phpbuilder.com โ€บ d โ€บ 10381187-mapping-xml-to-object
Mapping XML to Object - PHPBuilder Forums
September 2, 2011 - Does anyone know of a tool, script some of that nature that will map a multi-level XML file to a custom object? Using SimpleXML I can get a StdObject, but ne...
๐ŸŒ
Medium
sergheipogor.medium.com โ€บ convert-xml-to-json-like-a-pro-in-php-603d0a3351f1
Convert XML to JSON Like a Pro in PHP! | by Serghei Pogor | Medium
March 21, 2024 - Once we have the XML data parsed, we can convert it to JSON format. PHPโ€™s json_encode() function can be used to accomplish this. ... By passing the SimpleXMLElement object to json_encode(), PHP automatically converts it to a JSON string.
๐ŸŒ
CakePHP
discourse.cakephp.org โ€บ need help
Unit testing for converting xml file to php classes - Need Help - Cake Software Foundation, Inc.
March 19, 2019 - Hi there, I need help. I convert a xml file to php classes and Iโ€™d like to unit test this task. Thanks
๐ŸŒ
MojoAuth
mojoauth.com โ€บ serialize-and-deserialize โ€บ serialize-and-deserialize-xml-with-php
Serialize and Deserialize XML with PHP | Serialize & Deserialize Data Across Languages
SimpleXMLElement provides a convenient object-oriented interface for parsing XML strings or files. For quicker loading, functions like simplexml_load_string() and simplexml_load_file() are often employed, directly returning a SimpleXMLElement object.