a.xml文件<?xml version="1.0" encoding="GB2312" ?>
<air_info ret_value="1" date="21DEC(SUN)" from="SHA" to="PEK" fromname="上海虹桥" toname="北京首都" Mileage="1178">
<airline line_number="CA1858" company="中国国际航空公司" sc="SHA" sa="上海虹桥机场" ac="PEK" aa="北京首都机场" stime="0745" atime="1005" airrax="50" fulerax="150" food="1" dfly="1" type="330" et="1">
<c b="F" d="头等舱" p="1700" s="A" />
<c b="Y" d="全价舱" p="1130" s="A" />
<c b="S" d="*未知" p="0" s="A" />
<c b="W" d="*未知" p="0" s="A" />
<c b="T" d="*未知" p="0" s="A" />
</airline>
<airline line_number="*FM9891" company="中国上海航空公司" sc="SHA" sa="上海虹桥机场" ac="PEK" aa="北京首都机场" stime="0745" atime="1005" airrax="50" fulerax="150" food="1" dfly="1" type="330" et="1">
<c b="F" d="头等舱" p="1700" s="A" />
<c b="Y" d="全价舱" p="1130" s="A" />
</airline>
<airline line_number="MU5101" company="中国东方航空公司" sc="SHA" sa="上海虹桥机场" ac="PEK" aa="北京首都机场" stime="0800" atime="1020" airrax="50" fulerax="150" food="1" dfly="1" type="333" et="1">
<c b="F" d="头等舱" p="1700" s="A" />
<c b="A" d="*未知" p="0" s="5" />
<c b="Y" d="全价舱" p="1130" s="A" />
<c b="K" d="*未知" p="0" s="A" />
</airline>
</air_info> index.php
<?php
$doc = new DOMDocument;
$doc->load('a.xml');
$book = $doc->documentElement;
$airline = $book->getElementsByTagName('airline');
foreach($airline as $c)
{
echo $c->getAttribute('line_number'),' <br/>';
echo $c->getAttribute('company'),' <br/>';
echo $c->getAttribute('sc'),' <br/>';
/* 其它属性 */
$childnodes = $c->getElementsByTagName('c');
foreach($childnodes as $childc){
echo $childc->getAttribute('b'),' <br/>';
/* 其它属性 $childc->getAttribute('d')*/
}
}
?>问题是,我怎么算出这个节点号多少,几点的子节点号多少?如果算不出来请高手用for循环,帮助我重新写一个,这样子节点就很清晰就看到了
<air_info ret_value="1" date="21DEC(SUN)" from="SHA" to="PEK" fromname="上海虹桥" toname="北京首都" Mileage="1178">
<airline line_number="CA1858" company="中国国际航空公司" sc="SHA" sa="上海虹桥机场" ac="PEK" aa="北京首都机场" stime="0745" atime="1005" airrax="50" fulerax="150" food="1" dfly="1" type="330" et="1">
<c b="F" d="头等舱" p="1700" s="A" />
<c b="Y" d="全价舱" p="1130" s="A" />
<c b="S" d="*未知" p="0" s="A" />
<c b="W" d="*未知" p="0" s="A" />
<c b="T" d="*未知" p="0" s="A" />
</airline>
<airline line_number="*FM9891" company="中国上海航空公司" sc="SHA" sa="上海虹桥机场" ac="PEK" aa="北京首都机场" stime="0745" atime="1005" airrax="50" fulerax="150" food="1" dfly="1" type="330" et="1">
<c b="F" d="头等舱" p="1700" s="A" />
<c b="Y" d="全价舱" p="1130" s="A" />
</airline>
<airline line_number="MU5101" company="中国东方航空公司" sc="SHA" sa="上海虹桥机场" ac="PEK" aa="北京首都机场" stime="0800" atime="1020" airrax="50" fulerax="150" food="1" dfly="1" type="333" et="1">
<c b="F" d="头等舱" p="1700" s="A" />
<c b="A" d="*未知" p="0" s="5" />
<c b="Y" d="全价舱" p="1130" s="A" />
<c b="K" d="*未知" p="0" s="A" />
</airline>
</air_info> index.php
<?php
$doc = new DOMDocument;
$doc->load('a.xml');
$book = $doc->documentElement;
$airline = $book->getElementsByTagName('airline');
foreach($airline as $c)
{
echo $c->getAttribute('line_number'),' <br/>';
echo $c->getAttribute('company'),' <br/>';
echo $c->getAttribute('sc'),' <br/>';
/* 其它属性 */
$childnodes = $c->getElementsByTagName('c');
foreach($childnodes as $childc){
echo $childc->getAttribute('b'),' <br/>';
/* 其它属性 $childc->getAttribute('d')*/
}
}
?>问题是,我怎么算出这个节点号多少,几点的子节点号多少?如果算不出来请高手用for循环,帮助我重新写一个,这样子节点就很清晰就看到了
<air_info ret_value="1" date="21DEC(SUN)" from="SHA" to="PEK" fromname="上海虹桥" toname="北京首都" Mileage="1178">
<airline line_number="CA1858" company="中国国际航空公司" sc="SHA" sa="上海虹桥机场" ac="PEK" aa="北京首都机场" stime="0745" atime="1005" airrax="50" fulerax="150" food="1" dfly="1" type="330" et="1">
<c b="F" d="头等舱" p="1700" s="A" />
<c b="Y" d="全价舱" p="1130" s="A" />
<c b="S" d="*未知" p="0" s="A" />
<c b="W" d="*未知" p="0" s="A" />
<c b="T" d="*未知" p="0" s="A" />
</airline>
<airline line_number="*FM9891" company="中国上海航空公司" sc="SHA" sa="上海虹桥机场" ac="PEK" aa="北京首都机场" stime="0745" atime="1005" airrax="50" fulerax="150" food="1" dfly="1" type="330" et="1">
<c b="F" d="头等舱" p="1700" s="A" />
<c b="Y" d="全价舱" p="1130" s="A" />
</airline>
<airline line_number="MU5101" company="中国东方航空公司" sc="SHA" sa="上海虹桥机场" ac="PEK" aa="北京首都机场" stime="0800" atime="1020" airrax="50" fulerax="150" food="1" dfly="1" type="333" et="1">
<c b="F" d="头等舱" p="1700" s="A" />
<c b="A" d="*未知" p="0" s="5" />
<c b="Y" d="全价舱" p="1130" s="A" />
<c b="K" d="*未知" p="0" s="A" />
</airline>
</air_info> index.php
<?php
$doc = new DOMDocument;
$doc->load('a.xml');
$book = $doc->documentElement;
$airline = $book->getElementsByTagName('airline');
foreach($airline as $c)
{
echo $c->getAttribute('line_number'),' <br/>';
echo $c->getAttribute('company'),' <br/>';
echo $c->getAttribute('sc'),' <br/>';
/* 其它属性 */
$childnodes = $c->getElementsByTagName('c');
foreach($childnodes as $childc){
echo $childc->getAttribute('b'),' <br/>';
/* 其它属性 $childc->getAttribute('d')*/
}
}
?>
我要求不用foreach循环做,而用FOR循环做,
$doc = new DOMDocument;
$doc->load('a.xml');
$book = $doc->documentElement;
$airline = $book->getElementsByTagName('airline');
$fatherid=0;
foreach($airline as $c)
{
echo $c->getAttribute('line_number'),' <br/>';
echo $c->getAttribute('company'),' <br/>';
echo $c->getAttribute('sc'),' <br/>';
echo $fatherid;//airline节点号
/* 其它属性 */
$childnodes = $c->getElementsByTagName('c');
$childid = 0;
foreach($childnodes as $childc){
echo $childc->getAttribute('b'),' <br/>';
echo $childid;//对应c的节点号
/* 其它属性 $childc->getAttribute('d')*/
$childid++;
}
$fatherid++;
}