adding a namespace when using SimpleXMLElement

SimpleXML has an unusual quirk where the namespace prefixes are filtered from the root element. I’m not sure why it does this. However, a workaround I’ve used has been to basically prefix the prefix, so that the parser only removes the first ones, and leaves the second $xmlTest = new SimpleXMLElement(‘<xmlns:ws:Test></xmlns:ws:Test>’, LIBXML_NOERROR, false, ‘ws’, true); … Read more

Remove namespace from XML using PHP

I found the answer above to be helpful, but it didn’t quite work for me. This ended up working better: // Gets rid of all namespace definitions $xml_string = preg_replace(‘/xmlns[^=]*=”[^”]*”/i’, ”, $xml_string); // Gets rid of all namespace references $xml_string = preg_replace(‘/[a-zA-Z]+:([a-zA-Z]+[=>])/’, ‘$1’, $xml_string);

Format output of $SimpleXML->asXML(); [duplicate]

There’s a variety of solutions in the comments on the PHP manual page for SimpleXMLElement. Not very efficient, but certainly terse, is a solution by Anonymous $dom = dom_import_simplexml($simpleXml)->ownerDocument; $dom->formatOutput = true; echo $dom->saveXML(); The PHP manual page comments are often good sources for common needs, as long as you filter out the patently wrong … Read more

How do you rename a tag in SimpleXML through a DOM object?

Here’s what’s probably the simplest way to copy a node’s children and attributes without using XSLT: function clonishNode(DOMNode $oldNode, $newName, $newNS = null) { if (isset($newNS)) { $newNode = $oldNode->ownerDocument->createElementNS($newNS, $newName); } else { $newNode = $oldNode->ownerDocument->createElement($newName); } foreach ($oldNode->attributes as $attr) { $newNode->appendChild($attr->cloneNode()); } foreach ($oldNode->childNodes as $child) { $newNode->appendChild($child->cloneNode(true)); } $oldNode->parentNode->replaceChild($newNode, $oldNode); } … Read more

How to get attribute of node with namespace using SimpleXML? [closed]

The code you’ve got in your question does work. You have correctly written that you can access an attribute from a namespaced-element that is not in the default namespace as the root-element by making use of the SimpleXMLElement::children() method with the XML-namespace related parameters $ns and $is_prefix: $youtube->entry->children(‘yt’, TRUE)->duration->attributes()->seconds; // is “1870” As this is … Read more

SimpleXML SOAP response Namespace issues

You have to use SimpleXMLElement::children(), though at this point it would probably be easier to use XPath. <?php $XmlStr = <<<XML <?xml version=”1.0″ encoding=”UTF-8″?> <env:Envelope xmlns:env=”http://www.w3.org/2003/05/soap-envelope” xmlns:ns1=”http://soap.xxxxxx.co.uk/” > <env:Body> <ns1:PlaceOrderResponse> <xxxxxOrderNumber></xxxxxOrderNumber> <ErrorArray> <Error> <ErrorCode>24</ErrorCode> <ErrorText>The+client+order+number+3002254+is+already+in+use</ErrorText> </Error> <Error> <ErrorCode>1</ErrorCode> <ErrorText>Aborting</ErrorText> </Error> </ErrorArray> </ns1:PlaceOrderResponse> </env:Body> </env:Envelope> XML; $XmlArray = new SimpleXMLElement($XmlStr); $t = $XmlArray->children(“env”, true)->Body-> children(“ns1”, true)->PlaceOrderResponse-> … Read more