{"id":412,"title":"php\u8bfb\u53d6xml\u5185\u5bb9","good":0,"bad":0,"hit":1416,"created_at":"2018-12-20 17:57:50","content":"

xml\u5982\u4e0b\uff1a<\/p>

<\/p>

<?xml version="1.0" encoding="utf-8"?><\/p>

<list><\/p>

    <item index="0"><\/p>

        <name a="a0" b="b0"><![CDATA[\u5f20\u4e09]]><\/name><\/p>

        <site c="c0" d="d0">http:\/\/www.hu-rong.com<\/site><\/p>

    <\/item><\/p>

    <item index="1"><\/p>

        <name a="a1" b="b1"><![CDATA[\u674e\u56db]]><\/name><\/p>

        <site c="c1" d="d1">www.hu-rong.com<\/site><\/p>

    <\/item><\/p>

<\/list><\/p>

<\/p>

<\/p>

\u8f6c\u4e3a\u6570\u636e\u7ed3\u6784\u5982\u4e0b\uff1a<\/p>

Array\n(\n    [item] => Array\n        (\n            [0] => Array\n                (\n                    [@attributes] => Array\n                        (\n                            [index] => 0\n                        )\n\n                    [name] => Array\n                        (\n                            [@attributes] => Array\n                                (\n                                    [a] => a0\n                                    [b] => b0\n                                )\n\n                        )\n\n                    [site] => http:\/\/www.hu-rong.com\n                )\n\n            [1] => Array\n                (\n                    [@attributes] => Array\n                        (\n                            [index] => 1\n                        )\n\n                    [name] => Array\n                        (\n                            [@attributes] => Array\n                                (\n                                    [a] => a1\n                                    [b] => b1\n                                )\n\n                        )\n\n                    [site] => www.hu-rong.com\n                )\n\n        )\n\n)<\/pre>

<\/p>

\u5947\u602a\u7684\u662fsite\u7684\u5c5e\u6027\u4e22\u4e86<\/p>

<\/p>

php\u8bfbxml<\/p>

<\/p>

<?php<\/p>

$xml=simplexml_load_file('test.xml');<\/p>

\/\/print_r(json_decode(json_encode($xml),true));<\/p>

echo $xml->getName().'\u8282\u70b9\u4e0b\u6709'.$xml->count().'\u4e2aitem<br\/>';<\/p>

foreach($xml as $item){<\/p>

    $str='';<\/p>

    $child=$item->children();<\/p>

    $attributes=$child->attributes();<\/p>

    $str.='index\u4e3a'.$item->attributes()->index.'item\u4e0b\u6709';<\/p>

    $str.=$child[0]->getName().'(\u5c5e\u6027'.$attributes[0]->getName().'=>'.$attributes[0].','.$attributes[1]->getName().'=>'.$attributes[1].')\u548c';<\/p>

    $str.=$child[1]->getName().'\u8282\u70b9';<\/p>

    $str.='\u5176\u503c\u5206\u522b\u4e3a:'.$item->name.','.$child[1].'<br\/>';<\/p>

    echo $str;<\/p>

}<\/p>

<\/p>

\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>

index\u4e3a0item\u4e0b\u6709name(\u5c5e\u6027a=>a0,b=>b0)\u548csite\u8282\u70b9\u5176\u503c\u5206\u522b\u4e3a:\u5f20\u4e09,http:\/\/www.hu-rong.com<\/p>

index\u4e3a1item\u4e0b\u6709name(\u5c5e\u6027a=>a1,b=>b1)\u548csite\u8282\u70b9\u5176\u503c\u5206\u522b\u4e3a:\u674e\u56db,www.hu-rong.com<\/p>

list\u8282\u70b9\u4e0b\u67092\u4e2aitem<\/p>

<\/p>

<\/p>

\u4f8b2\uff1a<\/p>

<?php<\/p>

$xml=simplexml_load_file('test.xml');<\/p>

\/\/print_r(json_decode(json_encode($xml),true));<\/p>

$count=$xml->count();<\/p>

echo $xml->getName().'\u8282\u70b9\u4e0b\u6709'.$count.'\u4e2aitem<br\/>';<\/p>

$i=0;<\/p>

foreach($xml as $item){<\/p>

    $str='';<\/p>

    $child=$item->children()[$i];<\/p>

    $attributes=$child->attributes();<\/p>

    $str.='index\u4e3a'.$item->attributes()->index.'\u7684item\u4e0b\u7684';<\/p>

    $str.=$child->getName().'(\u5c5e\u6027'.$attributes->getName().'=>'.$attributes[0].','.$attributes[1]->getName().'=>'.$attributes[1].')';<\/p>

    $str.='\u5176\u503c\u5206\u522b\u4e3a:'.$child.'<br\/>';<\/p>

    echo $str;<\/p>

    $i++;<\/p>

}<\/p>

<\/p>

\u8fd0\u884c\u7ed3\u679c\uff1a<\/p>

<\/p>

index\u4e3a0\u7684item\u4e0b\u7684name(\u5c5e\u6027a=>a0,b=>b0)\u5176\u503c\u5206\u522b\u4e3a:\u5f20\u4e09<\/p>

index\u4e3a1\u7684item\u4e0b\u7684site(\u5c5e\u6027c=>c1,d=>d1)\u5176\u503c\u5206\u522b\u4e3a:www.hu-rong.com<\/p>

list\u8282\u70b9\u4e0b\u67092\u4e2aitem<\/p>

<\/p>"}