将 XML 读入 PHP
我正在尝试确定显示我正在从事的项目的数据的最佳操作方案。我的客户目前正在使用专门用于管理房地产的专有 CMS。它可以轻松地让您添加属性、平方英尺、价格、位置等。运行此 CMS 的公司以非常简单的 XML 文件提供数据,他们说可以访问我的客户输入的所有数据。
我已经阅读了 PHP5 的 SimpleXML 功能,并且很好地掌握了基本概念,但我的问题是:我可以像查询 MySQL 数据库一样以类似的方式访问 XML 数据吗?
例如,假设每个条目都有一个唯一的 ID,我是否能够设置一个视图并使用 URL 变量仅显示该记录,例如: http://example.com/apartment.php?id=14
您还可以根据字符串中的值显示结果吗?我正在考虑在这种情况下提交的表单仅返回两间卧室的房产。
如果这是一个菜鸟问题,请提前抱歉。如果没有其他原因,我宁愿不为我的客户构建自定义 CMS,而他们只需登录一个位置并进行相应更新。
I'm trying to determine the best course of action for the display of data for a project I'm working on. My client is currently using a proprietary CMS geared towards managing real estate. It easily lets you add properties, square footage, price, location, etc. The company that runs this CMS provides the data in a pretty straightforward XML file that they say offers access to all of the data my client enters.
I've read up on PHP5's SimpleXML feature and I grasp the basic concepts well enough, but my question is: can I access the XML data in a similar fashion as if I were querying a MySQL database?
For instance, assuming each entry has a unique ID, will I be able to set up a view and display just that record using a URL variable like: http://example.com/apartment.php?id=14
Can you also display results based on values within strings? I'm thinking a form submit that returns only two bedroom properties in this case.
Sorry in advance if this is a noob question. I'd rather not build a custom CMS for my client if for no other reason than they'd only have to login to one location and update accordingly.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对您的问题的一些简短回答:
一个。是的,您可以通过查询访问 XML 数据,但使用 XPath 而不是 SQL。 XPath 用于 XML 就像 SQL 用于数据库一样,工作方式完全不同。
b.是的,您可以构建一个 php 程序,该程序接收 id 作为参数,并使用它对给定的 XML 文件进行 XPath 搜索。
c. XML文件中的所有数据都是字符串,因此搜索或显示字符串是没有问题的。即使您的示例 id=14 也将作为字符串处理。
您可能对此进一步信息感兴趣:
http://www.ibm.com/developerworks /library/x-simplexml.html?S_TACT=105AGX06&S_CMP=LP
http://www.ibm.com/developerworks /library/x-xmlphp1.html?S_TACT=105AGX06&S_CMP=LP
PHP 不仅可以通过 SimpleXML 访问 XML,还可以通过 DOM 访问 XML。 SimpleXML 访问 PHP 数组等元素,DOM 提供与 w3c-DOM 兼容的 api。
请参阅 php.net 了解访问 XML 的其他方法,但它们似乎不适合您。
Some short answers on your questions:
a. Yes you can access XML data with queries, but using XPath instead of SQL. XPath is for XML what SQL is for databases, working quite different.
b. Yes you can build a php program that receives an id as parameter and uses this for an XPath search on a given XML file.
c. All data in a XML file is a string, so it is no problem to search for or display strings. Even your example id=14 is to handle as a string.
You might be interested in this further information:
http://www.ibm.com/developerworks/library/x-simplexml.html?S_TACT=105AGX06&S_CMP=LP
http://www.ibm.com/developerworks/library/x-xmlphp1.html?S_TACT=105AGX06&S_CMP=LP
PHP can access XML not only via SimpleXML but also with DOM. SimpleXML accesses the elements like PHP-arrays, DOM provides a w3c-DOM-compatible api.
See php.net for other ways to access XML, but they seem not to be appropriate for you.