Xml documents should never be passed as String, how to motivate?

I feel very uncomfortable seeing method signatures, such as:

public void foo(String theXml);

What will foofo do if I pass a line starting with a UTF-16 XML declaration? In my opinion, XML should be entered as a strong XML type, such as a DOM tree or a byte array. Therefore, foo should be updated as follows:

public void foo(Byte[] theXml);

As a rule, the programmer will uselessly assume that when reading theXmlhas a certain encoding. Often just hoping that the standard one for the file library will be correctly guessed.

How can I motivate this to my colleagues?

Incompatible document header encoding declarations are too weak. (Using a strong type or byte array will prevent the naive parsing / modification of the document.) I have seen many times when encoding is interrupted due to this kind of error.

+3
source share
4 answers

Make sure your test suite has a test that will fail if XML is not correctly processed in String. If the explanation of the problem does not motivate, showing that this is happening ...

+7
source

XML String , byte[] - , . , , , -. (- , , , , , . , byte[], - .)

, , java.io.Reader System.IO.TextReader ( XML ), : unparsed XML - , InputStream ( , ) Reader ( - ). , .

, :

public void foo(String theXml);
public void foo(byte[] theXml);
// Usage:
foo("<document />");
foo("<?xml version='1.0' encoding='UTF-8' ?><document />".getBytes("UTF-8"));

... :

public void foo(Reader source);
public void foo(InputStream source);
// Usage:
foo(new StringReader("<document />"));
foo(new ByteArrayInputStream("<?xml version='1.0' encoding='UTF-8' ?><document />"
                             .getBytes("UTF-8")));
+6

, , , , : foo(Encoding.Default.GetBytes(theString)). , , , string .

, - , - , , , , .

, . .

0

. , , . , , .

, , .

0

All Articles