dreamice2013 2014-06-15 08:38
浏览 156
已采纳

获取元素节点等于string的xml结果

ok, xml file looks like this, it is set to a variable $otherdata

<result>
 <sighting>
  <name>Johhny</name>
  <last>smith</last>
  <phone>5551234</phone>
 </sighting>
 </result>

and php code looks like this

$dom = new DOMDocument;
$dom ->load($otherdata);
$xpath = new DomXpath($dom);

$query = '//result/sighting[name = "Johhny"]/.';
$entries = $xpath->query($query);

foreach ($entries as $entry) {
 $newlat = $entry->textContent;

 echo $newlat
 }

where I am running into trouble is trying to get the value in the 'last' and 'phone' attribute and set it equal to variable to store and echo later...thanks

  • 写回答

2条回答 默认 最新

  • doumao6212 2014-06-15 08:42
    关注

    You could use

    $query = '//result/sighting[name = "Johhny"]';
    

    as the path as that way you directly select the sighting element(s). Then you can read out the contents and change it with

    foreach ($entries as $entry) {
     $last = $entry->getElementsByTagName('last')->item(0)->textContent;
     $entry->getElementsByTagName('name')->textContent = $newName;
     }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序