PHP读取XML数据的四种方法以及将XML转成JSON格式

<?
//PHP读取XML数据的四种方法
 

//------------------- 方法一 [DOMDOCUMENT] --------------------//
$doc = new DOMDOCUMENT();
$doc -> load('1.xml');
$peoples = $doc -> getElementsByTagName('peoples');
foreach($peoples as $people){
    $names = $people -> getElementsByTagName('name');
    $talls = $people -> getElementsByTagName('tall');
    //echo iconv('utf-8','gbk',$names->item(1)->nodeValue);//梁朝伟 <解决中文为乱码>
}
 

 

//--------------------- 方法二 [simplexml] (简单)-----------------//
$xml = simplexml_load_file('1.xml');
foreach($xml as $x){
    //echo iconv('utf-8','gbk',$x->name),' ';//Faya 梁朝伟
}
 

//---------------------- 方法三 [正则表达式] (正则匹配效率要差点) ------------------//
$xml = "";
$f = fopen('1.xml', 'r');
while($data = fread($f, 1024)) {
    $xml .= $data;
}
fclose($f);
preg_match_all("/\<peoples\>(.*?)\<\/peoples\>/s", $xml,$peoples);
foreach($peoples[1] as $k=>$people) {
    preg_match_all("/\<name\>(.*?)\<\/name\>/", $people, $name);
    preg_match_all("/\<tall\>(.*?)\<\/tall\>/", $people, $tall);
}
 

foreach($name[0] as $key=>$val){
    //echo $val,' '; //Faya 梁朝伟
}
 

//---------------------- 方法四 [XMLReader] (功能强大,主要针对属性)--------------------------//
$url = "http://www.google.com/ig/api?weather=guangzhou";
$xmlreader = new XMLReader();
$xmlreader -> open($url);
while($xmlreader->read()) {
 

    if ($xmlreader->name == 'day_of_week') {
       echo e2c($xmlreader->getAttribute('data')),':';
    }
 

    if ($xmlreader->name == 'low') {
       $centigrade = round(($xmlreader->getAttribute('data') - 32) / 1.8 , 1);
       echo  '最低气温:'.$centigrade.', ';
    }
    if ($xmlreader->name == 'high') {
       $centigrade = round(($xmlreader->getAttribute('data') - 32) / 1.8 , 1);
       echo  '最高气温:'.$centigrade.'<br>';
    }
}
$xmlreader->close();
 

function e2c($str){
    $str = strtolower($str);
    $en = array('mon','tue','wed','thu','fri','sat','sun');
    $cn = array('星期一','星期二','星期三','星期四','星期五','星期六','星期日');
    return str_replace($en,$cn,$str);
}
?>
 

 

<?xml version="1.0" encoding="gbk"?>
<peoples>
  <people>
    <name>ChyiChin</name>
    <sex>男</sex>
  </people>
  <people>
    <name>王菲</name>
    <sex>女</sex>
  </people>
</peoples>
<?
//对字段类型提供优化的建议SELECT * FROM pre_forum_post PROCEDURE ANALYSE(1)\G
echo long2ip('-888');
 

//XML转成JSON格式
function xml_to_json($source) {
    if(is_file($source)){
       $xml_array=simplexml_load_file($source);
    }else{
       $xml_array=simplexml_load_string($source);
    }
    $json = json_encode($xml_array);
    return $json;
}
echo '<br>';
echo xml_to_json('test.xml');
echo '<br>';
 

$str = <<<EOF
<peoples>
<people><name>王菲</name></people>
<people><name>陈奕迅</name></people>
</peoples>
EOF;
$str = iconv("gbk", "utf-8", $str);
echo xml_to_json($str);
?>

 

转载于:https://www.cnblogs.com/fuchuangxin/archive/2012/10/24/2736585.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值