php 使用对象方式读取XML

<?php 
	//不适合大本地文件跟远程文件
	$dom=new DOMDocument();
	$dom->load("user.xml");
	$persons=$dom->getElementsByTagName("person");
	//文本类型:3
	/*
	$person->item(0)->nodeType;//echo 输出节点类型
	$person->item(0)->nodeName;//echo 输出节点名字
	$person->item(0)->nodeValue;//echo 输出节点值
	*/
	//echo $person->item(0)->childNodes->item(0)->childNodes->item(0)->nodeType;
	//找下一个的同胞节点
	//echo $person->item(0)->childNodes->item(0)->nextSibling->nodeName;
	echo '<table border="1" width="500" align="center">';
	foreach($persons as $person){
		echo '<tr>';
		
		echo '<td>'.$person->getAttribute("id").'</td>';
		$usernames=$person->getElementsByTagName("username");
		echo '<td>'.$usernames->item(0)->childNodes->item(0)->nodeValue.'</td>';
		$phones=$person->getElementsByTagName("phone");
		echo '<td>'.$phones->item(0)->childNodes->item(0)->nodeValue.'</td>';
		echo '</tr>';
	}
	//header("Content-type:text/xml");
	//echo $dom->saveXML();


你可能感兴趣的:(xml,PHP,对象)