Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Passing XML as a <code>String</code> is no more wrong than passing it as a <code>byte[]</code> - if the input is already decoded, you can happily ignore the decoding hints in the processing directives. You worry about making incorrect assumptions about encodings, but if you use strings, encodings are a non-issue since you don't have to decode anything. (Someone could of course be constructing the strings from raw bytes and make incorrect assumptions at that point, which of course <em>would</em> be a problem. But then again, if you take a <code>byte[]</code> argument, someone might already have a string and convert it to bytes using an incorrect charset.)</p> <p>Parsing string input isn't stranger than parsing input from a decoding source like <code>java.io.Reader</code> or <code>System.IO.TextReader</code> (most XML parsers let you do so), which brings us to another topic: <strong>I would recommend against passing unparsed XML as strings or byte arrays altogether</strong> - you get more flexibility if you work with <code>InputStream</code>s (for sources you don't know the encoding of) and <code>Reader</code>s (if you do know the encoding, or if the data for some reason doesn't need to be decoded). You also get the benefit that the whole document doesn't need to reside in memory before it's parsed. </p> <p>So, instead of this:</p> <pre><code>public void foo(String theXml); public void foo(byte[] theXml); // Usage: foo("&lt;document /&gt;"); foo("&lt;?xml version='1.0' encoding='UTF-8' ?&gt;&lt;document /&gt;".getBytes("UTF-8")); </code></pre> <p>... you'd have:</p> <pre><code>public void foo(Reader source); public void foo(InputStream source); // Usage: foo(new StringReader("&lt;document /&gt;")); foo(new ByteArrayInputStream("&lt;?xml version='1.0' encoding='UTF-8' ?&gt;&lt;document /&gt;" .getBytes("UTF-8"))); </code></pre>
 

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