Heim > Backend-Entwicklung > PHP-Tutorial > 高分在线求助XML解析

高分在线求助XML解析

WBOY
Freigeben: 2016-06-23 14:25:06
Original
893 Leute haben es durchsucht

本帖最后由 HUENKE 于 2013-10-25 15:58:22 编辑

xml

<?xml version="1.0" encoding="utf-8"?><nodes>	<node id="c5569e2c-0f4a-4ec2-86ba-736b79eca18c" name="A公司"		type="0">		<node id="4f2b55e9-e10a-496b-8bca-60e6f26daee5" name="部门1" type="1">			<node id="363" name="叶子1" type="2" playurl="http://XXXXXXX363" />		</node>		<node id="0c5ce42f-ba31-4b7a-8173-79ecae4a73ca" name="部门2" type="1">			<node id="241" name="叶子1" type="2"	playurl="http://xxxxx=241" />			<node id="356" name="叶子1" type="2" playurl="xxxxxxxxx=356" />		</node>	</node></nodes>
Nach dem Login kopieren


结构为 nodes下面有node下面还有node
我想做三重循环
foreach()
{
存公司名称
foreach()
{
存部门名称
foreach()
{ 存叶子名称}
}
}

请问我该怎么解析呢,


回复讨论(解决方案)

SimpleXML 用过嘛?

SimpleXML 没解析出来
能帮简单调试一下吗

你想要怎样的数据格式?,下面自己组织一下数据结构吧

<?php$string = <<<XML<?xml version="1.0" encoding="utf-8"?><nodes>    <node id="c5569e2c-0f4a-4ec2-86ba-736b79eca18c" name="A公司"        type="0">        <node id="4f2b55e9-e10a-496b-8bca-60e6f26daee5" name="部门1" type="1">            <node id="363" name="叶子1" type="2" playurl="http://XXXXXXX363" />        </node>        <node id="0c5ce42f-ba31-4b7a-8173-79ecae4a73ca" name="部门2" type="1">            <node id="241" name="叶子1" type="2"    playurl="http://xxxxx=241" />            <node id="356" name="叶子1" type="2" playurl="xxxxxxxxx=356" />        </node>    </node></nodes>XML;$xml = new SimpleXMLElement($string);$result = $xml->xpath('//node/@name');while(list( , $node) = each($result)) {    echo '@name: ',$node,"\n";}$result = $xml->xpath('//node/@id');while(list( , $node) = each($result)) {    echo '@id: ',$node,"\n";}$result = $xml->xpath('//node[@type=1]/../@id');while(list( , $node) = each($result)) {    echo '@部门的parent_id: ',$node,"\n";}$result = $xml->xpath('//node[@type=2]/../@id');while(list( , $node) = each($result)) {    echo '@叶子的parent_id: ',$node,"\n";}?>
Nach dem Login kopieren

$s =<<< XML<?xml version="1.0" encoding="utf-8"?><nodes>    <node id="c5569e2c-0f4a-4ec2-86ba-736b79eca18c" name="A公司"        type="0">        <node id="4f2b55e9-e10a-496b-8bca-60e6f26daee5" name="部门1" type="1">            <node id="363" name="叶子1" type="2" playurl="http://XXXXXXX363" />        </node>        <node id="0c5ce42f-ba31-4b7a-8173-79ecae4a73ca" name="部门2" type="1">            <node id="241" name="叶子1" type="2"    playurl="http://xxxxx=241" />            <node id="356" name="叶子1" type="2" playurl="xxxxxxxxx=356" />        </node>    </node></nodes>XML;$xml = simplexml_load_string($s);foreach($xml->node as $node) {  foreach($node->attributes() as $k=>$v) echo "$k => $v\n";  foreach($node->node as $node) {    foreach($node->attributes() as $k=>$v) echo "$k => $v\n";    foreach($node->node as $node) {      foreach($node->attributes() as $k=>$v) echo "$k => $v\n";    }  }}
Nach dem Login kopieren
id => c5569e2c-0f4a-4ec2-86ba-736b79eca18c
name => A公司
type => 0
id => 4f2b55e9-e10a-496b-8bca-60e6f26daee5
name => 部门1
type => 1
id => 363
name => 叶子1
type => 2
playurl => http://XXXXXXX363
id => 0c5ce42f-ba31-4b7a-8173-79ecae4a73ca
name => 部门2
type => 1
id => 241
name => 叶子1
type => 2
playurl => http://xxxxx=241
id => 356
name => 叶子1
type => 2
playurl => xxxxxxxxx=356

就是按他原样的树状结构

A公司
    部门1
叶子1

部门2
。。。。

再给你一个吧,自己修改一下就是树状了

<?php$string = <<<XML<?xml version="1.0" encoding="utf-8"?><nodes>    <node id="c5569e2c-0f4a-4ec2-86ba-736b79eca18c" name="A公司"        type="0">        <node id="4f2b55e9-e10a-496b-8bca-60e6f26daee5" name="部门1" type="1">            <node id="363" name="叶子1" type="2" playurl="http://XXXXXXX363" />        </node>        <node id="0c5ce42f-ba31-4b7a-8173-79ecae4a73ca" name="部门2" type="1">            <node id="241" name="叶子1" type="2"    playurl="http://xxxxx=241" />            <node id="356" name="叶子1" type="2" playurl="xxxxxxxxx=356" />        </node>    </node></nodes>XML;$xml = new SimpleXMLElement($string);// var_export($xml);$xmlIterator = new RecursiveIteratorIterator(    new SimpleXMLIterator($string),    RecursiveIteratorIterator::SELF_FIRST);foreach ($xmlIterator as $nodeName => $node) {    echo str_repeat('-', (int)$node['type']);    echo $node['id'], ',', $node['name'], PHP_EOL;}?>
Nach dem Login kopieren

上面#6
$xml = new SimpleXMLElement($string); 
这句可以删掉,前面调试的时候留下的

Verwandte Etiketten:
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage