前提條件: 閱讀XML基礎
SimpleXMLElement::children()函數是PHP中的一個內置函數,該函數返回SimpleXML對象中給定節點的子代。
用法:
SimpleXMLElement SimpleXMLElement::children( $namespace, $is_prefix )
參數:該函數接受上述和以下描述的兩個參數:
- $namespace:它是可選參數。它指定XML名稱空間。
- $is_prefix:這是布爾參數,它是可選的。如果$namespace為前綴,則為True;如果$namespace為命名空間URL,則為False。其默認值為False。
返回值:該函數返回一個SimpleXMLElement對象。
注意:此函數在PHP 5.0.1和更高版本上可用。
以下示例程序旨在說明PHP中的SimpleXMLElement::children()函數:
程序1:該程序顯示子節點的值。
<?php
// Loading XML document to $user
$user = <<<XML
<user>
<username>Geeks123</username>
<name>GeeksforGeeks</name>
<phone>+91-XXXXXXXXXX</phone>
<address>
Noida, UP, India
</address>
</user>
XML;
// Loading string as simple xml object
$xml = simplexml_load_string($user);
// Print children node
foreach($xml->children() as $child) {
echo "Child node: " . $child . "</br>";
}
?>
輸出:
Child node: Geeks123 Child node: GeeksforGeeks Child node: +91-XXXXXXXXXX Child node: Noida, UP, India
程序2:該程序顯示標簽名稱及其值。
<?php
// Loading XML document to $user
$user = <<<XML
<user>
<username>Geeks123</username>
<name>GeeksforGeeks</name>
<phone>+91-XXXXXXXXXX</phone>
<address>
Noida, UP, India
</address>
</user>
XML;
// Loading string as simple xml object
$xml = simplexml_load_string($user);
// Print children node
foreach($xml->children() as $child) {
echo "Child node: " . $child->getName()
. " => " . $child . "</br>";
}
?>
輸出:
Child node: username => Geeks123 Child node: name => GeeksforGeeks Child node: phone => +91-XXXXXXXXXX Child node: address => Noida, UP, India
程序3:該程序使用其值檢索子級屬性名稱。
<?php
// Loading XML document to $user
$user = <<<XML
<user>
<username font-color="green"
font="awsome-fonts" font-size="72px">
Geeks123
</username>
<name font-color="blue" font="awsome-fonts"
font-size="36px">
GeeksforGeeks
</name>
<phone font-color="blue" type="number"
font="awsome-fonts" font-size="24px">
+91-XXXXXXXXXX
</phone>
<address font-color="blue" font="awsome-fonts"
font-size="24px">
Noida, UP, India
</address>
</user>
XML;
// Loading string as simple xml object
$xml = simplexml_load_string($user);
// Print children attribute
foreach($xml->children() as $child) {
echo "Child name: " . $child->getName()
. " =>" . $child . "<br>";
foreach($child->attributes() as $key => $value)
echo " parameter: "
. $key . " => " . $value . "</br>";
}
?>
輸出:
Child name: username => Geeks123 parameter: font-color => green parameter: font => awsome-fonts parameter: font-size => 72px Child name: name => GeeksforGeeks parameter: font-color => blue parameter: font => awsome-fonts parameter: font-size => 36px Child name: phone => +91-XXXXXXXXXX parameter: font-color => blue parameter: type => number parameter: font => awsome-fonts parameter: font-size => 24px Child name: address => Noida, UP, India parameter: font-color => blue parameter: font => awsome-fonts parameter: font-size => 24px
參考: https://www.php.net/manual/en/simplexmlelement.children.php
相關用法
- PHP SimpleXMLElement saveXML()用法及代碼示例
- PHP SimpleXMLElement addAttribute()用法及代碼示例
- PHP SimpleXMLElement registerXPathNamespace()用法及代碼示例
- PHP SimpleXMLElement asXML()用法及代碼示例
- PHP SimpleXMLElement count()用法及代碼示例
- PHP SimpleXMLElement XPath()用法及代碼示例
- PHP SimpleXMLElement getDocNamespaces()用法及代碼示例
- PHP SimpleXMLElement addChild()用法及代碼示例
- PHP SimpleXMLElement getNamespaces()用法及代碼示例
- PHP SimpleXMLElement attributes()用法及代碼示例
- PHP SimpleXMLElement::getName()用法及代碼示例
- PHP SimpleXMLElement::__construct()用法及代碼示例
- HTML DOM children用法及代碼示例
注:本文由純淨天空篩選整理自gekcho大神的英文原創作品 PHP | SimpleXMLElement children() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。