Friday, September 26, 2008

Pretty Print XML in Java

Something I keep forgetting how to do. Obviously something would need to be done to make this any sort of utility class. This is simply an example of the classes that can be used to perform pretty print of an xml string. There are many more ways to get the actual string from a file or Document.

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;

import org.w3c.dom.Document;

public class XmlTest {

public static void main(String[] args) throws Exception {
String xmlString = "<tag><nested>hello</nested></tag>";
DocumentBuilder documentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();

InputStream inputStream = new ByteArrayInputStream(xmlString.getBytes());
Document document = documentBuilder.parse(inputStream);

XmlTest xmlTest = new XmlTest();
xmlTest.serialize(document, System.out);


public void serialize(Document doc, OutputStream out) throws Exception {

TransformerFactory tfactory = TransformerFactory.newInstance();
Transformer serializer;
try {
serializer = tfactory.newTransformer();
//Setup indenting to "pretty print"
serializer.setOutputProperty(OutputKeys.INDENT, "yes");
serializer.setOutputProperty("{}indent-amount", "2");

DOMSource xmlSource = new DOMSource(doc);
StreamResult outputTarget = new StreamResult(out);
serializer.transform(xmlSource, outputTarget);
} catch (TransformerException e) {
// this is fatal, just dump the stack and throw a runtime exception

throw new RuntimeException(e);

Monday, September 22, 2008

I drafted this description of services to give non-technical individuals understanding of the services of a Service Oriented Architecture (SOA). I refined it a smidge here from its original context. It is not a complete, thorough example. It focuses on giving a definition to the word service.
We use services everyday. An example is that you don't have to be a barista to drink an espresso. You simply order, pay, and enjoy your beverage. This analogy has a good correlation with the service orientation of software architecture. In software we call this a black box. You don't know what happens inside the box. You put in money and espresso comes out. A service consumer needs no knowledge of how a provider has implemented a service. The Consumer need only know the input required and output expected from the service. In our example of the espresso service, a barista takes money as input and outputs espresso. As a consumer, I need not know how to grow, harvest, roast, and brew coffee beans.

There are cost and quality aspects to consider. How much will it cost the service consumer, who has no prior knowledge of coffee beans, to go from plant to espresso? Would the quality be the same as when the coffee experts do the same thing? Clearly the answer is to leave espresso and the service logic to the experts. This frees each consumer to put full effort into their own area of expertise.

Tuesday, September 16, 2008

Welcome and Thank You

There is such a wealth of information on the internet. I am continually amazed. I have such an expectation that someone else already gave a good answer for any question, that I won't need to provide answers of my own. I do however need a space where I can catalog the answers I have found. Who knows, maybe I'll eventually find a topic that I can provide a novel approach to solving. Should it happen I can use this space to share and give back to the community from which I have gained so much.

So I say welcome to my blog. I also say thank you. Thank you, internet, for all the answers past, present, and future.