从链接将 XML 文件加载到 PHP 中
我的网站上的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用 $_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.
需要记住的是,PHP 在服务器上运行,而 HTML 和显示在客户端(浏览器)上运行,因此任何时候您想要来回传递信息(在本例中,是要解析的文件名,以及解析和格式化的文档)您需要转到单独的页面,或者使用 AJAX 异步执行请求。
我将让您自行解析和格式化 XML,但是您认为合适,但这应该可以帮助您开始:
它链接到自身,但带有一个指示要加载哪个文件的查询字符串。首先,我们检查该文件变量是否已设置,如果是,则继续加载、解析和格式化 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:
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.
感谢那些花时间回答的人。这是我最终用来创建链接并在 iframe 中显示解析后的 XML 的方法:
在 iframe 中加载的 PHP 文件 (Iframe.PHP)
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:
The PHP file loaded in the iframe (Iframe.PHP)