从链接将 XML 文件加载到 PHP 中

发布于 2024-10-27 05:06:42 字数 184 浏览 2 评论 0原文

我的网站上的 div 中显示了 XML 文件的链接列表:

File1.xml
文件2.xml
文件3.xml
等等...

我有一个单独的 div 来以我喜欢的格式显示解析后的 XML。解析是用PHP 完成的。我想通过单击单个文件的链接将文件内容加载到 PHP 文件中进行解析。有办法做到这一点吗?

I have a list of links to XML files displayed in a div on my site:

File1.xml
File2.xml
File3.xml
etc...

And I have a separate div to display the parsed XML in a formatting of my liking. The parsing is done with PHP. I want to load the file contents into the PHP file for parsing by clicking the link to the individual file. Is there a way to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

隱形的亼 2024-11-03 05:06:42

您可以使用 $_GET 单独加载每个文件。

mysite.com/index.php?file=file1.xml

然后使用某种解析器,您解析的文件将是 get 变量中的文件。

you can use $_GET to load each file individually.

mysite.com/index.php?file=file1.xml

then use some sort of parser where the file you parse will be that one that is in the get variable.

凉墨 2024-11-03 05:06:42

需要记住的是,PHP 在服务器上运行,而 HTML 和显示在客户端(浏览器)上运行,因此任何时候您想要来回传递信息(在本例中,是要解析的文件名,以及解析和格式化的文档)您需要转到单独的页面,或者使用 AJAX 异步执行请求。

我将让您自行解析和格式化 XML,但是您认为合适,但这应该可以帮助您开始:

<?php
$file = $_GET['file'];
if (!empty($file)) {
    $xml = load_xml($file);
    $parsed = parse_xml($xml);
    $formatted = format_xml($parsed);
} else {
    $formatted = "";
}
?>
<html>
    <head><title>Sample XML Picker</title></head>
    <body>
    <ul class="list_of_documents">
        <li><a href="<?php echo $_SERVER['PHP_SELF']?>?file=File1.xml">File1.xml</a></li>
        ...
    </ul>
    <div class="results">
        <?php echo $formatted ?>
    </div>
</html>

它链接到自身,但带有一个指示要加载哪个文件的查询字符串。首先,我们检查该文件变量是否已设置,如果是,则继续加载、解析和格式化 xml。请注意,这些函数只是供您使用您选择的任何方法填写的存根。

The thing to keep in mind is that PHP runs on the server, where as the HTML and display runs on the client (browser), so anytime you want to pass information back and forth (in this case, the file name to parse, and the parsed and formatted document) you need to either go to a separate page, or use AJAX to do the request asynchronously.

I'll leave it up to you to parse and format the XML however you see fit, but this should get you started:

<?php
$file = $_GET['file'];
if (!empty($file)) {
    $xml = load_xml($file);
    $parsed = parse_xml($xml);
    $formatted = format_xml($parsed);
} else {
    $formatted = "";
}
?>
<html>
    <head><title>Sample XML Picker</title></head>
    <body>
    <ul class="list_of_documents">
        <li><a href="<?php echo $_SERVER['PHP_SELF']?>?file=File1.xml">File1.xml</a></li>
        ...
    </ul>
    <div class="results">
        <?php echo $formatted ?>
    </div>
</html>

This links to itself, but with a query string indicating which file to load. At the very beginning, we check to see if that file variable is set, and if it is, go ahead and load, parse, and format the xml. Note that those functions are just stubs for you to fill in with whatever method you choose.

累赘 2024-11-03 05:06:42

感谢那些花时间回答的人。这是我最终用来创建链接并在 iframe 中显示解析后的 XML 的方法:

<?php
$dirname = "Directory";
$dir = opendir($dirname);
while (false != ($file = readdir($dir))) {
    if (($file != ".") and ($file != "..")) {
        $filename = current(explode(".", $file));
        echo("<a href='Iframe.php?file=$dirname/$file' target='iframe'>$filename</a> <br />");
    }
}
?>
</div>

<div>
<iframe name="iframe" width="400" height="640" style="background: white;"
frameborder="1">

在 iframe 中加载的 PHP 文件 (Iframe.PHP)

<?php

$story = $_GET[file];
$xml = simplexml_load_file($story);
echo $xml->getName() . "<br />";

foreach($xml->children() as $child) {
    echo $child->getName() . ": " . $child . "<br />";
}
?> 

</body>
</html>
</iframe>
<div>

Thank you to those who took the time to answer. This is the method I finally used to create the links and display the parsed XML in an iframe:

<?php
$dirname = "Directory";
$dir = opendir($dirname);
while (false != ($file = readdir($dir))) {
    if (($file != ".") and ($file != "..")) {
        $filename = current(explode(".", $file));
        echo("<a href='Iframe.php?file=$dirname/$file' target='iframe'>$filename</a> <br />");
    }
}
?>
</div>

<div>
<iframe name="iframe" width="400" height="640" style="background: white;"
frameborder="1">

The PHP file loaded in the iframe (Iframe.PHP)

<?php

$story = $_GET[file];
$xml = simplexml_load_file($story);
echo $xml->getName() . "<br />";

foreach($xml->children() as $child) {
    echo $child->getName() . ": " . $child . "<br />";
}
?> 

</body>
</html>
</iframe>
<div>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文