|
Red Squirrel Reflections
Dave Hoover explores the psychology of software development
|
|
Mon, 17 Jan 2005Quote 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:
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... |