我的程序如下:
<?php
header('Content-Type: text/xml');
$url="http://open.client.lashou.com/list/api/";
$xml_str=file_get_contents($url);
$doc = new DOMDocument();
$doc->loadXML($xml_str);
$urllist = $doc->documentElement->getElementsByTagName( 'url');
$length=$urllist->length;
for($i=0;$i< $length; $i++)
{
$listcity = $urllist->item($i)->getElementsByTagName('city');
$tmpcity=$listcity->item(0)->nodeValue;
if($tmpcity != '全国'||$tmpcity != '西安')
{
$urllist->item($i)->parentNode->removeChild($urllist->item($i));
}
}
echo $doc->saveXML();
?>肯定是我的程序错误,但我找不到错在哪里,麻烦高手帮忙看看,谢谢了
<?php
header('Content-Type: text/xml');
$url="http://open.client.lashou.com/list/api/";
$xml_str=file_get_contents($url);
$doc = new DOMDocument();
$doc->loadXML($xml_str);
$urllist = $doc->documentElement->getElementsByTagName( 'url');
$length=$urllist->length;
for($i=0;$i< $length; $i++)
{
$listcity = $urllist->item($i)->getElementsByTagName('city');
$tmpcity=$listcity->item(0)->nodeValue;
if($tmpcity != '全国'||$tmpcity != '西安')
{
$urllist->item($i)->parentNode->removeChild($urllist->item($i));
}
}
echo $doc->saveXML();
?>肯定是我的程序错误,但我找不到错在哪里,麻烦高手帮忙看看,谢谢了
<?php
header('Content-Type: text/xml');
$url="http://open.client.lashou.com/list/api/";
$xml_str=file_get_contents($url);
$doc = new DOMDocument();
$doc->loadXML($xml_str);
$urllist = $doc->documentElement->getElementsByTagName( 'urlset');
$length=$urllist->length;
for($i=0;$i< $length; $i++)
{
$listcity = $urllist->item($i)->getElementsByTagName('city');
$tmpcity=$listcity->item(0)->nodeValue;
if($tmpcity != '全国'||$tmpcity != '西安')
{
$urllist->item($i)->parentNode->removeChild($urllist->item($i));
}
}
echo $doc->saveXML();
改了下,不报错了
<?php
header('Content-Type: text/html;charset=utf-8;');
$url="http://open.client.lashou.com/list/api/";
$xml_str=file_get_contents($url);
$doc = new DOMDocument();
$doc->loadXML($xml_str);
$urllist = $doc->documentElement->getElementsByTagName( 'url');
foreach($urllist as $city)
{
$tmpcity= $city->getElementsByTagName('city')->item(0)->firstChild->nodeValue; if($tmpcity!="全国"||$tmpcity !="西安")
{
$city->parentNode->removeChild($city);
}
} $doc->saveXML();
$doc->save("lashou.xml");
?>这样试试,不知道是否OK。还没测试过!
原因不明,请自行查找由于该 xml 文档较大,调试时可先保存在本地
file_put_contents('xml.xml', $xml_str);
我之前也怀疑那个api有问题,但#4的程序又能输出完整的xml(当然逻辑上这个程序并不符合LZ需求),奇怪
测试了OK!但是如果生成XML,被过滤的部分会变成一大丢空白留在XML文件里。