Php获取xml中的节点值
XML: test_xml2.xml
xsi:schemaLocation="http://api.xiaonei.com/1.0/ http://api.xiaonei.com/1.0/xiaonei.xsd">
200032219
84525914
12345689
98765432
PHP代码1:
$doc=new DOMDocument();
$doc->load('test_xml2.xml');
$productProfile=$doc->getElementsByTagName('friends_get_response');
echo '
';foreach($productProfile as $profile)
{
//$productNames = $profile->getElementsByTagName("uid");
//$productName = $productNames->item(0)->nodeValue;
//echo $productName;
echo $profile->nodeValue;
}
?>
结果: 这样nodeValue,直接把节点中的所有值都取出来
200032219
84525914
12345689
98765432
PHP 代码2:
$doc = new DOMDocument();
$doc->load('test_xml2.xml');
$xiaoNei = $doc->getElementsByTagName( "friends_get_response" );
$i=0;
foreach($xiaoNei as $key ){
$uid = $key->getElementsByTagName( "uid" );
foreach ( $uid as $param) {
echo $param -> nodeValue .'
';
}
}
?>
结果:这是得到friends_get_response->uid 节点的值
200032219
84525914
12345689
98765432
XML2: test_xml4.xml
aa
bb1
bbb1
bbb2
cc
ff
PHP代码:
echo '
';$doc = new DOMDocument();
$doc->load( 'test_xml4.xml' );
$xiaoNei = $doc->getElementsByTagName( "test" );
foreach( $xiaoNei as $v)
{
echo $v->nodeValue.'
';
}
?>
结果为: 使用nodeValue直接就得到test节点下面的 多层子节点的值
aa
bb1
bbb1
bbb2
cc
ff
/***************************************/
PHP代码:
$xmlstr = <<PHP: Behind the Parser
Ms. Coder
Onlivia Actora
Mr. Coder
El ActÓr
So, this language. It's like, a programming language. Or is it a
scripting language? All is revealed in this thrilling horror spoof
of a documentary.
7
5
XML;
$xml = simplexml_load_string($xmlstr);
echo $xml->movie[0]->title;
echo '
';
$arr = $xml->movie[0]->characters[0]->character;
echo '
';
foreach($arr as $kk => $vv)
{
echo $vv->name;
echo '
';
}
?>
结果为:
$xml->movie[0]->title:
PHP: Behind the Parser
$vv->name: Ms. Coder
Mr. Coder