我可以用php包含xml文件中的元素吗?
我有一个列表,由如下链接组成:我可以用php包含xml文件中的元素吗?,php,xml,post,dynamic,Php,Xml,Post,Dynamic,我有一个列表,由如下链接组成: <a href=index.php?p=page_1>Page 1</a> <a href=index.php?p=page_2>Page 2</a> <a href=index.php?p=page_3>Page 3</a> <program> <item> <date>27/8</date> <
<a href=index.php?p=page_1>Page 1</a>
<a href=index.php?p=page_2>Page 2</a>
<a href=index.php?p=page_3>Page 3</a>
<program>
<item>
<date>27/8</date>
<title>Page 1</title>
<info>This is info text</info>
</item>
<item>
<date>3/9</date>
<title>Page 2</title>
<info>This is info text again</info>
</item>
<item>
<date>10/9</date>
<title>Page 3</title>
<info>This just some info</info>
</item>
</program>
$xmlString = file_get_contents("path/to/xml/file.xml");
$xml = new SimpleXMLElement($xmlString);
$results = $xml->xpath("/program/item/date");
$dates = array();
if (!empty($results)) {
foreach ($results as $date) {
array_push($dates, (string) $date); // It's important to typecast from SimpleXMLElement to string here
}
}
单击后,由于以下脚本,我的页面上包含一个页面(page_1.inc.php或page_2.inc.php或page_3.inc.php):
<?php
$pages_dir = 'pages';
if(!empty($_GET['p'])){
$pages = scandir($pages_dir, 0);
unset($pages[0], $pages[1]);
$p = $_GET['p'];
if (in_array($p.'.inc.php', $pages)){
include ($pages_dir.'/'.$p.'.inc.php');
}
else {
echo 'Sorry, could not find the page!';
}
}
else {
include($pages_dir.'/home.inc.php');
}
?>
句号。
我还有一个如下所示的xml文件:
<a href=index.php?p=page_1>Page 1</a>
<a href=index.php?p=page_2>Page 2</a>
<a href=index.php?p=page_3>Page 3</a>
<program>
<item>
<date>27/8</date>
<title>Page 1</title>
<info>This is info text</info>
</item>
<item>
<date>3/9</date>
<title>Page 2</title>
<info>This is info text again</info>
</item>
<item>
<date>10/9</date>
<title>Page 3</title>
<info>This just some info</info>
</item>
</program>
$xmlString = file_get_contents("path/to/xml/file.xml");
$xml = new SimpleXMLElement($xmlString);
$results = $xml->xpath("/program/item/date");
$dates = array();
if (!empty($results)) {
foreach ($results as $date) {
array_push($dates, (string) $date); // It's important to typecast from SimpleXMLElement to string here
}
}
27/8
第1页
这是信息文本
3/9
第2页
这又是信息文本
10/9
第3页
这只是一些信息
这就是我想要实现的目标:如果我点击链接“第1页”,页面上将显示“这是信息文本”。
如果我点击链接“第2页”,它将在页面上显示“这又是信息文本”。
如果我点击链接“第3页”,页面上将显示“这只是一些信息”。
我够清楚了吗?
有什么解决办法吗?您应该可以使用该方法来实现这一点 更新: 要获取所有日期的数组,请执行以下操作:
<a href=index.php?p=page_1>Page 1</a>
<a href=index.php?p=page_2>Page 2</a>
<a href=index.php?p=page_3>Page 3</a>
<program>
<item>
<date>27/8</date>
<title>Page 1</title>
<info>This is info text</info>
</item>
<item>
<date>3/9</date>
<title>Page 2</title>
<info>This is info text again</info>
</item>
<item>
<date>10/9</date>
<title>Page 3</title>
<info>This just some info</info>
</item>
</program>
$xmlString = file_get_contents("path/to/xml/file.xml");
$xml = new SimpleXMLElement($xmlString);
$results = $xml->xpath("/program/item/date");
$dates = array();
if (!empty($results)) {
foreach ($results as $date) {
array_push($dates, (string) $date); // It's important to typecast from SimpleXMLElement to string here
}
}
此外,如果需要,还可以结合第一个和第二个示例中的逻辑。您可以对多个XPath查询重用$xml
对象
如果需要
$dates
唯一,可以在执行数组推送()之前在数组中添加
检查,也可以在foreach之后使用数组唯一()
。确定。。。因为我是php新手,我想学习它,而不仅仅是使用它,让我们看看我是否理解!前两行使用SimpleXMLElement函数读取xml文件的内容,对吗?第三行设置$info,将元素“info”作为“item”的字符串读取,其中“title”表示$page。最后一行打印$info。“我说得对吗?”林德94别担心。我曾经是php新手。是的,前两行你是对的。第三行是使用XPath(我建议您仔细阅读)扫描$XML
变量中包含的XML。xpath查询正在向下过滤到我们关心的XML节点。它从程序开始
,然后转到所有项目
,然后限制返回的项目
,这些项目有一个带有文本“第1页”、“第2页”等的节点标题
。最后,它在该上下文中选择信息
节点。info
节点中的文本作为数组返回到$info
中,我们只选择第一个索引(只有一个结果)。