我如何解析xml页面以其所需的方式输出其数据? [英] how do i parse an xml page to output its data pieces to the way i want?

查看:80
本文介绍了我如何解析xml页面以其所需的方式输出其数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我要解析的页面 (我给出的api链接只是一个开发测试,因此可以公开) http://api.scribd.com/api?method=docs. getList& api_key = 2apz5npsqin3cjlbj0s6m

here is the page i want to parse (the api link i gave is just a dev test so its ok to be public) http://api.scribd.com/api?method=docs.getList&api_key=2apz5npsqin3cjlbj0s6m

我正在寻找的输出是这样的(目前)

the output im looking for is something like this (for now)

 Doc_id: 29638658
 access_key: key-11fg37gwmer54ssq56l3
 secret_password: 1trinfqri6cnv3gf6rnl
 title: Sample
 description: k
 thumbnail_url: http://i6.scribdassets.com/public/images/uploaded/152418747/xTkjCwQaGf_thumbnail.jpeg
 page_count: 100

ive尝试了我可以在互联网上找到的所有内容,但效果不佳.我有这个剧本

ive tried everything i can find on the internet but nothing works good. i have this one script

<?php
$xmlDoc = new DOMDocument();
$xmlDoc->load("http://api.scribd.com/api?method=docs.getList&api_key=2apz5npsqin3cjlbj0s6m");

$x = $xmlDoc->documentElement;
foreach ($x->childNodes AS $item) {
  print $item->nodeName . " = " . $item->nodeValue;
 }



 ?>

其输出如下所示:

#text = 
  resultset = 

      29638658
      key-11fg37gwmer54ssq56l3
      1trinfqri6cnv3gf6rnl

        Sample


        k

      http://i6.scribdassets.com/public/images/uploaded/152418747/xTkjCwQaGf_thumbnail.jpeg

        DONE

      100


      29713260
      key-18a9xret4jf02129vlw8
      25fjsmmvl62l4cbwd1vq

        book2


        description bla bla 

      http://i6.scribdassets.com/public/images/uploaded/153065528/oLVqPZMu3zhsOn_thumbnail.jpeg

        DONE

      7

  #text = 

我需要重大帮助,即时通讯真的卡住了,不知道该怎么办.请帮帮我. thnx

i need major help im really stuck and dont know what to do. please please help me. thnx

推荐答案

我建议您将XML数据加载到新的

I recommend you load the XML data into a new SimpleXmlElement object as this will allow you to run xpath queries against the document.

您将需要对其工作方式进行一些研究,但是这里有一些提示...

You will need to do a little research on how it works, but here's a few pointers...

像这样执行xpath:

Execute the xpath like so:

// $xml is a SimpleXMLElement object
$xml = simplexml_load_file('/path/to/file');
$nodes = $xml->xpath('/xpathquery');

单个/表示根节点(在您的情况下为rsp).双斜杠代表任何匹配的节点.例如//title将返回所有标题. xpath查询的每个结果都是一个SimpleXMLElements数组.您可以像这样从中获取数据:

A single / represents the root node (in your case rsp). A double slash represents any matching node. For example //title would return all titles. Each result of an xpath query is an array of SimpleXMLElements. You can get data from it like so:

# Untested        
$xml = simplexml_load_file('/path/to/file');
$nodes = $xml->xpath('//result');

foreach ($result as $node) {
    // Print out the value in title
    echo $node->title;
}

// Print out the amount of results
echo $xml->rsp->attributes()->totalResultsAvailable;

最后一个带有结果编号的示例可能无法正常工作,但确实符合这些原则.

The final example with the results numbers may not work, but it is along those lines.

这篇关于我如何解析xml页面以其所需的方式输出其数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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