Simple XML – Dealing With Colons In Nodes

The solution is explained in this nice article. You need the children() method for accessing XML elements which contain a namespace. This code snippet is quoted from the article: $feed = simplexml_load_file(‘http://www.sitepoint.com/recent.rdf’); foreach ($feed->item as $item) { $ns_dc = $item->children(‘http://purl.org/dc/elements/1.1/’); echo $ns_dc->date; }

SimpleXML Reading node with a hyphenated name

Your assumption is correct. Use $officeXML->{‘document-meta’} to make it work. Please note that the above applies to Element nodes. Attribute nodes (those within the @attributes property when dumping the SimpleXmlElement) do not require any special syntax to be accessed when hyphenated. They are regularly accessible via array notation, e.g. $xml = <<< XML <root> <hyphenated-element … Read more

SimpleXML: Selecting Elements Which Have A Certain Attribute Value

Try this XPath: /object/data[@type=”me”] Which reads as: Select (/) children of the current element called object Select (/) their children called data Filter ([…]) that list to elements where … the attribute type (the @ means “attribute”) has the text value me So: $myDataObjects = $simplexml->xpath(‘/object/data[@type=”me”]’); If object is not the root of your document, … Read more

Remove a child with a specific attribute, in SimpleXML for PHP

Contrary to popular belief in the existing answers, each Simplexml element node can be removed from the document just by itself and unset(). The point in case is just that you need to understand how SimpleXML actually works. First locate the element you want to remove: list($element) = $doc->xpath(‘/*/seg[@id=”A12″]’); Then remove the element represented in … Read more