从php中的xml文件中提取内容并将其解析为表行
我有一个名为从php中的xml文件中提取内容并将其解析为表行,php,xml,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
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"]/..');
?>
36017P.xml
中的内容片段是:
<StringAssetInfo>
<attrName>CASE_SERIES_TITLE</attrName>
<attrTagName>CASE_SERIES_TITLE</attrTagName>
<value>PrimeTime Politics</value>
</StringAssetInfo>
案例系列标题
案例系列标题
黄金时段政治
此代码建立了从每个文件提取的数据列表,因此在循环之后,$programs
包含每个文件的信息
我已经修改了XPath表达式,以使它们更易于使用,因为它可能会丢失任何项(如果您确信它们会在那里,您可以删除此位)
因此,?
位将确保有一些数据要使用,(字符串)
确保它是一个字符串(而不是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文件内容样本吗?这有助于使结果更可靠。”奈杰伦补充道。请看一看。该样本数据是否完整,或者该数据是否存在高于此级别的重复?该
案例系列标题
、案例系列标题
和黄金时段政治
没有重复。它在整个xml中是唯一的。好的-但是在这些(即根节点)之上有xml的级别吗?这些是分组在另一个节点下的吗?只是检查$xml\u文件中的内容$xm\u文件=simplexml\u加载文件(“传入的\u文件夹”)还是死(“无法加载xml”)
$xml\u文件
是您的预处理的结果
看起来好像需要添加$src\u dir
,所以$xml=simplexml\u加载文件($src\u dir./“$file)
将语法更改为$programs[]=array(“seri
(检查更新的答案以了解完整行)
(string)($path_program_en[0]??"")
$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>';
}
$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);
}