Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>One key point in programming is to reduce the complexity of some code. That includes reducing the indentation so that there is not so much code stuck into each other. That often is hard to follow.</p> <p>For example, the if-clause that is inside your while body can be reduced to a great extend, moving the inner already one level up:</p> <pre><code>while ($row = mysql_fetch_assoc($result)) { if (empty($row)) { continue; } $title = $row['title']; ... } </code></pre> <p>The <code>continue</code> inside the loop just says: next iteration.</p> <p>There is also indentation for the XML tags you create. Not all can be prevented, however, some can. The <a href="http://php.net/xmlwriter_write_element" rel="nofollow noreferrer"><code>XMLWriter::writeElement()</code></a> method for example allows to output a whote element including it's inner text. This allows to reduce the following three lines:</p> <pre><code>$xml-&gt;startElement('loc'); $xml-&gt;writeRaw('http://domain.com/article/'); $xml-&gt;endElement(); </code></pre> <p>To a single one:</p> <pre><code>$xml-&gt;writeElement('loc', 'http://domain.com/article/'); </code></pre> <p>As there are multiple groups of such lines, the code actually now is pretty shortened. The end can be improved as well by ending the document, then there even is no need to flush. To make the indentation more readable, you can also make use of square brackets to express the indentation:</p> <pre><code>while ($row = mysql_fetch_assoc($result)) { if (empty($row)) { continue; } $title = $row['title']; $xml-&gt;writeElement('loc', 'http://domain.com/article/'); $xml-&gt;startElement('news:news'); { $xml-&gt;startElement("news:publication"); { $xml-&gt;writeElement("news:name", 'Name'); $xml-&gt;writeElement("news:language", 'en'); } $xml-&gt;endElement(); $xml-&gt;writeElement('news:title', $title); } $xml-&gt;endElement(); } $xml-&gt;endDocument(); </code></pre> <p>So this is not only more readable, the good news is that the case where you did run into an error is fixed, too. That is because the <a href="http://php.net/xmlwriter_write_raw" rel="nofollow noreferrer"><code>XMLWriter::writeRaw()</code></a> method has been removed. What that function does it write raw text, that means unescaped:</p> <pre><code>$title = 'hackers &lt;3 noodles'; $xml-&gt;startElement('news:title'); $xml-&gt;writeRaw($title); $xml-&gt;endElement(); </code></pre> <p>Output:</p> <pre><code>&lt;news:title&gt;hackers &lt;3 noodles&lt;/news:title&gt; ^ </code></pre> <p>As the output demonstrates, the <code>&lt;</code> character went into the output verbatim. Depending on the title, even pure XML codes can be injected destroying the whole document structure and leading to the error. Using <code>XMLWriter::writeElement()</code> is immune to that:</p> <pre><code>$title = 'hackers &lt;3 noodles'; $xml-&gt;writeElement('news:title', $title); </code></pre> <p>Output:</p> <pre><code>&lt;news:title&gt;hackers &amp;lt;3 noodles&lt;/news:title&gt; ^^^^ </code></pre> <p>As the output demonstrates, the propery XML Entity is used here preserving the document structure.</p> <p>So the method you were originally looking for then is <a href="http://php.net/xmlwriter_text" rel="nofollow noreferrer"><code>XMLWriter::text()</code></a>. But you don't need it any longer for this case, because the optimized code does not have that problem any longer. All text output is properly encoded through <code>XMLReader::writeElement()</code>. See as well <a href="https://stackoverflow.com/q/12844599/367456"><em>Retain XML code when using PHP XMLWriter::writeElement</em></a> which is about the same topic but from the opposite.</p> <p>I hope this is still of use for you as the question was a little older.</p>
 

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