1,指定的文件夹下。如:CMMB/02/000601文件夹,查询次文件夹下的的第一个XML文件。2,xml文件格式如下:<?xml version="1.0" encoding="UTF-8"?>
<result>
<components>
<component>
<id>common_1</id>
<type>common</type>
<title>IT时代</title>
<items>
<item>
<id>news/130122001</id>
<time>2013-01-21 17:47:02</time>
<title><![CDATA[网购影响国人消费习惯 ]]></title>
    <description><![CDATA[揭秘“线下试用,线上购买”的“抄码族”]]></description>
    <imageNum>1</imageNum>
    <isImageOnly>false</isImageOnly>
    <content_type>text/html</content_type>
    <icon>FA2BD84B53B94CE8B6E35E0F2CB5CBE9.jpg</icon>
</item>
<item>
<id>news/130122002</id>
<time>2013-01-21 17:47:02</time>
<title><![CDATA[百度遭苹果下架后半日后恢复]]></title>
    <description><![CDATA[“除了公关缺失,苹果在中国还需弥补开发者关系这一课。”]]></description>
    <imageNum>1</imageNum>
    <isImageOnly>false</isImageOnly>
    <content_type>text/html</content_type>
    <icon>95C96D68E4C6449E860F7C514762D805.jpg</icon>
</item>
<item>
<id>news/130122003</id>
<time>2013-01-21 17:47:02</time>
<title><![CDATA[2012最爱APP排行榜发布]]></title>
    <description><![CDATA[北京晨报制作出一份APP排行榜:我们最爱的十款产品。]]></description>
    <imageNum>1</imageNum>
    <isImageOnly>false</isImageOnly>
    <content_type>text/html</content_type>
    <icon>F851463AF50E49DBA1937378BCA1FFD3.jpg</icon>
</item>
<item>
N个item。
</item>
</items>
</component>
</components>
</result>3,将XML文件内的数据存入数组。
如:N个item
$arr1[$N]=id
$arr2[$N]=time
$arr3[$N]=title
$arr4[$N]=description
$arr5[$N]=isImageOnly
$arr6[$N]=content_type
$arr7[$N]=icon4,循环"N"输出数组。
初学PHP不太会,求代码,谢谢
xml

解决方案 »

  1.   

    http://www.cnblogs.com/likwo/archive/2011/08/24/2151793.html看看这个
      

  2.   

    demo地址:http://sandbox.onlinephpfunctions.com/code/afd0a674a5eaf9ac910d2b3a2b8ec37ff701cec2$str = <<<EOT
    <?xml version="1.0" encoding="UTF-8"?>
    <result>
    <components>
    <component>
    <id>common_1</id>
    <type>common</type>
    <title>IT时代</title>
    <items>
    <item>
    <id>news/130122001</id>
    <time>2013-01-21 17:47:02</time>
    <title><![CDATA[网购影响国人消费习惯 ]]></title>
        <description><![CDATA[揭秘“线下试用,线上购买”的“抄码族”]]></description>
        <imageNum>1</imageNum>
        <isImageOnly>false</isImageOnly>
        <content_type>text/html</content_type>
        <icon>FA2BD84B53B94CE8B6E35E0F2CB5CBE9.jpg</icon>
    </item>
    <item>
    <id>news/130122002</id>
    <time>2013-01-21 17:47:02</time>
    <title><![CDATA[百度遭苹果下架后半日后恢复]]></title>
        <description><![CDATA[“除了公关缺失,苹果在中国还需弥补开发者关系这一课。”]]></description>
        <imageNum>1</imageNum>
        <isImageOnly>false</isImageOnly>
        <content_type>text/html</content_type>
        <icon>95C96D68E4C6449E860F7C514762D805.jpg</icon>
    </item>
    <item>
    <id>news/130122003</id>
    <time>2013-01-21 17:47:02</time>
    <title><![CDATA[2012最爱APP排行榜发布]]></title>
        <description><![CDATA[北京晨报制作出一份APP排行榜:我们最爱的十款产品。]]></description>
        <imageNum>1</imageNum>
        <isImageOnly>false</isImageOnly>
        <content_type>text/html</content_type>
        <icon>F851463AF50E49DBA1937378BCA1FFD3.jpg</icon>
    </item>
    </items>
    </component>
    </components>
    </result>
    EOT;
    $doc = new DOMDocument();
    $doc->loadXML($str);
    $item = $doc->getElementsByTagName( "item" );
    foreach($item as $row){
      $titles = $row->getElementsByTagName( "title" );
      $title = $titles->item(0)->nodeValue;
      echo "$title\n";
      //这里以title为例,ID,TIME等自己填写吧。
    }
      

  3.   


    <?php
    $base_xml_data = simplexml_load_file(dirname(__FILE__) . '/data.xml');
    $data = array();foreach($base_xml_data->components->component as $component){
        $component_id    = (string)$component->id;
        $data[$component_id] = array(
            'id'    => $component_id,
            'type'  => (string)$component->type,
            'title' => (string)$component->title,
            'items' => array()
        );
        foreach($component->items->item as $item){
            $item_id = (string)$item->id;
            $data[$component_id]['items'][$item_id] = array(
                'id'            => $item_id,
                'time'          => (string)$item->time,
                'title'         => (string)$item->title,
                'description'   => (string)$item->description,
                'imageNum'      => (int)$item->imageNum,
                'isImageOnly'   => (string)$item->isImageOnly,
                'content_type'  => (string)$item->content_type,
                'icon'          => (string)$item->icon
            );
        }
    }print_r($data);
      

  4.   


    <?PHP
    //可优化~~
    $dir='D:\wamp\run\test';
    function readxml($dir,&$arr,$state=false){ //$state 状态 ,默认false
    if($handle=opendir($dir)){
    while(false!==($file=readdir($handle))){
    $sub_dir=$dir.DIRECTORY_SEPARATOR.$file;
    if(!$state&&is_dir($sub_dir)&&$file!='.'&&$file!='..'){//状态为false时,即是为传入的$dir,可继续往下遍历
    readxml($sub_dir,$arr,true);//遍历次文件夹是状态为true
    }elseif($state&&preg_match('/\.xml$/',$file)){//判断状态为true时,并且为xml后缀文件
    if(count($arr)>0){
    return false;
    }
    $arr[]=$sub_dir;
    }
    }
    }
    }
    readxml($dir,$arr);
    $item_list=array();
    foreach($arr as $x){ //遍历$arr文件数组,只有一个,你可以修改下不需要遍历
    $doc = new DOMDocument();
    $doc->load ($x);
    //$tmp = $doc->getElementsByTagName('component');
    //($tmp as $t){
    //分层遍历xml
    $items =  $doc->getElementsByTagName("items");
    foreach($items as $it){
    $item=$it->getElementsByTagName("item");
    $i=0;
    foreach($item as $title){
    $item_list[$i]['id']=$title->getElementsByTagName('id')->item(0)->nodeValue;
    $item_list[$i]['time']=$title->getElementsByTagName('time')->item(0)->nodeValue;
    $item_list[$i]['title']=$title->getElementsByTagName('title')->item(0)->nodeValue;
    $item_list[$i]['description']=$title->getElementsByTagName('description')->item(0)->nodeValue;
    $item_list[$i]['imageNum']=$title->getElementsByTagName('imageNum')->item(0)->nodeValue;
    $item_list[$i]['isImageOnly']=$title->getElementsByTagName('isImageOnly')->item(0)->nodeValue;
    $item_list[$i]['content_type']=$title->getElementsByTagName('content_type')->item(0)->nodeValue;
    $item_list[$i]['icon']=$title->getElementsByTagName('icon')->item(0)->nodeValue;
    $i++;
    }
    //}
    }
    }
    //循环输出xml数据
    foreach($item_list as $key=>$val){
    Echo 'Item'.$key.":<br>";
    foreach($val as $k=>$v){
    Echo $k."=>".$v."<br>";
    }
    Echo "<p>--------------------------------------------</p>";
    }
      

  5.   


    $lines= simplexml_load_file('CMMB/02/000601/***.xml');
    $xml=simplexml_load_string($lines,'SimpleXMLElement' , LIBXML_NOCDATA);
    $arrs=array();
    for($i=0;$i<count($xml);$i++){
    $arrs[$i]['id'] = $xml->item[$i]->id;
    $arrs[$i]['time'] = $xml->item[$i]->time;
    $arrs[$i]['title'] = $xml->item[$i]->title;
    $arrs[$i]['description'] = $xml->item[$i]->description;
    $arrs[$i]['isImageOnly'] = $xml->item[$i]->isImageOnly;
    $arrs[$i]['content_type'] = $xml->item[$i]->content_type;
    $arrs[$i]['icon'] = $xml->item[$i]->icon;
    }看这样能行不
      

  6.   

    来晚了,但是还是分享一下我处理 PHP 数组与 XML 互转的类库
    http://www.bacysoft.cn/thread-91-1-1.html