码迷,mamicode.com
首页 > Web开发 > 详细

PHP解析xml文档 1

时间:2015-06-25 01:23:17      阅读:105      评论:0      收藏:0      [点我收藏+]

标签:simplexml

SimpleXML 解析xml 如果xml没有声明encoding默认使用utf-8进行解码,最终得到的数据编码为utf-8格式。

我猜想它内部实现了iconv(encoding=’utf-8’, ‘utf-8’,str); 所以当xml中存在中文,但是xml没有声明encoding = ‘gbk’ 会报错误。



testxml.xml文件如下

<?xml version="1.0" encoding="utf-8"?>
<languages>
 <lang name="C">
  <appeared>1972</appeared>
  <creator>Dennis Ritchie</creator>
 </lang>
 <lang name="PHP">
  <appeared>1995</appeared>
  <creator>Rasmus Lerdorf</creator>
 </lang>
 <lang name="Java">
  <appeared>1995</appeared>
  <creator>James Gosling</creator>
 </lang>
</languages>

<?php 
    /*获取xml对象*/
    $xmlobj = simplexml_load_file(‘testxml.xml‘);
    //var_dump($xmlobj);

    /*获取元素节点  返回XML对象*/
    $appeared = $xmlobj->lang[0]->appeared;
    $creator = $xmlobj->lang[0]->creator;
    //var_dump($appeared,$creator);

    /*获取元素的名称  返回字符串*/
    $rootName = $xmlobj->getName();
    //var_dump($rootName);

    /*属性节点访问  foreach遍历xml节点*/
    foreach($xmlobj->lang as $lang){
        printf("<p>%s appeared in %d and was created by %s.</p>",
        $lang[‘name‘],
        $lang->appeared,
        $lang->creator);
    }
?>

textxml2.xml文件如下:

<?xml version="1.0" encoding="utf-8"?>
<languages
 xmlns:dc="http://purl.org/dc/elements/1.1/">
 <lang name="C">
  <appeared>1972</appeared>
  <dc:creator>Dennis Ritchie</dc:creator>
 </lang>
 <lang name="PHP">
  <appeared>1995</appeared>
  <dc:creator>Rasmus Lerdorf</dc:creator>
 </lang>
 <lang name="Java">
  <appeared>1995</appeared>
  <dc:creator>James Gosling</dc:creator>
 </lang>
</languages>

<?php
    $ncxmlobj = simplexml_load_file(‘testxml2.xml‘);

    /*获取命名空间下的节点  */
    $nc = $ncxmlobj ->lang[0]->children(‘http://purl.org/dc/elements/1.1/‘);
    $ncreator = $nc->creator;
    //var_dump($ncreator);

    /*获取命名空间  获取节点下属于nc的节点*/
    $nc = $ncxmlobj -> getNamespaces(TRUE);
    $dc = $ncxmlobj ->lang[0]->children($nc[‘dc‘]);
    //var_dump($dc->creator);
?>

兼容的比较lower


<?php
    $xml = file_get_contents($url);
    foreach($http_response_header as $header){
        if(preg_match(‘#Content-Type(.*)charset=(.*)#i‘,$header,$match)){
            switch(strtolower($match[2])){
                case ‘utf-8‘:
                    break;
                case ‘iso-8859-1‘:
                    $xml = utf8_encode($xml); //Encodes an ISO-8859-1 string to UTF-8
                    break;
                default:
                    $xml = iconv($m[1], ‘utf-8‘, $xml);
            }
            break;
        }
    }
    simplexml_load_string($xml);
?>

PHP解析xml文档 1

标签:simplexml

原文地址:http://blog.csdn.net/whynottrythis/article/details/46629491

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!