首页 > 解决方案 > 从 php 中的 xml 文件中提取内容并将其解析为表行

问题描述

我有一个名为的目录incoming_folder,其中有一些 xml 文件(36017P.xml、36031P.xml 和 hello.xml)

<?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"]/..');
?>

问题陈述:

我想知道我应该在上面的 php 代码中进行哪些更改,以便它 从它们各自的 xmls 36017P.xml、36031P.xml 和 hello.xml中提取子元素CASE_SERIES_TITLE、CASE_EPISODE_TITLE 和 CASE_DESCRIPTION_ENGLISH值 并在表中解析它行。

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

每个 xml(36017P.xml、36031P.xml 和 hello.xml)中都存在CASE_SERIES_TITLE、CASE_EPISODE_TITLE 和 CASE_DESCRIPTION_ENGLISH子元素

<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>

内容片段来自36017P.xml

<StringAssetInfo>
   <attrName>CASE_SERIES_TITLE</attrName>
   <attrTagName>CASE_SERIES_TITLE</attrTagName>
   <value>PrimeTime Politics</value>
</StringAssetInfo>

标签: phpxml

解决方案


此代码构建从每个文件中提取的数据列表,因此在循环之后$programs包含每个文件的信息。

我已经修改了 XPath 表达式以使它们更易于使用,并且任何项目都可能丢失(如果你确定它们会在那里,你可以删除这个位)它使用

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

因此,该??位将确保有一些数据要使用,并(string)确保它是一个字符串(而不是 SimpleXMLElement)。

一旦建立起来,再次建立另一个循环来建立表格......

$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>';
}

注意:请确保元素名称正确 - 因为我在您拥有的示例 XML 中找不到CASE_SERIES_TITLE

编辑:

对于旧版本的 PHP 使用..

$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);
}

推荐阅读