Red Squirrel Reflections
Dave Hoover explores the psychology of software development

Dave Hoover
dave.hoover@gmail.com

Categories
All [Atom]
Craftsmanship [Atom]
Dynamic [Atom]
Intersection [Atom]
Learning [Atom]
Links [Atom]
Polyglot [Atom]
Projects [Atom]
XP [Atom]
Old Blog

Obtivian Blogs

Andy Maleh
Colin Harris
Fred Polgardy
Jim Breen
Kevin Taylor
Todd Webb
Turner King
Tyler Jennings

Archives

March 2009 (1)
January 2009 (1)
December 2008 (1)
October 2008 (3)
September 2008 (1)
June 2008 (4)
April 2008 (3)
March 2008 (1)
February 2008 (1)
August 2007 (1)
July 2007 (1)
June 2007 (1)
May 2007 (4)
April 2007 (3)
March 2007 (5)
February 2007 (6)
January 2007 (6)
December 2006 (10)
November 2006 (5)
October 2006 (8)
September 2006 (8)
August 2006 (5)
July 2006 (12)
June 2006 (7)
May 2006 (5)
April 2006 (5)
March 2006 (4)
February 2006 (2)
January 2006 (5)
December 2005 (5)
November 2005 (3)
October 2005 (3)
September 2005 (6)
August 2005 (4)
July 2005 (7)
June 2005 (14)
May 2005 (6)
April 2005 (8)
March 2005 (9)
February 2005 (11)
January 2005 (16)
Old Archives

 

Mon, 17 Jan 2005

Quote Manager Project -- Day 3

An ongoing pet project blog...

Looks like the code snippet I took from the Java XSL tutorial uses DOM parsing (as opposed to SAX parsing). This doesn't mean much to me, other than pointing me toward the correct documentation. OK, the documentation sucks. I'm going to experiment...

Made some progress. Experimenting with a nice Java IDE like IDEA turned out to be more useful than the documentation. Here's what I have so far. This tells me that I have 6 quotes in my data file...

        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document document = builder.parse("sample.xml");

        NodeList quotes = document.getElementsByTagName("quote");
        System.out.println(quotes.getLength());
Still trying to unwind this XML. I'm having flashbacks to using Perl's XML::Parser. OK, looks like I've got a handle on it. Here's what I have now...it prints out all of the child tags of each quote, and accesses the isbn attribute of the source tag:
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document document = builder.parse("sample.xml");

        NodeList quotes = document.getElementsByTagName("quote");
        for (int i = 0; i < quotes.getLength(); i++) {
            NodeList children = quotes.item(i).getChildNodes();
            for (int j = 0; j < children.getLength(); j++) {
                Node node = children.item(j);
                String nodeName = node.getNodeName();
                if (nodeName == "#text") continue;
                System.out.print(nodeName + ":");
                if (nodeName == "source") {
                    NamedNodeMap attributes = node.getAttributes();
                    Node isbn = attributes.getNamedItem("isbn");
                    if (isbn != null)
                        System.out.print(isbn + ":");
                }
                System.out.println(node.getTextContent().trim());
            }
            System.out.println("-----------------");
        }
Here is the output:
text:We in the software industry are working with a more or less invisible product,
			yet this very invisibility only heightens our need for feedback.
quoter:GeraldWeinberg
source:isbn="0932633447":Project Retrospectives
author:NormKerth
-----------------
text:By building tests before you implement the code,
			you get to try out the interface before you commit to it.
source:isbn="020161622X":The Pragmatic Programmer
author:DaveThomas
author:AndyHunt
-----------------
text:For everyone who exalts himself will be humbled,
			and he who humbles himself will be exalted.
source:Luke 18:14b
-----------------
text:Programming is the Great Game.
			It consumes you, body and soul.
			When you're caught up in it, nothing else matters.
source:How Software Companies Die
author:Orson ScottCard
-----------------
text:It's never the size of the step that a person takes that counts,
			but its direction.
source:isbn="0393700984":Narrative Means to Therapeutic Ends
author:MichaelWhite
author:DavidEpston
-----------------
text:Having a plan isn't everything, but planning is.
source:isbn="0201708426":Extreme Programming Installed
author:RonJeffries
author:AnnAnderson
author:ChetHendrickson
-----------------
I'm in the station now. Looks like updating XML files will wait until the ride home.

OK, let's see how to update an XML document. I want to change Jerry's URL to http://ayeconference.com/...

Got it. It works like this:

        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document document = builder.parse("sample.xml");

        NodeList quoters = document.getElementsByTagName("quoter");
        NamedNodeMap jerryAttributes = quoters.item(0).getAttributes();

        Node node = jerryAttributes.getNamedItem("url");
        node.setNodeValue("http://ayeconference.com/");
But that doesn't change the actual file, just the in-memory XML structure. How do I write the Document to a file? Judging from the public methods I see in IDEA, it doesn't look like a Document knows how to write itself, or even provide an OutputStream. Back to the tutorial...

Oh, it looks like I already had it. Here's the code. It should look familiar:

        String xmlFile = "sample.xml";

        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document document = builder.parse(xmlFile);

        NodeList quoters = document.getElementsByTagName("quoter");
        NamedNodeMap jerryAttributes = quoters.item(0).getAttributes();

        Node node = jerryAttributes.getNamedItem("url");
        node.setNodeValue("http://ayeconference.com/");

        Transformer transformer = TransformerFactory.newInstance().newTransformer();

        Source source = new DOMSource(document);
        Result result = new StreamResult(new FileOutputStream(xmlFile));
        transformer.transform(source, result);

This is just like the code I used to generate HTML, except I don't specify an XSL file in my call to TransformerFactory.newInstance().newTransformer(). This code updates the file lickety-split. Cool.

Time for a check in.

As the customer, I'd like to get an update on the progress of the project. As the developer, I will give my appraisal of how far I've come:

  • I want to change the quotes page to be sorted by quoter author. DONE. There should be a list of all the quoters at the top. NOT DONE. Their names should link down to their quotes on the page below. NOT DONE.
  • I want to use a simple Swing application to manage the quotes. NOT DONE.
  • The Swing app will store the quotes in XML. NOT DONE. The app will have the ability to transform this XML into HTML via XSLT. NOT DONE.
Dang, that's depressing. As the customer, I'm disappointed. As the developer looking at the plan, I am nervous about how little I actually got done. But my confidence about how to accomplish these tasks is much, much higher.

So what is my next task? It's time to view the quote data in a Swing app. Finally! Some actual coding! No more spiking!

Crap, my stop is coming up. This will have to wait until tomorrow...

[/projects/quotes] permanent link


powered by blosxom