用php查询xpath

用php查询xpath,php,xpath,Php,Xpath,我编写了以下php代码来从该xml中提取节点信息: <sioctBoardPost rdfabout="http//boards.ie/vbulletin/showpost.php?p=67075"> <rdftype rdfresource="http//rdfs.org/sioc/ns#Post" /> <dctitle>hib team</dctitle> <siochas_creator> <siocU

我编写了以下php代码来从该xml中提取节点信息:

<sioctBoardPost rdfabout="http//boards.ie/vbulletin/showpost.php?p=67075">
  <rdftype rdfresource="http//rdfs.org/sioc/ns#Post" />
  <dctitle>hib team</dctitle>
  <siochas_creator>
    <siocUser rdfabout="http//boards.ie/vbulletin/member.php?u=497#user">
      <rdfsseeAlso rdfresource="http//boards.ie/vbulletin/sioc.php?sioc_type=user&amp;sioc_id=497" />
    </siocUser>
  </siochas_creator>
  <dctermscreated>1998-04-25T213200Z</dctermscreated>
  <sioccontent>zero, those players that are trialing 300 -400 pingers? umm..mager lagg and even worse/</sioccontent>
</sioctBoardPost>

<?php
$xml = simplexml_load_file("boards.xml");
$products[0] = $xml->xpath("/sioctBoardPost/sioccontent");
$products[1] = $xml->xpath("/sioctBoardPost/dctermscreated");
$products[2] = $xml->xpath("/sioctBoardPost/@rdfabout");
print_r($products);
  ?>
但我只需要节点内容作为输出,即不需要数组([0]=>数组等)

输出应如下所示:

zero, those players that are trialing for hib team, (hpb's) most of them are like 300 -400 pingers? umm..mager lagg and even worse when they play on uk server's i bet

1998-04-25T213200Z

http//boards.ie/vbulletin/showpost.php?p=67075

提前感谢

正如您所观察到的,
xpath()
方法返回一个匹配节点的数组,因此您需要处理返回数组的元素。我认为在这种情况下应该可以这样做:

$xml = simplexml_load_file("boards.xml");
$products[0] = $xml->xpath("/sioctBoardPost/sioccontent")[0];
$products[1] = $xml->xpath("/sioctBoardPost/dctermscreated")[0];
$products[2] = $xml->xpath("/sioctBoardPost/@rdfabout")[0];
print_r($products);
您可以使用
current()
仅获取每个XPath结果的第一个元素(即数组),然后使用
(字符串)
强制转换来获取节点内容:

$products[0] = (string)current($xml->xpath("/sioctBoardPost/sioccontent"));
$products[1] = (string)current($xml->xpath("/sioctBoardPost/dctermscreated"));
$products[2] = (string)current($xml->xpath("/sioctBoardPost/@rdfabout"));
print_r($products);

这会让你得到你需要的

foreach ($products as $product) { // iterate through the $products array
    print $product[0]->nodeValue  // output the node value of the SimpleXMLElement Object
}

你能展示你的xml文件吗?hib team 1998-04-25T213200Z zero,那些正在试用300-400个Pinger的玩家?嗯..mager lagg,甚至更糟/但我有5.3.5及以上的代码给出以下错误:解析错误:语法错误,意外'['在C:\wamp\www\tentry.php第10行,你能在你的问题中提供上面的XML吗?hib team 1998-04-25T213200Z zero,那些正在试用300-400个Pinger的玩家?嗯..mager lagg甚至更糟/谢谢你的建议,你的代码返回以下输出:数组([0]=>SimpleXMLElement对象([0]=>hib team)[1]=>SimpleXMLElement对象([0]=>zero,那些正在为hib团队(hpb)进行试用的玩家,他们中的大多数都像300-400个Pinger?嗯..mager lagg,甚至更糟糕的是,当他们在英国服务器的i bet/上玩时,[2]=>SimpleXMLElement对象([0]=>1998-04-25T213200Z)[3]=>SimpleXMLElement对象([@attributes]=>Array([rdfabout]=>http//boards.ie/vbulletin/showpost.php?p=67075)))但我不想在输出中使用单词simplexmlement对象。@MuhammadShahzadFaisal您可能需要额外的
(string)
cast。实际上我不知道如何应用(string)在这个问题上,请帮助我怎么做。@MuhammadShahzadFaisal我已经在更新的答案中向您展示了,请检查。非常感谢您,亲爱的,现在是OK谢谢您的建议,但它没有显示任何内容作为输出,出现一个空白屏幕
foreach ($products as $product) { // iterate through the $products array
    print $product[0]->nodeValue  // output the node value of the SimpleXMLElement Object
}