How to pass an xml to foreach without file usage?

1

From the data returned by a query, I would like to generate an xml, but without saving it to the file and traversing the xml tags to treat the data in some way. I would like to know how to pass the xml to foreach without having this xml in file.

Suppose the generated xml is:

<disciplinas>
<disciplina id="1">
    <periodo>1</periodo>
    <nome>matematica</nome>
</disciplina>
<disciplina id="id_disciplina">
    <periodo>1</periodo>
    <nome>portugues</nome>
</disciplina>
<disciplina id="2">
    <periodo>1</periodo>
    <nome></nome>
</disciplina>
<disciplina id="3">
    <periodo>2</periodo>
    <nome>historia</nome>
</disciplina>
</disciplinas>

With xml, I want to check the data to generate a table with rowspan where the values of the "period" are repeated

insteadofatablewithoutrowspan.

    
asked by anonymous 16.07.2014 / 18:10

1 answer

1

Simple:

  • query
  • processes with laço ;
  • generates string XML
  • echo no header and no XML as document body
  • Example:

    <?php
    
    $test_array = array (
      'bla' => 'blub',
      'foo' => 'bar',
      'another_array' => array (
        'stack' => 'overflow',
      ),
    );
    $xml = new SimpleXMLElement('<root/>');
    array_walk_recursive($test_array, array ($xml, 'addChild'));
    print $xml->asXML();
    

    Result:

    <?xml version="1.0"?>
    <root>
      <blub>bla</blub>
      <bar>foo</bar>
      <overflow>stack</overflow>
    </root>
    

    Consider $test_array as the result of processing in step 2

        
    16.07.2014 / 18:59