-1

I'm trying to combine two or more xml files for better use. Let's say I have two XML files and I want to combine their content before extract their data into a table.

<table id="rounded-corner">
    <tr>
            <th>Title</th>
            <th>Download Link</th>
            <th>Language</th>
    </tr>


<?php

        $xml_request_url = 'file.xml';
        $xml = new SimpleXMLElement($xml_request_url, null, true);



        foreach ( $xml->results->content as $item )
        {

        echo "


<tr><td>" . $item->movie . "</td><td><a href=" . $item->download . ">Download Link</a>    </td><td>" . $item->language . "</td></tr>
";

}

        ?> </table>

Can you give me an example of how to do it? I don't want to make tables for each XML file I have.

<find>
<base>
http://www.example.com
</base>
<results items="2" itemsfound="2">
<song>
  <downloadlink>
  http://example.com/dldlink.php?lk=43543
  </downloadlink>
  <format>
  mp3
  </format>
</song>
<song>
  <downloadlink>
  http://example.com/dldlink.php?lk=87798
  </downloadlink>
  <format>
  mp4
  </format>
 </song>
 </results>
 </find>
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
  • Check out this other [post](http://stackoverflow.com/questions/6385666/merge-two-xml-files-recursively) / possible duplicate – mseancole May 16 '12 at 13:15
  • they are simple xml, same format but different content – user1040157 May 16 '12 at 13:16
  • @showerhead: That question is not yet answered. However I know this question has been asked and answered before, the TS should really use the search for the answer. – hakre May 16 '12 at 13:18
  • why you guys vote negative the question? – user1040157 May 16 '12 at 13:22
  • 1
    This is not the same. The other post is about merging xml files, the TS wants to display the data from 2 xml files in the same table. There's a difference. – TJHeuvel May 16 '12 at 13:23
  • @hakre can you show me how to make one table for all files? – user1040157 May 16 '12 at 13:29
  • @user1040157: [That's my answer below](http://stackoverflow.com/a/10619490/367456). – hakre May 16 '12 at 13:29
  • @user1040157: I added the HTML around it, so it's probably more clear. It should be enough to demonstrate that it must not change much compared to your original code (I modified it a bit, but you can ignore that, the important part is how to create the iterator you use inside the foreach that outputs the table rows.) – hakre May 16 '12 at 13:33
  • Catchable fatal error: Argument 1 passed to AppendIterator::append() must implement interface Iterator, instance of SimpleXMLElement given in /home/ – user1040157 May 16 '12 at 13:40

2 Answers2

2

Your question actually is not asking about how to join two XML files, but more precisely how to create one iterator out of the two. That's possible by using the AppendIterator:

<table id="rounded-corner">
    <tr>
            <th>Title</th>
            <th>Download Link</th>
            <th>Language</th>
    </tr>
<?php

    $content = new AppendIterator();
    $content->append(new IteratorIterator(simplexml_load_file('file1.xml')->results->content));
    $content->append(new IteratorIterator(simplexml_load_file('file2.xml')->results->content));
    foreach($content as $item)
    {
?>
    <tr>
        <td><?= $item->movie ?></td>
        <td><a href="<?= $item->download ?>">Download Link</a></td>
        <td><?= $item->language ?></td>
    </tr>
<?php
    }
?>
</table>
hakre
  • 193,403
  • 52
  • 435
  • 836
  • Catchable fatal error: Argument 1 passed to AppendIterator::append() must implement interface Iterator, instance of SimpleXMLElement given in /home/ – user1040157 May 16 '12 at 14:00
  • Can you show me how to do it on the code you've posted, please? – user1040157 May 16 '12 at 14:08
  • @user1040157: Sure, see the edit. That works. Ensure you select the right elements from the XML, the ones here aren't those in your XML example (verify the tag names). – hakre May 16 '12 at 14:33
  • how to to query result with xpath? – Mehmet Feb 15 '17 at 13:34
  • @Mehmet you could merge both files first -or- apply the same xpath on both files and create the iterator from the xpath result (use `ArrayIterator` on the `xpath()` return value as it returns arrays) – hakre Feb 15 '17 at 22:45
0

You could store them in an intermediate array. For example:

$files = array('test1.xml','test2.xml');
$data = array();
foreach($files as $file)
{
 $xml = new SimpleXMLElement($xml_request_url, null, true);
 foreach($xml as $node)
   $data[] = $node;
}

Then use your $data array to get all the values.

TJHeuvel
  • 12,403
  • 4
  • 37
  • 46