PHP DOM: How to get child elements by tag name in an elegant manner?

后端 未结 3 1567
有刺的猬
有刺的猬 2020-12-04 01:53

I\'m parsing some XML with PHP DOM extension in order to store the data in some other form. Quite unsurprisingly, when I parse an element I pretty often need to obtain all c

3条回答
  •  情书的邮戳
    2020-12-04 02:28

    My solution used in a production:

    Finds a needle (node) in a haystack (DOM)

    function getAttachableNodeByAttributeName(\DOMElement $parent = null, string $elementTagName = null, string $attributeName = null, string $attributeValue = null)
    {
        $returnNode = null;
    
        $needleDOMNode = $parent->getElementsByTagName($elementTagName);
    
        $length = $needleDOMNode->length;
        //traverse through each existing given node object
        for ($i = $length; --$i >= 0;) {
    
            $needle = $needleDOMNode->item($i);
    
            //only one DOM node and no attributes specified?
            if (!$attributeName && !$attributeValue && 1 === $length) return $needle;
            //multiple nodes and attributes are specified
            elseif ($attributeName && $attributeValue && $needle->getAttribute($attributeName) === $attributeValue) return $needle;
        }
    
        return $returnNode;
    }
    

    Usage:

    $countryNode = getAttachableNodeByAttributeName($countriesNode, 'country', 'iso', 'NL');
    

    Returns DOM element from parent countries node by specified attribute iso using country ISO code 'NL', basically like a real search would do. Find a certain country by it's name in an array / object.

    Another usage example:

    $productNode = getAttachableNodeByAttributeName($products, 'partner-products');
    

    Returns DOM node element containing only single (root) node, without searching by any attribute. Note: for this you must make sure that root nodes are unique by elements' tag name, e.g. countries->country[ISO] - countries node here is unique and parent to all child nodes.

提交回复
热议问题