节点中包含的PHP XML回显值 [英] PHP XML echoing values contained in a node

查看:174
本文介绍了节点中包含的PHP XML回显值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个XML文档,其中有3个文章节点,每个文章包含:
标题
图像
链接



我需要能够从节点中获取标题/图像/链接值。

  $ query = $ xpath-> ;查询( '//部/物品'); 

foreach($ query as $ currentArticle => $ artContents):
print_r($ artContents-> title);
endforeach

;
这对我来说不起作用,我可以使用 - > NodeName和nodeValue,但是它们不会够深入,只能分别显示'article'或者全部内容。



解释更多:



我的XML是

 <物品> 
< title>标题< / title>
< link> http://< / link>
< img> image src< / img>
< / article>

,我需要的输出是:



标题 - >标题



链接 - > http://



等。
$ b

更新以解释:

  foreach($ query as $ articles):
foreach($ articles-> childNodes as $ childNode){

if($ childNode-> nodeType === XML_ELEMENT_NODE){

$ stored = $ childNode-> ;的nodeValue;

array_push($ availAds,$ stored);

}
}

endforeach;



是我现在感谢Gordon的。这使我的阵列看起来像:

  //以前的值:

}
[ 1300884672_071.jpg] =>
array(3){
[image] =>
string(30)1300884672_071.jpg
[title] =>
string(6)secind title
[link] =>
string(10)grtgrtgrtg
}
[1300884618_071.jpg] =>
array(3){
[image] =>
string(30)1300884618_071.jpg
[title] =>
string(5)first title
[link] =>
string(10)http://www.google.com
}

//更新值
[0] =>
string(6)我的标题
[1] =>
string(89)/1300961550.jpg
[2] =>
string(22)rtherherhgerg thursada
[3] =>
string(20)custome 222222222222

我显然需要我的数组来保持一致,但是无法解决如何做到这一点。谢谢你的耐心。



Bob

解决方案

我还不知道什么问题是,但是你正在寻找

  foreach($ query as $ articles):
foreach($ articles-> ; childNodes as $ childNode){
if($ childNode-> nodeType === XML_ELEMENT_NODE){
printf(%s->%s%s,$ childNode-> nodeName, $ childNode-> nodeValue,PHP_EOL);
}
}
}

这将遍历所有DOMElement节点是查询中返回的$ article节点的直接子节点,并以这种格式打印出nodename-> nodevalue和换行符。


I have a XML doc , with 3 article nodes in it , each Article contains : Title Image Link

I need to be able to get the Title/Image/Link values out of the node.

$query = $xpath->query('//section/article');

foreach($query as $currentArticle => $artContents):
print_r( $artContents->title);
endforeach

; This doesn't work for me, I can use ->NodeName and nodeValue , but they dont drill down enough , just display 'article' or the whole contents respectively.

To explain more:

My XML is

<article>
<title>Title </title>
<link>http:// </link>
<img>image src </img>
</article>

and the output I require is :

Title->Title

Link->http://

etc.

Update to explain :

foreach($query as $articles):
foreach($articles->childNodes as $childNode) {

    if ($childNode->nodeType === XML_ELEMENT_NODE) {

                  $stored =    $childNode->nodeValue;

                    array_push( $availAds, $stored );           

    }
}

endforeach;

is what I currently have thanks to Gordon . This though makes my array look like:

//previous values:

      }
  ["1300884672_071.jpg"]=>
  array(3) {
    ["image"]=>
    string(30) "1300884672_071.jpg"
    ["title"]=>
    string(6) "secind title"
    ["link"]=>
    string(10) "grtgrtgrtg"
  }
  ["1300884618_071.jpg"]=>
  array(3) {
    ["image"]=>
    string(30) "1300884618_071.jpg"
    ["title"]=>
    string(5) "first title"
    ["link"]=>
    string(10) "http://www.google.com"
  }

//updated values that 
  [0]=>
  string(6) "My Title"
  [1]=>
  string(89) "/1300961550.jpg"
  [2]=>
  string(22) "rtherherhgerg thursada"
  [3]=>
  string(20) "custome 222222222222"

I obviously need my array to be consistent, but cannot work out how to do that. Thanks for your patience.

Bob

解决方案

I'm still not sure what the problem is, but are you looking for

foreach($query as $articles):
    foreach($articles->childNodes as $childNode) {
        if ($childNode->nodeType === XML_ELEMENT_NODE) {
            printf("%s->%s%s", $childNode->nodeName, $childNode->nodeValue, PHP_EOL);
        }
    }
}

This will iterate over all the DOMElement nodes that are direct children of the $article nodes returned in the query and print them in this format "nodename->nodevalue" and a newline.

这篇关于节点中包含的PHP XML回显值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆