ホームページ >バックエンド開発 >PHPチュートリアル >高分在线XML解析
高分在线求助XML解析
<br /><?xml version="1.0" encoding="utf-8"?><br /><nodes><br /> <node id="c5569e2c-0f4a-4ec2-86ba-736b79eca18c" name="A公司"<br /> type="0"><br /> <node id="4f2b55e9-e10a-496b-8bca-60e6f26daee5" name="部门1" type="1"><br /> <node id="363" name="叶子1" type="2" playurl="http://XXXXXXX363" /><br /> </node><br /> <node id="0c5ce42f-ba31-4b7a-8173-79ecae4a73ca" name="部门2" type="1"><br /> <node id="241" name="叶子1" type="2" playurl="http://xxxxx=241" /><br /> <node id="356" name="叶子1" type="2" playurl="xxxxxxxxx=356" /><br /> </node><br /> </node><br /></nodes><br />
<br /><?php<br />$string = <<<XML<br /><?xml version="1.0" encoding="utf-8"?><br /><nodes><br /> <node id="c5569e2c-0f4a-4ec2-86ba-736b79eca18c" name="A公司"<br /> type="0"><br /> <node id="4f2b55e9-e10a-496b-8bca-60e6f26daee5" name="部门1" type="1"><br /> <node id="363" name="叶子1" type="2" playurl="http://XXXXXXX363" /><br /> </node><br /> <node id="0c5ce42f-ba31-4b7a-8173-79ecae4a73ca" name="部门2" type="1"><br /> <node id="241" name="叶子1" type="2" playurl="http://xxxxx=241" /><br /> <node id="356" name="叶子1" type="2" playurl="xxxxxxxxx=356" /><br /> </node><br /> </node><br /></nodes><br />XML;<br /><br />$xml = new SimpleXMLElement($string);<br /><br />$result = $xml->xpath([email protected]');<br />while(list( , $node) = each($result)) {<br /> echo [email protected]: ',$node,"\n";<br />}<br />$result = $xml->xpath([email protected]');<br />while(list( , $node) = each($result)) {<br /> echo [email protected]: ',$node,"\n";<br />}<br />$result = $xml->xpath('//node[@type=1]/..[email protected]');<br />while(list( , $node) = each($result)) {<br /> echo [email protected]_id: ',$node,"\n";<br />}<br />$result = $xml->xpath('//node[@type=2]/..[email protected]');<br />while(list( , $node) = each($result)) {<br /> echo [email protected]_id: ',$node,"\n";<br />}<br /><br />?><br />
$s =<?xml version="1.0" encoding="utf-8"?><br><nodes><br> <node></node> type="0"><br> <node><br> <node></node><br> </node><br> <node><br> <node></node><br> <node></node><br> </node><br> <br></nodes><br>XML;<br>$xml = simplexml_load_string($s);<br><br>foreach($xml->node as $node) {<br> foreach($node->attributes() as $k=>$v) echo "$k => $v\n";<br> foreach($node->node as $node) {<br> foreach($node->attributes() as $k=>$v) echo "$k => $v\n";<div class="clear"> </div>