0

I have a directory called incoming_folder in which there are some xml files (36017P.xml, 36031P.xml, and hello.xml) in it.

<?php
$src_dir    = 'incoming_folder';  /* Place where xml files are present */
$xml_files = preg_grep('~\.(xml)$~', scandir($src_dir));
print_r($xml_files);              /* Line#A */

Line#A display the following o/p:

Array ( [3] => 36017P.xml [5] => 36031P.xml [7] => hello.xml )

$xml=simplexml_load_file("incoming_folder") or die('Unable to load XML');                                      

$path_program_en = $xml->xpath('//StringAssetInfo/attrName[text()="CASE_SERIES_TITLE"]/..');
$path_title_en = $xml->xpath('//StringAssetInfo/attrName[text()="CASE_EPISODE_TITLE"]/..');
$path_description_en = $xml->xpath('//TextAssetInfo/attrName[text()="CASE_DESCRIPTION_ENGLISH"]/..');
?>

Problem Statement:

I am wondering what changes I should make in the php code above so that it pull the sub-elements CASE_SERIES_TITLE, CASE_EPISODE_TITLE, and CASE_DESCRIPTION_ENGLISH values from their respective xmls 36017P.xml, 36031P.xml, and hello.xml and parse it in the table rows.

Program (EN) Title (EN) Description (EN)

CASE_SERIES_TITLE, CASE_EPISODE_TITLE, and CASE_DESCRIPTION_ENGLISH sub-elements are present in every xml (36017P.xml, 36031P.xml, and hello.xml)

<tr>
    <th style="width:8%;" >Program (EN)</th>
    <th style="width:8%;" >Title (EN)</th>
    <th style="width:8%;" >Description (EN)</th>
</tr>
    <td style="width:8%; text-align:center;"><?php echo $path_program_en; ?></td>
    <td style="width:8%; text-align:center;"><?php echo $path_title_en;  ?></td>
    <td style="width:8%; text-align:center;"><?php echo $path_description_en; ?></td>
</tr>

The snippet of content from 36017P.xml is:

<StringAssetInfo>
   <attrName>CASE_SERIES_TITLE</attrName>
   <attrTagName>CASE_SERIES_TITLE</attrTagName>
   <value>PrimeTime Politics</value>
</StringAssetInfo>
8
  • Can you add a sample XML file contents to the question, it can help to make the result more reliable. Commented May 17, 2019 at 16:24
  • @NigelRen Added. Please have a look. Commented May 17, 2019 at 16:25
  • Is that sample data complete or are there repeats of this data with another level above this? Commented May 17, 2019 at 16:30
  • There are no repeats for that <attrName>CASE_SERIES_TITLE</attrName>, <attrTagName>CASE_SERIES_TITLE</attrTagName> and <value>PrimeTime Politics</value>. It is unique in the whole xml. Commented May 17, 2019 at 16:31
  • OK - but are there levels of the XML above these (i.e. a root node) are these grouped under another node? Commented May 17, 2019 at 16:34

1 Answer 1

1

This code builds up a list of the data extracted from each file, so after the loop $programs contains each files information.

I've modified the XPath expressions to make them easier to use and as it is possible that any item may be missing (you can remove this bit if you are sure they will be there) it uses

(string)($path_program_en[0]??"")

So the ?? bit will make sure there is some data to use and the (string) makes sure it's a string (and not a SimpleXMLElement).

Once built up, again another loop to build the table up...

$programs = [];
foreach ( $xml_files as $file ) {
    $xml = simplexml_load_file($file);

    $path_program_en = $xml->xpath('//StringAssetInfo[attrName="CPAC_SERIES_TITLE"]/value');
    $path_title_en = $xml->xpath('//StringAssetInfo[attrName="CPAC_EPISODE_TITLE"]/value');
    $path_description_en = $xml->xpath('//TextAssetInfo[attrName="CPAC_DESCRIPTION_ENGLISH"]/value');

    $programs[] = [ "series_title" => (string)($path_program_en[0]??""), 
        "episode_title" => (string)($path_title_en[0]??""), 
        "description" => (string)($path_description_en[0]??"")];
}

echo '<tr>
<th style="width:8%;" >Program (EN)</th>
<th style="width:8%;" >Title (EN)</th>
<th style="width:8%;" >Description (EN)</th>
</tr>';

foreach ( $programs as $program)    {
    echo '<tr>
             <td style="width:8%; text-align:center;">'.$program["series_title"].'</td>
             <td style="width:8%; text-align:center;">'.$program["episode_title"].'</td>
            <td style="width:8%; text-align:center;">'.$program["description"].'</td>
        </tr>';
}

Note: Please make sure the element names are correct - as I couldn't find CASE_SERIES_TITLE in the sample XML you have.

Edit:

For older versions of PHP use..

$programs = array();
foreach ( $xml_files as $file ) {
    $xml = simplexml_load_file($file);

    $path_program_en = $xml->xpath('//StringAssetInfo[attrName="CPAC_SERIES_TITLE"]/value');
    $path_title_en = $xml->xpath('//StringAssetInfo[attrName="CPAC_EPISODE_TITLE"]/value');
    $path_description_en = $xml->xpath('//TextAssetInfo[attrName="CPAC_DESCRIPTION_ENGLISH"]/value');

    $path_program_en = isset($path_program_en[0])?$path_program_en[0]:"";
    $path_title_en = isset($path_title_en[0])?$path_title_en[0]:"";
    $path_description_en = isset($path_description_en[0])?$path_description_en[0]:"";

    $programs[] = array( "series_title" => (string)$path_description_en, 
        "episode_title" => (string)$path_title_en, 
        "description" => (string)$path_description_en);
}
Sign up to request clarification or add additional context in comments.

16 Comments

Just checking whats inside $xml_files ?
Should I be doing something like this $xm_files=simplexml_load_file("incoming_folder") or die('Unable to load XML');
$xml_files is the result of your preg_grep
Looks as though you need to add $src_dir, so $xml = simplexml_load_file($src_dir."/".$file);
Changed the syntax to $programs[] = array( "seri (check the updated answer for the full line)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.