xml.php(生成xml)
---------------------------------------------------------
<?
$conn = mysql_connect('localhost', 'root', '123456') or die('Could not connect: ' . mysql_error());
mysql_select_db('vdigital', $conn) or die ('Can/'t use database : ' . mysql_error());
$str = "SELECT id,username FROM `admin` GROUP BY `id` ORDER BY `id` ASC";
$result = mysql_query($str) or die("Invalid query: " . mysql_error());
if($result)
{
    $xmlDoc = new DOMDocument();
if(!file_exists("01.xml")){
    $xmlstr = "<?xml version='1.0' encoding='utf-8' ?><message></message>";
    $xmlDoc->loadXML($xmlstr);
    $xmlDoc->save("01.xml");
}
else {    $xmlDoc->load("01.xml");}
$Root = $xmlDoc->documentElement;

//ѭ
while ($arr = mysql_fetch_array($result)){

$node1 = $xmlDoc->createElement("id");
$text = $xmlDoc->createTextNode(iconv("GB2312","UTF-8",$arr["id"]));
$node1->appendChild($text);

$node2 = $xmlDoc->createElement("name");
$text2 = $xmlDoc->createTextNode(iconv("GB2312","UTF-8",$arr["username"]));
$node2->appendChild($text2);

$Root->appendChild($node1);
$Root->appendChild($node2);

$xmlDoc->save("01.xml");

} //ѭ
}
mysql_close($conn);
?>




test.php(应用测试)
--------------------------------------------------------------------
<?
$xmlDoc = new DOMDocument();
$xmlDoc->load("http://localhost/xml/xml.php");
$x=$xmlDoc->getElementsByTagName('name');
for ($i=0; $i<=$x->length-1; $i++)
{
if(strpos($x->item($i)->nodeValue,"fang")!==false)
{
echo    $x->item($i)->parentNode->childNodes->item(1)->nodeValue;
}
}

?>
Logo

开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!

更多推荐