2

I have a bunch of XML files in the storage/xml directory.

I want to get all the files from that directory and then parse each one. I am trying to use simple_xml to do this but I am getting various errors as seen below.

Attempt #1:

$files = Storage::files('xml');  //get all files in the dir
foreach($files as $file){
    $xml = simplexml_load_file($file);
}

This gets us:

simplexml_load_file(): I/O warning : failed to load external entity "xml/srml-21-2010-f339048-matchresults.xml"

However, the file xml/srml-21-2010-f339048-matchresults.xml does exist and is a valid XML file. I have parsed it successfully with plain PHP on a different server.

Attempt #2:

$files = Storage::files('xml');  //get all files in the dir
foreach($files as $file){
    $file_contents = Storage::get($file);
    $xml = simplexml_load_string($file_contents) or die("Error: Cannot create object");
    logger()->debug($xml);
}

This doesn't throw any errors, but when I try to log the outcome, in the log file I find that logger()->debug($xml) prints only 3 empty lines. Nothing more. However, if I try logger()->debug($file_contents) I get what looks like correct data:

[2019-04-24 14:50:31] local.DEBUG: <?xml version="1.0" encoding="UTF-8"?>
<!-- Copyright 2001-2019 Opta Sportsdata Ltd. All rights reserved. -->

<!-- PRODUCTION HEADER
     produced on:        valde-jobq-a01.nexus.opta.net
     production time:    20190304T081850,751Z
     production module:  Opta::Feed::XML::Soccer::F7
-->
<SoccerFeed TimeStamp="20190304T081848+0000">
  <SoccerDocument Type="Result" detail_id="1" uID="f339048">
    <Competition uID="c21">
        etc etc

I do not want to use a library like XmlParser (or any other library). I will have no problem with parsing the data, I just need help actually getting hold of it. What am I doing wrong?

sveti petar
  • 3,637
  • 13
  • 67
  • 144
  • Try `logger()->debug($xml->asXML());` to check what is loaded. – Nigel Ren Apr 24 '19 at 14:56
  • @NigelRen That logs the actual XML correctly. However, if I try to get `$xml->SoccerDocument->attributes()`, instead of the an array like `['Type' => 'Result', 'detail_id' => 1, 'uID' => 'f339048']` I just get the string `'Result'`. I've never had this issue before with any of these XML files. It seems like I have the XML loaded correctly but for some reason the logging isn't working as expected. – sveti petar Apr 25 '19 at 08:15
  • If you `echo $xml->SoccerDocument["uID"];` do you get the right value? – Nigel Ren Apr 25 '19 at 08:17
  • @NigelRen Yes I do, I just tried that. But how can I get the logging working for whole elements/attribute lists? I guess that's the real question here. The XML gets quite complex, it's a whole soccer match with several levels of players, events, attributes etc. So easy logging would be very helpful. – sveti petar Apr 25 '19 at 08:18
  • OK - can I suggest to create a new question which assumes you can load the XML, showing the XML content and what you want to display. I'm going out for an hour or so, but I will look at it when I get back. – Nigel Ren Apr 25 '19 at 08:20
  • @NigelRen Thanks. I am going out now myself so I can create it in the afternoon. – sveti petar Apr 25 '19 at 08:22

1 Answers1

-1

I created a package that removes the pain of XML with PHP. https://github.com/mtownsend5512/xml-to-array Under the hood it uses the DOMDocument object to parse XML. It even comes with a global Laravel helper if you prefer that sort of thing.

$files = Storage::files('xml');  //get all files in the dir
foreach($files as $file) {
    $xml = xml_to_array($file);
    // or $xml = XmlToArray::convert($file);
}
Mark
  • 1,255
  • 1
  • 13
  • 25