当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


PHP SimpleXMLElement children()用法及代码示例


前提条件: 阅读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



相关用法


注:本文由纯净天空筛选整理自gekcho大神的英文原创作品 PHP | SimpleXMLElement children() Function。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。