📜  PHP | SimpleXMLElement children()函数

📅  最后修改于: 2022-05-13 01:56:38.010000             🧑  作者: Mango

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

    Geeks123
    GeeksforGeeks
    +91-XXXXXXXXXX
    
        Noida, UP, India     
XML;   // Loading string as simple xml object $xml = simplexml_load_string($user);   // Print children node foreach($xml->children() as $child) {     echo "Child node: " . $child . "
"; }   ?>


php

    Geeks123
    GeeksforGeeks
    +91-XXXXXXXXXX
    
        Noida, UP, India     
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 . "
"; }   ?>


php

    
        Geeks123
    
     
    
        GeeksforGeeks
    
     
    
        +91-XXXXXXXXXX
    
     
    
        Noida, UP, India     
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 . "
";           foreach($child->attributes() as $key => $value)         echo "      parameter: "                 . $key . " => " . $value . "
"; }   ?>


输出:

Child node: Geeks123
Child node: GeeksforGeeks
Child node: +91-XXXXXXXXXX
Child node: Noida, UP, India 

程序 2:该程序显示标签名称及其值。

PHP


    Geeks123
    GeeksforGeeks
    +91-XXXXXXXXXX
    
        Noida, UP, India     
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 . "
"; }   ?>

输出:

Child node: username => Geeks123
Child node: name => GeeksforGeeks
Child node: phone => +91-XXXXXXXXXX
Child node: address => Noida, UP, India 

程序 3:该程序检索子属性名称及其值。

PHP


    
        Geeks123
    
     
    
        GeeksforGeeks
    
     
    
        +91-XXXXXXXXXX
    
     
    
        Noida, UP, India     
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 . "
";           foreach($child->attributes() as $key => $value)         echo "      parameter: "                 . $key . " => " . $value . "
"; }   ?>

输出:

Child name: username => Geeks123 
     parameter: font-color => green
     parameter: font => awesome-fonts
     parameter: font-size => 72px
Child name: name => GeeksforGeeks 
     parameter: font-color => blue
     parameter: font => awesome-fonts
     parameter: font-size => 36px
Child name: phone => +91-XXXXXXXXXX 
     parameter: font-color => blue
     parameter: type => number
     parameter: font => awesome-fonts
     parameter: font-size => 24px
Child name: address => Noida, UP, India 
     parameter: font-color => blue
     parameter: font => awesome-fonts
     parameter: font-size => 24px

参考: https://www. PHP.net/manual/en/simplexmlelement.children。 PHP