children() function in PHP XML

From a XML string or tag we can get the children of a given node. We will be using simpleXMLElement object for this. Here is the XML string.

xml-sample1.php

<?Php
$str_xml = <<<XML
<?xml version='1.0' standalone='yes'?>
<details>
<name>Abcd</name>
<address>
  	<door_no>234-ac</door_no>
	<street>Great Wall</street>
	<city>Alabama</city>
</address>

</details>
XML;
?>
We have also used getName to display the tag name along with the children function. Here is the php code.
<?php
require "xml-sample1.php";
$main1 = new SimpleXMLElement($str_xml);

foreach ($main1->children() as $child1) {
echo  '<br><b>getName</b> : '.$child1->getName().' <b>children</b> : '. $child1;
foreach ($child1->children() as $child2) {
echo  '<br><b>getName</b> : '.$child2->getName().' <b>children</b> : '. $child2;
}
}

?>
The output is
getName : name children : Abcd
getName : address children :
getName : door_no children : 234-ac
getName : street children : Great Wall
getName : city children : Alabama

Use the xml file file-xml-demo.php

$xml = simplexml_load_file('file-xml-demo.xml');
   
 echo $xml->details[0]->name;     // Output John Deo 
 echo $xml->details[0]->count(); // Output 3 
 echo "<br>";
 echo $xml->details[2]->name;     // Output Arnold 
To display all the data.
$xml = simplexml_load_file('file-xml-demo.xml');

foreach ($xml as $child1) {
echo  "<br>". $child1->getName()." : $child1";
foreach ($child1 as $child2) {
echo "<br>". $child2->getName()." : $child2";
}
}

Example: Looping through Child Elements

$xml = <<<XML
<bookstore>
    <book>
        <title>PHP Basics</title>
        <author>John Doe</author>
    </book>
    <book>
        <title>Learning XML</title>
        <author>Jane Smith</author>
    </book>
</bookstore>
XML;

$xmlObject = simplexml_load_string($xml);
foreach($xmlObject->children() as $book) {
    echo $book->title . " by " . $book->author . "<br>";
}

Example: Handling Nested Child Elements

$xml = <<<XML
<bookstore>
    <book>
        <title>PHP Advanced</title>
        <author>John Doe</author>
        <publisher>
            <name>TechBooks</name>
            <year>2021</year>
        </publisher>
    </book>
</bookstore>
XML;

$xmlObject = simplexml_load_string($xml);
foreach($xmlObject->book->children() as $child) {
    if ($child->getName() == "publisher") {
        foreach($child->children() as $publisherDetail) {
            echo $publisherDetail->getName() . ": " . $publisherDetail . "<br>";
        }
    }
}

Example: Handling Missing Child Elements

$xml = <<<XML
<bookstore>
    <book>
        <title>PHP Advanced</title>
        <author>John Doe</author>
    </book>
</bookstore>
XML;

$xmlObject = simplexml_load_string($xml);

// Check if 'publisher' exists
if (isset($xmlObject->book->publisher)) {
    echo "Publisher: " . $xmlObject->book->publisher->name;
} else {
    echo "Publisher info not available.";
}

Example: Error Handling for Malformed XML

$xml = "<bookstore><book><title>PHP Advanced</title></bookstore>"; // missing closing tag for <book>

libxml_use_internal_errors(true);  // Suppress errors
$xmlObject = simplexml_load_string($xml);

if ($xmlObject === false) {
    echo "Failed to load XML. Errors: ";
    foreach (libxml_get_errors() as $error) {
        echo $error->message;
    }
    libxml_clear_errors();
} else {
    echo "XML loaded successfully.";
}

XML getName()

Subscribe to our YouTube Channel here


Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com







    Post your comments , suggestion , error , requirements etc here





    PHP video Tutorials
    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2024 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer