【问题标题】:Reading an xml data using phpQuery使用 phpQuery 读取 xml 数据
【发布时间】:2013-01-31 12:29:28
【问题描述】:

我想使用 phpQuery 从 xml 文件中读取节点..我已经尝试了下面的代码,但它不起作用..任何人都可以帮助我使用 phpQuery 解析 xml 的语法......

$file = 'demo.xml'
phpQuery::newDocumentFileXML($file, $charset = 'utf-8');

$nodeElement = pq('nodes'); 

    $node = $nodeElement->xml();
echo '<h2>Nodes:</h2>';
echo '<p>' . htmlentities( $title) . '</p>';

上面的代码不起作用.....我的语法似乎是错误的......

【问题讨论】:

  • 澄清“它不起作用”。
  • 我的意思是..“它不工作”是不够的信息。
  • 哥们,我提到语法可能是错误的,并要求正确的语法
  • 那么你的问题的症状是什么......试着解释发生了什么。你有错误吗?什么都没有?
  • 页面正在加载中

标签: php phpquery


【解决方案1】:

我在大多数事情上都使用 simplexml_load_file 为什么不使用它呢?哪里是使用 google maps api 提取 2 个位置之间的距离并将返回的“文本”值从结果 xml 存储到 $distance 变量中的示例。您可以在他们的网站上找到从 google 返回的 xml 示例。同样可以用于您的代码,而不是通过加载本地 xml 文件来请求 url。

//create request url
                    $request_url = "http://maps.googleapis.com/maps/api/distancematrix/xml?origins=".$tslocation->TransLat.",".$tslocation->TransLong."&destinations=".$sitelocation->SiteLat.",".$sitelocation->SiteLong."&sensor=false&units=metric";
                    //perform query
                    $xml = simplexml_load_file($request_url) or die("url not loading");
                    //get distance text
                    $distance = $xml->row->element->distance->text;

【讨论】:

  • 你有理由让它必须在 phpquery 中吗?您的 apache error.log 文件中是否有任何错误说明您目前拥有的内容无法正常工作的原因?
  • phpQuery 因为它支持 DOM 模型......如果我想从 xml 中的指定类中检索特定数据,phpQuery 可以做到
  • simple xml 支持 xml dom 就好了。据我了解,phpquery 只是用于访问 Web 客户端 dom 的 jquery 的 php 实现。没有什么是普通的 jquery + 简单的 xml 在更少的代码和更少的问题中无法实现的。
猜你喜欢
  • 1970-01-01
  • 2020-09-17
  • 1970-01-01
  • 2011-05-18
  • 1970-01-01
  • 1970-01-01
  • 2012-01-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多