Extracting Node Attributes from XML with PHP's DOM Parser
When working with XML data in PHP, the DOM parser offers a robust API for accessing and manipulating nodes. One common task is extracting attributes from XML nodes.
In this specific case, we're interested in extracting the path attribute from the following XML markup:
<files> <file path="http://www.thesite.com/download/eysjkss.zip" title="File Name" /> </files>
Using SimpleXML
SimpleXML is a convenient PHP library for parsing XML documents. It provides a simple and intuitive API that makes it easy to work with XML data. To extract the path attribute using SimpleXML:
<code class="php">$xml = new SimpleXMLElement($xmlstr); echo $xml->file['path']."\n";</code>
In this code, $xmlstr represents the XML string to be parsed. The SimpleXMLElement constructor creates a SimpleXML object that represents the XML document. To access the path attribute, we can use the ['path'] notation.
Output:
http://www.thesite.com/download/eysjkss.zip
The above is the detailed content of How to Extract Node Attributes from XML using PHP\'s DOM Parser and SimpleXML?. For more information, please follow other related articles on the PHP Chinese website!