Reading XML Files

Topics: Developer Forum
Jan 7, 2011 at 12:09 AM

Can PHPExcel read an xml file and create a csv or xls file from it, maybe with IOFactory?

I tried the following but it just made a csv including the entire contents of the file tags and all.

$xml = PHPExcel_IOFactory::load('test.xml');
$csv = new PHPExcel_Writer_CSV($xml);
$csv->save('test.csv');
Coordinator
Jan 7, 2011 at 8:52 AM

PHPExcel cannot guess the structure of an arbitrary XML file... even Excel doesn't do that, but requires you to create a mapping before it can read an XML file, unless it's of a known structure that it has been predefined to recognise (xlsx files are actually a set of xml files in a zip archive, and Excel2003XML likewise).

For example:

<?xml version="1.0" encoding="ISO-8859-1"?>
<log4php:configuration xmlns:log4php="http://www.vxr.it/log4php/" threshold="all" debug="false">
 <appender name="default" class="LoggerAppenderEcho">
  <layout class="LoggerPatternLayout">
   <param name="conversionPattern" value="${MY_CONSTANT} %-5p [%t] [%X{env.OS}]: %m in %F line %L%n" />
  </layout>
 </appender>
 <appender name="fileSystemTiming" class="LoggerAppenderFile">
  <param name="file" value="/usr/local/apache/logs/<%%_SERVER_NAME_%%>_<%%_CODE_VERSION_%%>_log4php_timingInfo.xml" />
  <param name="append" value="true" />
  <layout class="LoggerXmlLayout">
   <param name="locationInfo" value="true" />
  </layout>
  <filter class="LoggerLevelMatchFilter">
   <param name="LevelToMatch" value="TIMING" />
   <param name="AcceptOnMatch" value="true"/>
  </filter>
  <filter class="LoggerDenyAllFilter" />
 </appender>
 <appender name="fileSystemDatabase" class="LoggerAppenderFile">
  <param name="file" value="/usr/local/apache/logs/<%%_SERVER_NAME_%%>_<%%_CODE_VERSION_%%>_log4php_databaseInfo.xml" />
  <param name="append" value="true" />
  <layout class="LoggerXmlLayout">
   <param name="locationInfo" value="true" />
  </layout>
  <filter class="LoggerLevelMatchFilter">
   <param name="LevelToMatch" value="DATABASEQUERY" />
   <param name="AcceptOnMatch" value="true"/>
  </filter>
  <filter class="LoggerLevelMatchFilter">
   <param name="LevelToMatch" value="DATABASERESULT" />
   <param name="AcceptOnMatch" value="true"/>
  </filter>
  <filter class="LoggerDenyAllFilter" />
 </appender>
 <root>
  <level value="debug" />
  <appender_ref ref="fileSystemTiming" />
  <appender_ref ref="fileSystemDatabase" />
 </root>
</log4php:configuration>

Is a perfectly acceptable xml file. How should that data appear if it were to be converted to a CSV file?

Jan 7, 2011 at 3:13 PM

I see your point, you're totally right. Thank you for your time and input.