php dom load不会加载变量?

时间:2012-09-01 15:17:33

标签: php xml dom

我之前的页面工作正常,直到我试图将其切换为从变量加载xml文件。现在它只显示没有信息。我要做的是制作一个包含在“xml”文件夹中找到的所有文件的下拉框列表,然后能够在列表框中选择其中一个选项并扫描该文件并显示其信息。

XML文件:

<?xml version='1.0' encoding='utf-8'?>
<calibredb>
  <record>
    <id>5055</id>
    <uuid>83885ffc-93d8-41ba-aee2-e5c0ae48fc68</uuid>
    <publisher>Now Comics</publisher>
    <size>5803436</size>
    <title sort="Terminator - The Burning Earth 5, The">The Terminator - The Burning Earth 5</title>
    <authors sort="Unknown">
      <author>Unknown</author>
    </authors>
    <timestamp>2012-05-13T19:38:03-07:00</timestamp>
    <pubdate>2012-05-13T19:38:03-07:00</pubdate>
    <series index="5.0">The Terminator: The Burning Earth</series>
    <cover>M:/Comics/Unknown/The Terminator - The Burning Earth 5 (5055)/cover.jpg</cover>
    <formats>
      <format>M:/Comics/Unknown/The Terminator - The Burning Earth 5 (5055)/The Terminator - The Burning Earth 5 - Unknown.cbr</format>
    </formats>
  </record>
</clibredb>

代码:

if (isset($_POST['xml']) && $_POST['xml'] != "") {
$loc = $_POST['xml'];
$dom = new DOMDocument();  
$dom->load($loc);  
foreach ($dom->getElementsByTagName('record') as $e) {

$publisher = $e->getElementsByTagName('publisher')->item(0)->textContent; 
$title = $e->getElementsByTagName('title')->item(0)->textContent;    

echo 'Title: '.$title.'<br/>';
echo 'Publisher: '.$publisher.'<br/>';

} 

}

$_POST['xml']让我们说=现在Comics.xml

我应该避免在文件名中使用空格吗?

表格代码:

<form name="xmlselect" method="post" action="convertxml.php">
<select name="xml">
<?php echo getXMLFiles(); ?>
</select>
<input type="submit" value="Submit" />
</form>

我的完整代码:

<?php
include("config.php");
include("core.php");
function getXMLFiles() {
    if ($handle = opendir("E:/xampp/htdocs/sale/xml")) {
        while (false !== ($entry = readdir($handle))) {
            if ($entry == "." || $entry == "..") {

            }else{
            $name = str_replace(".xml", "", $entry);
                echo '<option value="'.$entry.'">'.$name.'</option>';
            }
        }
    }
}

if (isset($_REQUEST['xml']) && $_POST['xml'] != "") {
$loc = $_POST['xml'];
$dom = new DOMDocument();  
$dom->load($loc);  
foreach ($dom->getElementsByTagName('record') as $e) {

$publisher = $e->getElementsByTagName('publisher')->item(0)->textContent; 
$title = $e->getElementsByTagName('title')->item(0)->textContent;    

echo 'Title: '.$title.'<br/>';
echo 'Publisher: '.$publisher.'<br/>';



} 

}
?> 

1 个答案:

答案 0 :(得分:1)

答案是先将帖子转换为变量,然后我需要在文件中包含确切的位置。

$file = $_POST['xml'];
$loc = 'E:/xampp/htdocs/sale/xml/'.$file;
$dom = new DOMDocument();  
$dom->load($loc);