xml转json,怎么筛选数据?

WBOY
Release: 2016-06-23 14:19:40
Original
1023 people have browsed it

JSON XML

下边的xml,想转为json
但P节点下只需要这两个节点的数据,其他数据不需要。
如果直接用json_encode转出来,是所有的数据
请问怎么能转为json,但只要的数据,怎么弄?

例如
{"PN":"\u7b2c\u4e00\u5468\u64ad\u5267\u573a\uff1a\u8ffd\u9c7c\u4f20\u5947 31","PT":"2013-08-19 22:01:00"}

-------xml---------


  


    110171675
    张三
    2013-08-19 00:02:00
    46
    

24
    74750
    0
    2013-08-19 00:02:00
    AM
    
  
  


    110171676
    我是大美人
    2013-08-19 01:15:00
    46
    

24
    74501
    0
    2013-08-19 01:15:00
    AM
    
  
  


    110171677
    李四
    2013-08-19 02:09:00
    46
    

24
    64519
    71411
    2013-08-19 02:09:00
    AM
    
    


回复讨论(解决方案)

$string = <<<XML<Root>  <P>    <id>110171675</id>    <PN>张三</PN>    <PT>2013-08-19 00:02:00</PT>    <Cd>46</Cd>    <Td>24</Td>    <Fd>74750</Fd>    <Fd2>0</Fd2>    <cPT>2013-08-19 00:02:00</cPT>    <AMPM>AM</AMPM>    <ls/>  </P>  <P>    <id>110171676</id>    <PN>我是大美人</PN>    <PT>2013-08-19 01:15:00</PT>    <Cd>46</Cd>    <Td>24</Td>    <Fd>74501</Fd>    <Fd2>0</Fd2>    <cPT>2013-08-19 01:15:00</cPT>    <AMPM>AM</AMPM>    <ls/>  </P>  <P>    <id>110171677</id>    <PN>李四</PN>    <PT>2013-08-19 02:09:00</PT>    <Cd>46</Cd>    <Td>24</Td>    <Fd>64519</Fd>    <Fd2>71411</Fd2>    <cPT>2013-08-19 02:09:00</cPT>    <AMPM>AM</AMPM>    <ls/>  </P>  </Root>XML;$xml = simplexml_load_string($string);foreach($xml->P as $item){     $item=(array)$item;    $arr[]=array('PN'=>$item['PN'],'PT'=>$item['PT']);}echo json_encode($arr);
Copy after login

[{"PN":"\u5f20\u4e09","PT":"2013-08-19 00:02:00"},{"PN":"\u6211\u662f\u5927\u7f8e\u4eba","PT":"2013-08-19 01:15:00"},{"PN":"\u674e\u56db","PT":"2013-08-19 02:09:00"}]

$xml =<<< XML<Root>  <P>    <id>110171675</id>    <PN>张三</PN>    <PT>2013-08-19 00:02:00</PT>    <Cd>46</Cd>    <Td>24</Td>    <Fd>74750</Fd>    <Fd2>0</Fd2>    <cPT>2013-08-19 00:02:00</cPT>    <AMPM>AM</AMPM>    <ls/>  </P>  <P>    <id>110171676</id>    <PN>我是大美人</PN>    <PT>2013-08-19 01:15:00</PT>    <Cd>46</Cd>    <Td>24</Td>    <Fd>74501</Fd>    <Fd2>0</Fd2>    <cPT>2013-08-19 01:15:00</cPT>    <AMPM>AM</AMPM>    <ls/>  </P>  <P>    <id>110171677</id>    <PN>李四</PN>    <PT>2013-08-19 02:09:00</PT>    <Cd>46</Cd>    <Td>24</Td>    <Fd>64519</Fd>    <Fd2>71411</Fd2>    <cPT>2013-08-19 02:09:00</cPT>    <AMPM>AM</AMPM>    <ls/>  </P>  </Root>XML;$sm = simplexml_load_string($xml);foreach($sm->P as $item) {  $r[] = array('PN' => strval($item->PN), 'PT' => strval($item->PT));}echo json_encode($r);
Copy after login
[{"PN":"\u5f20\u4e09","PT":"2013-08-19 00:02:00"},{"PN":"\u6211\u662f\u5927\u7f8e\u4eba","PT":"2013-08-19 01:15:00"},{"PN":"\u674e\u56db","PT":"2013-08-19 02:09:00"}]

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template