Note that there are some explanatory texts on larger screens.

plurals
  1. POPHP invalid character error
    primarykey
    data
    text
    <p>I'm getting this error when running this code: <code>Fatal error: Uncaught exception 'DOMException' with message 'Invalid Character Error' in test.php:29 Stack trace: #0 test.php(29): DOMDocument->createElement('1OhmStable', 'a') #1 {main} thrown in test.php on line 29 </code></p> <p>The nodes that from the original XML file do contain invalid characters, but as I am stripping the invalid characters away from the nodes, the nodes should be created. What type of encoding do I need to do on the original XML document? Do I need to decode the saveXML?</p> <pre><code>function __cleanData($c) { return preg_replace("/[^A-Za-z0-9]/", "",$c); } $xml = new DOMDocument('1.0', 'UTF-8'); $xml-&gt;load('test.xml'); $xml-&gt;formatOutput = true; $append = array(); foreach ($xml-&gt;getElementsByTagName('product') as $product ) { foreach($product-&gt;getElementsByTagName('name') as $name ) { $append[] = $name; } foreach ($append as $a) { $nodeName = __cleanData($a-&gt;textContent); $element = $xml-&gt;createElement(htmlentities($nodeName) , 'a'); } $product-&gt;removeChild($xml-&gt;getElementsByTagName('details')-&gt;item(0)); $product-&gt;appendChild($element); } $result = $xml-&gt;saveXML(); $file = "data.xml"; file_put_contents($file,$result); </code></pre> <p>This is what the original XML looks like:</p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;?xml-stylesheet href="/v1/xsl/xml_pretty_printer.xsl" type="text/xsl"?&gt; &lt;products&gt; &lt;product&gt; &lt;modelNumber&gt;M100&lt;/modelNumber&gt; &lt;itemId&gt;1553725&lt;/itemId&gt; &lt;details&gt; &lt;detail&gt; &lt;name&gt;1 Ohm Stable&lt;/name&gt; &lt;value&gt;600 x 1&lt;/value&gt; &lt;/detail&gt; &lt;/details&gt; &lt;/product&gt; &lt;/products&gt; </code></pre> <p>The new document is supposed to look like this:</p> <pre><code> &lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;?xml-stylesheet href="/v1/xsl/xml_pretty_printer.xsl" type="text/xsl"?&gt; &lt;products&gt; &lt;product&gt; &lt;modelNumber&gt;M100&lt;/modelNumber&gt; &lt;itemId&gt;1553725&lt;/itemId&gt; &lt;1 Ohm Stable&gt; &lt;/1 Ohm Stable&gt; &lt;/product&gt; &lt;/products&gt; </code></pre>
    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.
 

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