如何从该 xml 对象中提取值?

发布于 2024-10-12 06:07:47 字数 1108 浏览 4 评论 0原文

object(SimpleXMLElement)#1 (3) { 
["@attributes"]=> array(1) { 
   ["responsecode"]=> string(3) "200" 
} 
["nextpage"]=> object(SimpleXMLElement)#2 (0) { 
} 
["resultset_web"]=> object(SimpleXMLElement)#3 (2) { 
   ["@attributes"]=> array(4) { 
      ["count"]=> string(2) "10" 
      ["start"]=> string(1) "0" 
      ["totalhits"]=> string(8) "22497060" 
      ["deephits"]=> string(8) "23000000" 
   } 
   ["result"]=> array(10) { 
      [0]=> object(SimpleXMLElement)#4 (7) { 
         ["abstract"]=> string(110) "MSN's all-in-one Internet portal, the home of Hotmail, MSN Messenger, MSNBC News, Encarta, and Slate Magazine." 
         ["clickurl"]=> string(360) "http://teqpad.com/www/msn.com" 
         ["date"]=> string(10) "2011/01/14" 
         ["dispurl"]=> object(SimpleXMLElement)#14 (0) { 
         } 
         ["size"]=> string(5) "82136" 
         ["title"]=> string(3) "MSN" 
         ["url"]=> string(19) "http://www.msn.com/" 
      } 

我想使用 php 从上面的 xml 中提取 titleabstract 的值。

object(SimpleXMLElement)#1 (3) { 
["@attributes"]=> array(1) { 
   ["responsecode"]=> string(3) "200" 
} 
["nextpage"]=> object(SimpleXMLElement)#2 (0) { 
} 
["resultset_web"]=> object(SimpleXMLElement)#3 (2) { 
   ["@attributes"]=> array(4) { 
      ["count"]=> string(2) "10" 
      ["start"]=> string(1) "0" 
      ["totalhits"]=> string(8) "22497060" 
      ["deephits"]=> string(8) "23000000" 
   } 
   ["result"]=> array(10) { 
      [0]=> object(SimpleXMLElement)#4 (7) { 
         ["abstract"]=> string(110) "MSN's all-in-one Internet portal, the home of Hotmail, MSN Messenger, MSNBC News, Encarta, and Slate Magazine." 
         ["clickurl"]=> string(360) "http://teqpad.com/www/msn.com" 
         ["date"]=> string(10) "2011/01/14" 
         ["dispurl"]=> object(SimpleXMLElement)#14 (0) { 
         } 
         ["size"]=> string(5) "82136" 
         ["title"]=> string(3) "MSN" 
         ["url"]=> string(19) "http://www.msn.com/" 
      } 

I want to extract value of title and abstract from above xml using php.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

最近可好 2024-10-19 06:07:47

您已经使用 SimpleXML 解析了它,您真正想要做的是遍历对象并找到 titleabstract 的值。

如果您的对象是 $xml,则 $xml->resultset_web->result[0]->abstract 包含 abstract
$xml->resultset_web->result[0]->title 包含一个值的 title。对于所有的值,

foreach ($xml->resultset_web->result as $v) {
  $title = $v->title;
  $abstract = $v->title;
}

You have already parsed it with SimpleXML, what you really want to do is traverse through your object and find the values of title and abstract.

If your object is $xml, $xml->resultset_web->result[0]->abstract contains abstract while
$xml->resultset_web->result[0]->title contains title for one value. For all the values,

foreach ($xml->resultset_web->result as $v) {
  $title = $v->title;
  $abstract = $v->title;
}
迎风吟唱 2024-10-19 06:07:47

据此判断,我可以看到您大约有 10 个标题可供提取:

["result"]=> array(10) { 

这样做的方法是:

foreach ($simpleXML->resultset_web->result as $result) {
    $title = $result->title;
    $abstract = $result->abstract;
}

I can see that you have about 10 titles to extract by judging from this:

["result"]=> array(10) { 

The way of doing this would be:

foreach ($simpleXML->resultset_web->result as $result) {
    $title = $result->title;
    $abstract = $result->abstract;
}
终止放荡 2024-10-19 06:07:47

这看起来像是保存 SimpleXML 对象的 var 的 var_dump。
假设您有这样的东西:

//$data i a string containing your XML
$xmlobj = new SimpleXMLElement($data);

那么您应该能够访问这样的项目:

foreach ($xmlobj->resultset_web->result as $result) {
    echo $result->abstract;
    echo $result->title;
}

That looks like a var_dump of a var that holds a SimpleXML object.
So let's say that you have something like this:

//$data i a string containing your XML
$xmlobj = new SimpleXMLElement($data);

then you should be able to access the items like this:

foreach ($xmlobj->resultset_web->result as $result) {
    echo $result->abstract;
    echo $result->title;
}
奶茶白久 2024-10-19 06:07:47

如果你想获取路径为resultset_web的所有节点->结果->标题:

$xml = [your object];
$allTitlesAsArray = $xml->xpath('/resultset_web/result/title');
$allAbstractAsArray = $xml->xpath('/resultset_web/result/abstract');

If you want to get all nodes with path resultset_web -> result -> title:

$xml = [your object];
$allTitlesAsArray = $xml->xpath('/resultset_web/result/title');
$allAbstractAsArray = $xml->xpath('/resultset_web/result/abstract');
心清如水 2024-10-19 06:07:47

未经测试,但应该可以工作:

$ob->resultset_web->result[0]->title;

正如所说,只需阅读 php simplexml 文档即可。请注意,并非每种情况都这么简单,如果您有多个结果,则必须使用 Children() 方法迭代子级,您无法像我上面那样直接访问它们。

Untested, but should work :

$ob->resultset_web->result[0]->title;

As said, just read up on the php simplexml documentation. And note that not each case will be this simple, if you have multiple results you will have to iterate through the children with the children() method, you can't access them directly as I did above.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文