Note that there are some explanatory texts on larger screens.

plurals
  1. POPHP DOM XML - Create Multiple Namespace Attributes?
    primarykey
    data
    text
    <p>I'm working on some PHP to create XML from a database using the DOM extension.</p> <p>Basically, I need to create a NameSpace and add 3 attributes to it:</p> <pre><code>&lt;NameSpaceName xmlns="uri:xxx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="uri:xxx"&gt; </code></pre> <p>The full code i've written is below:</p> <pre><code>include_once("includes/connect.php"); $sql = ("SELECT * FROM tableName"); $query = mysql_query($sql) or die("Error: " . mysql_error()); // create a new XML document $doc = new DomDocument('1.0', 'UTF-8'); // create root node $root = $doc-&gt;createElementNS('uri:xxx', 'PayerRecords'); $root = $doc-&gt;appendChild($root); $root-&gt;setAttributeNS('http://www.w3.org/2000/xmlns/' ,'xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance'); $root-&gt;setAttributeNS('http://www.w3.org/2000/xmlns/' ,'xsi:schemaLocation', 'uri:xxx'); // process one row at a time while($row = mysql_fetch_assoc($query)) { // add node for each row $occ = $doc-&gt;createElement('Content'); $occ = $root-&gt;appendChild($occ); // add a child node for each field foreach ($row as $fieldname =&gt; $fieldvalue) { $child = $doc-&gt;createElement($fieldname); $child = $occ-&gt;appendChild($child); $value = $doc-&gt;createTextNode($fieldvalue); $value = $child-&gt;appendChild($value); } // foreach } // while // get completed xml document $xml_string = $doc-&gt;saveXML(); echo $xml_string; </code></pre> <p>But when I execute the above I get this error:</p> <blockquote> <p>Fatal error: Uncaught exception 'DOMException' with message 'Namespace Error' in xml.php:21 Stack trace: #0 xml.php(21): DOMElement->setAttributeNS('<a href="http://www.w3.o" rel="noreferrer">http://www.w3.o</a>...', 'xsi:schemaLocat...', 'uri:xxx...') #1 {main} thrown in xml.php on line 21</p> </blockquote> <p>Line 21 is the second 'setAttributeNS' line.</p> <p>Can anyone see where i'm going wrong?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload