Search code examples
phpxmlsimplexmldomdocument

Convert XML to Object in PHP and again convert that Object to XML?


Lets say I have a XML as below -

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

I want this to be converted to object, but when I perform below steps, it gives me stdclass object as below [I am assigning it to $stepInformation variable] -

$xml = json_decode(json_encode((array) simplexml_load_string($xml)), 1);

$stepInformation = stdClass Object
(
    [@attributes] => Array
        (
            [number] => 9
        )

    [s_name] => test
    [b_sel] => 12345
    [b_ind] => 7
)

So when I am parsing this stdclass object in a php function

function convertStepInformationToArray($stepInformation)
{
     $dom = new DOMDocument();
    $stepInfo = "{$stepInformation->s_name}{$stepInformation->b_sel}{$stepInformation->b_ind}";    
$dom->loadXML("<document>" . $stepInfo . "</document>");
    $domx = new DOMXPath($dom);
    $entries = $domx->evaluate("//step");
    return $entries;
}

The output I am getting is

DOMNodeList Object
(
    [length] => 0
)

I want [length] => 1 to proceed with my project. I know the problem is with the <step number="9"> which is coming as below after converting it to object.

stdClass Object
    (
        [@attributes] => Array
            (
                [number] => 9
            )

NOTE- I have even tried with below steps but no luck:

$xml = simplexml_load_string($xml);

$stepInformation = SimpleXMLElement Object
        (
            [@attributes] => Array
            (
                [number] => 9
            )

        [s_name] => test
        [b_sel] => 12345
        [b_ind] => 7
)

Could you guys please give me some pointers on this, how can I get the output as below ? Any alternative approach would be Ok, as long as I am getting the exact output -

   DOMNodeList Object
    (
        [length] => 1
    )

Any help on this would be appreciated.

Thank you.


Solution

  • 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($xml);
    
    $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($xml);
    $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($dom);
        $entries = $domx->evaluate("//step");
        return $entries;
    }
    
    print_r(convertStepInformationToArray($xml));