I have to create an XML body for a web service call. I want to use:
private void createProductBody(Product product, StringBuilder body) {
body.append(WSConstants.WS_PRODUCT_ID_START_TAG)
.append(product.getId())
.append(WSConstants.WS_PRODUCT_ID_END_TAG)
.append(WSConstants.WS_SIZE_TEXT_START_TAG)
.append(product.getSize())
.append(WSConstants.WS_SIZE_TEXT_END_TAG);
}
But some colleagues state that this writing is hard to read and they want us to use this:
private void createProductBody(Product product, StringBuilder body) {
body.append("<ProductId>").append(product.getId()).append("</ProductId>")
.append("<Size>").append(product.getSize()).append("</Size>");
}
Which are the pro and cons for using hardcoded values?
WSConstantsa class that you wrote, or is it part of some library that you are using? \$\endgroup\$XmlBuilder. You need to be careful constructing XML by hand because the values may need to be escaped. E.g. if a product id were changed to be something like this"Mills & Boon"your XML would be invalid. The ampersand needs to be escaped as&. In your example, it's probably OK because the values are probably only numbers. \$\endgroup\$