highwire / better-dom-document
BetterDOMDocument是一个方便的PHP实用类,用于处理XML。它是PHP内置的DOMDocument的包装器,提供了一组便捷的快捷方式,使PHP中的XML处理变得轻松。它对命名空间和xpath查询提供了强大的内置支持。
3.0.16
2024-09-14 11:29 UTC
Requires
- symfony/css-selector: v7.1.1
Requires (Dev)
- phpunit/phpunit: 11.3.4
This package is not auto-updated.
Last update: 2024-09-23 04:45:18 UTC
README
BetterDOMDocument是一个方便的PHP实用类,用于处理XML。它是PHP内置的DOMDocument的包装器,提供了一组便捷的快捷方式,使PHP中的XML处理变得轻松。它对命名空间和xpath查询提供了强大的内置支持。
composer require highwire/better-dom-document
<?php use BetterDOMDocument\DOMDoc; // We can load a new BetterDOMDocument from either a string or a DOMNode object $dom = new DOMDoc($xmlstring); // It's easy to output the entire document as an array, which is sometimes easier to work with in PHP $array = $dom->getArray(); // It's easy to query too! $node_list = $dom->xpath('//xpath/to/node', $optional_context_node); // If you know you're only going to find a single DOMNode, you can use a querySingle $dom_node = $dom->xpathSingle('//xpath/to/node'); // Swapping out DOMNodes is really easy $dom->replace($dom_node, $replacementNode); // Removing a node is easy $dom->remove($dom_node); // Most places where you want to pass a DOMNode or element, you can just pass an xpath instead $dom->remove('//xpath/to/element/to/remove'); // The same goes for replacments $dom->replace('//xpath/to/replace', '<xml>You can pass a string, a DOMNode, or a document</xml>'); // It's easy to output the XML $xml = $dom->out(); // Or just output a single DOMNode $xml = $dom->out($dom_node); // Working with namespaced documents is made really easy $xml = ' <entry xmlns="http://www.w3.org/2005/Atom" xmlns:nlm="http://schema.highwire.org/NLM/Journal"> <author> <name>Li Xu</name> <nlm:name name-style="eastern"> <nlm:surname>Li</nlm:surname> <nlm:given-names>Xu</nlm:given-names> </nlm:name> </author> </entry>'; // If your document (like the one above) has a default namespace, you should declare // it's prefix as the second value when constructing a new BetterDOMDocument $dom = new DOMDoc($xml, 'atom'); // We register the 'atom' prefix against the default namespace // Now we can do mixed namespace queries! $surname = $dom->querySingle('//atom:author/nlm:name/nlm:surname')->nodeValue; // If you need to register another namespace before doing a query, thats a snap. // Note that by default all namespace declarations in the root element are automatically registered. $dom->registerNamespace('kml','http://www.opengis.net/kml/2.2'); // If you want to query with CSS selectors, no problem! $dom->select('nlm:name[@name-style="eastern"]');