Viewed   59 times

I'm iterating through a set of SimpleXML objects, and I can't figure out how to access each object's parent node. Here's what I want:

$divs = simplexml->xpath("//div");
foreach ($divs as $div)
{
   $parent_div = $div->get_parent_node(); // Sadly, there's no such function.
}

Seems like there must be a fairly easy way to do this.

 Answers

3

You could run a simple XPath query to get it:

$parent_div = $div->xpath("parent::*");

And as this is Simplexml and it only has element and attribute nodes and a parent node can only be an element and never an attribute, the abbreviated syntax can be used:

$parent_div = $div->xpath("..");

(via: Common Xpath Cheats - SimpleXML Type Cheatsheet (Feb 2013; by hakre) )

Friday, September 9, 2022
 
joro
 
2

Not sure why you used namespace notaion in the first place(the sample xml is not namespaced)

In your xpath, you need to select all condition/normal tags, not the condition tag as you were doing... Also, xpath() returns a list, so foreach over it. You don't need to access it as children, unless you want to parse the children of $child. There it would make sense, and it would work as expected.

foreach ($xml->xpath("/Condition/Normal") as $child) {
    echo $child["type"] . "="  . $child->getName()."<br/>";
}

outputs

TEMPERATURE=Normal
LOAD=Normal
POSITION=Normal
Monday, September 12, 2022
 
1

You can use the SimpleXMLElement::xpath method to query for the specific UNIT that you want with an XPath query like //UNIT[@index=2].

$value = intval($_GET['index']);
$xml   = new SimpleXMLElement('demo.xml',NULL,true);
$units = $xml->xpath("//UNIT[@index=$value]"); // xpath returns an array
if (isset($units[0])) {
    echo $units[0]->ID;
} else {
    echo "No unit with index $value";
}
Friday, August 5, 2022
 
sagar
 
5

You were probably not "cycling" the countries and cities:

<?php
    $xml_file = '/path';
    $xml = simplexml_load_file($xml_file);

    foreach($xml->Continent as $continent) {
        echo "<div class='continent'>".$continent['Name']."<span class='status'>".$continent['Status']."</span>";
        foreach($continent->Country as $country) {
            echo "<div class='country'>".$country['Name']."<span class='status'>".$country['Status']."</span>";
            foreach($country->City as $city) {
                echo "<div class='city'>".$city['Name']."<span class='status'>".$city['Status']."</span></div>";
            }
            echo "</div>"; // close Country div
        }
        echo "</div>"; // close Continent div
    }
?>
Sunday, August 14, 2022
4

You can probably do it with XPath or something, but SimpleXMLElement::xpath() returns an array that is easy to sort:

usort($xQuery, function ($a, $b) { return strcmp($a->Make, $b->Make); });
foreach ($xQuery as $results) {
    // …
}
Saturday, September 24, 2022
 
madness
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :