XML – Johan Känngård http://johankanngard.net Running, coding and mind dumps Thu, 01 Dec 2016 18:23:26 +0000 sv-SE hourly 1 Managing logging with Log4j and Chainsaw http://johankanngard.net/2007/06/19/managing-logging-with-log4j-and-chainsaw/ Tue, 19 Jun 2007 11:52:32 +0000 http://johankanngard.net/2007/06/19/managing-logging-with-log4j-and-chainsaw/ […]]]> Those of you who haven’t used System.out.println(”My variable: ” + myVariable) in some Java classes to get debug information to the console, raise your hands please. None? 😉

I am very fond of logging as a complement to debugging, since I can ”trace” actions in my programs even if the classes is in production. ”But hey, logging makes the application go slower!” you might think. Well, it’s up to you to decide, a small performance hit is likely to occur, a big hit if you don’t read the documentation of for instance the PatternLayout.
I use Log4j a lot in the Java code I design and write, and haven’t noticed any big difference between logging and not logging.
So, you might have used Log4j a bit and used Chainsaw to look at Log4j logging files. Why not look at the logging in realtime? Here is how to do it.

Setting up Log4j

To see the logging in real time, I use an appender called SocketHubAppender that acts as a server. Create the log4j.xml in the root of your Java application’s directory, or add the vital bits to your own:

< ?xml version="1.0" encoding="UTF-8" ?>
< !DOCTYPE log4j:configuration SYSTEM "log4j.dtd">





When starting your application and outputs some logging messages with the usual logger.debug(”Hello world!”); it will be sent to the appender’s socket, defined above as 1020. The output can’t be read using a telnet session, because the things you will see is the logging events serialized… You need something to look at those serialized logging events with – Chainsaw!

Configure Chainsaw

Create a file (or add the vital bits below to your own…) called chainsaw-config.xml somewhere on your local disk, for instance c:tempchainsaw-config.xml, and change the host from my.remote.host.net to the host where you set up the logging with the SocketHubAppender and that the port matches the one you used:

< ?xml version="1.0" encoding="UTF-8" ?>
< !DOCTYPE log4j:configuration >






Start Chainsaw via Java Web Start by launching the jnlp-file at the homepage. Open the menu ”View”, ”Show Application-wide Preferences”, go to the ”General” tab and enter ”file://c:/temp/chainsaw-config.xml” (leave out the quotation marks) in the ”Automatic Configuration URL” field, or whatever you named your config file. Click ”OK” and restart Chainsaw. You should now see your receiver to the right named ”MyRemoteLogging”.
Try starting up your Java application and output some logging, and you will see them pop up directly in Chainsaw!

So why not use a simple file or database to log to? You can do that too, you are not limited to one appender per application. You can set up as many as you want, for instance a ConsoleAppender, a SocketHubAppender and a JDBCAppender that logs the events to all those targets.
Log4j is powerful, but it doesn’t makes miracles if you don’t use some structured logging. Events like ”Hello world!” is not going to help you, but some well designed NDC.push()’s and other will do good. And don’t forget to add the NotesExceptionRenderer to your class path, so that you get more info from those badly-designed exceptions :-)

]]>
g33k date http://johankanngard.net/2007/05/25/g33k-date/ http://johankanngard.net/2007/05/25/g33k-date/#comments Fri, 25 May 2007 09:59:54 +0000 http://johankanngard.net/2007/05/25/g33k-date/ […]]]> Johan defeating Darth Vader
I was a key speaker at the first g33k date ever yesterday, and it was Ekakan that held the meeting in their small but great office at Saltmätargatan in Stockholm. Quite a lot showed up, many of which I know by name, but not by face, since they are all bloggers with no pictures at their sites! :-) After having slurped down some well-known red-canned sugary liquid, I started my speech using OpenOffice Impress as presenter application. The slides can be found at the bottom of this post. It has not been cleaned up, so some references are to my local file system and applications on my Domino server will not work. I have put the database I used for the web service stuff as an attachment below, but use it at your own risk!
I talked briefly about who I am, what I like, what my shoe size is and other important stuff. A short history on how the web has evolved over time (1.0, 1.5, 2.0) and my own suggestion of the next 3.0 level, which includes web services on the client side. I demonstrated a simple AJAX solution for typeahead with script.aculo.us and Domino and a web service consumer in the browser.
A Midlet with a web service consumer was the next item to show, and it worked as a charm in the emulator!
After my 15 minutes of fame (or rather 40), we had a short drinking and chatting pause.

Joachim Dagerot introduced us into ext.js, a client-side framework with lot’s of goodies like menus, trees, tabbed tables, layouts etc. The framework looked really impressive, and applications based on it was more of a Windows experience. When developing applications with ext.js, almost everything is done in JavaScript, including the templates of the pages. You don’t have to download the complete framework, you can take bits and pieces, if you want only a part of the gadgets.

After more beverages, snacks and more chatting, connections were made, business cards and email addresses exchanged hands. I left rather early though, I hope my fellow bloggers to give the full story 😉 Photos were taking during the meeting, and I would really love to see them, since I didn’t use my own camera.

I hope that this event wasn’t the last of it’s kind in Sweden, it was great fun to see fellow bloggers and geeks!
Thanks for a great event everyone, especially Ekakan who arranged the g33k date!!

Btw, the picture at the top is NOT from the g33k date, it was just a picture I used in the presentation as an illustration of the thin red line between being a geek and a nerd. 😀

[tags]g33k, geek, J2ME, AJAX, Webservice, Ekakan[/tags]

]]>
http://johankanngard.net/2007/05/25/g33k-date/feed/ 7
Echo XML from a file, String or InputStream in Java with SAX http://johankanngard.net/2006/02/13/echo-xml-from-a-file-string-or-inputstream-in-java-with-sax/ Mon, 13 Feb 2006 13:51:06 +0000 http://johankanngard.net/2006/02/13/echo-xml-from-a-file-string-or-inputstream-in-java-with-sax/ […]]]> Here is an example how to use the XMLEchoer class, that I have refurbished from Sun’s tutorial Echoing an XML File with the SAX Parser. It is of limited use, but can be a good start learning SAX.

import java.io.PrintWriter;
import lotus.domino.AgentBase;
import net.kanngard.xml.XMLEchoer;

public class JavaAgent extends AgentBase {

public void NotesMain() {

try {
String xml = "Hello World";
PrintWriter writer = getAgentOutput();
writer.println("Content-type: text/xml");
XMLEchoer echoer = new XMLEchoer(writer);
echoer.start(xml);
} catch(Exception e) {
e.printStackTrace();
}
}
}

To get it working, you need a SAX implementation, like Apache Xerces 2. Just drop the JAR files from Xerces 2 into the jvm/lib/ext directory in your Notes or Domino installation. Restart the http task if you are testing with Domino.

[tags]Java, XML, SAX[/tags]

]]>
XML Parser Performance in LotusScript http://johankanngard.net/2006/01/17/xml-parser-performance-in-lotusscript/ http://johankanngard.net/2006/01/17/xml-parser-performance-in-lotusscript/#comments Tue, 17 Jan 2006 13:58:56 +0000 http://johankanngard.net/?p=55 […]]]> I am not Microsoft’s biggest fan, since I was a Macintosh fanatic in the old days, but had to convert into the Wintel platform for about 10 years ago. :-) But I have to admit though, when comparing Microsoft’s XML parser with the ones that are built into Notes/Domino, that their products sometimes are great!
When comparing Microsoft’s XML parser with the other two, I created an agent that parsed a big (over 700 KB) and complex XML document and read an element’s value. I did this 25 times per parser, and came up with this interesting result (average execution time per parser) when executed in the Notes client:

  1. Microsoft XML Parser: 0.2 seconds
  2. NotesSAXParser: 2.2 seconds
  3. NotesDOMParser: 2.5 seconds

When executed on a Domino server, the result was a bit different, since the server is a bit old:

  1. Microsoft XML Parser: 1.0 seconds
  2. NotesSAXParser: 5.0 seconds
  3. NotesDOMParser: 7.7 seconds
]]>
http://johankanngard.net/2006/01/17/xml-parser-performance-in-lotusscript/feed/ 5
Manually serializing NotesDOMNodes http://johankanngard.net/2006/01/11/manually-serialize-notesdomnodes/ http://johankanngard.net/2006/01/11/manually-serialize-notesdomnodes/#comments Wed, 11 Jan 2006 17:41:30 +0000 http://johankanngard.net/?p=52 […]]]> In my earlier post today, I described a problem I have, where I wanted to get the XML string of a DOM tree. I’ve started writing my own routine, this is what I have right now. It’s far from complete, since it can not handle namespaces, CDATA etc. yet:
This is a new version that handles siblings better, and not getting out of stack space :-)


Public Sub serializeNode(node As NotesDOMNode_
, outStream As NotesStream)
On Error Goto catch

If node.IsNull Then Exit Sub

Dim currentNode As NotesDOMNode
Set currentNode = node

Do Until currentNode.isNull
Select Case currentNode.nodeType
Case DOMNODETYPE_DOCUMENT_NODE
Case DOMNODETYPE_XMLDECL_NODE
Dim xmlDecl As NotesDOMXMLDeclNode
Set xmlDecl = currentNode
Call outStream.WriteText("< ?xml version=""" _ + xmlDecl.version + """ encoding=""" + xmldecl.encoding + """?>")
Case DOMNODETYPE_ELEMENT_NODE
Call outStream.writeText("< " + currentNode.nodeName + "") Dim nodeMap As NotesDOMNamedNodeMap Set nodeMap = currentNode.attributes Dim attribute As NotesDOMNode Dim i As Integer For i = 1 To nodeMap.numberOfEntries Set attribute = nodeMap.getItem(i) Call outStream.WriteText(" " + attribute.nodeName + "=") Call outStream.WriteText("""" + attribute.NodeValue + """") Next i Call outStream.writeText(">")
Case DOMNODETYPE_TEXT_NODE
Call outStream.writeText(currentNode.NodeValue)
Case DOMNODETYPE_COMMENT_NODE
Call outStream.writeText("")
Case Else
Error 2000, "Unhandled node type [" _
& currentNode.nodeType & "]"
End Select

Dim endWritten As Boolean

If currentNode.hasChildNodes Then
Call serializenode(currentNode.firstChild, outStream)
End If

If currentNode.NodeType = DOMNODETYPE_ELEMENT_NODE Then
Call outStream.writeText("")
endWritten = True
End If

Set currentNode = currentNode.nextSibling
Loop

If node.NodeType = DOMNODETYPE_ELEMENT_NODE _
And Not endWritten Then
Call outStream.writeText("")
End If
Exit Sub
catch:
Error Err, Err & Lsi_Info(2) & ":" & Erl & "|" & Error
End Sub

]]>
http://johankanngard.net/2006/01/11/manually-serialize-notesdomnodes/feed/ 2
Serialize a DOM tree in LotusScript? http://johankanngard.net/2006/01/11/serialize-a-dom-tree-in-lotusscript/ http://johankanngard.net/2006/01/11/serialize-a-dom-tree-in-lotusscript/#comments Wed, 11 Jan 2006 16:28:52 +0000 http://johankanngard.net/?p=51 […]]]> I have a problem, that nor I nor any of my colleagues can’t solve easily. I have several classes that are passing objects/variables between them. On of them is handling Web Service request, and parsing the responses to a NotesDOMDocumentNode via the NotesDOMParser. The NotesDOMDocumentNode is passed returned, and are used in several places to extract information. But, I need to cache the response, so concurrent request with the same parameters are sped up.
To cache the response, I need to serialize the NotesDOMDocumentNode. At first, I thought this was easy, since there is a Serialize method on the NotesDOMParser. But no… You have to keep the original NotesDOMParser that parsed the raw XML data. The only solutions I can figure out are:

  • pass both the NotesDOMDocumentNode AND the original NotesDOMParser at all times
  • write my own Serialize method
  • pass the XML string only, and do the parsing at each method. I think this would be really slow…

To further illustrate what I want, here is an example:

Public Sub testDOMParser()
Dim parser As NotesDOMParser
Dim inStream As NotesStream
Dim node As NotesDOMDocumentNode
Set inStream = session.createStream()
Call inStream.open("c:temptest.xml")
Set parser = session.createDOMParser(inStream)
Call parser.process()
Set node = parser.document
Call serializeDocument(node)
End Sub
Public Sub serializeDocument(node As NotesDOMDocumentNode)
' Here, I want to do something like this:
Dim outStream As NotesStream
Dim parser As NotesDOMParser
Set outStream = session.createStream()
Set parser = session.createDOMParser(node, outStream)
parser.exitOnFirstFatalError = True
Call parser.serialize()
Print "Serialized tree: " + outStream.readText()
End Sub

Anyone who have had any experience with this and are willing to share their thoughts? We are talking R6 here…

]]>
http://johankanngard.net/2006/01/11/serialize-a-dom-tree-in-lotusscript/feed/ 5
Getting the root element node in XML using LotusScript http://johankanngard.net/2005/12/16/getting-the-root-element-node-in-xml-using-lotusscript/ http://johankanngard.net/2005/12/16/getting-the-root-element-node-in-xml-using-lotusscript/#comments Fri, 16 Dec 2005 10:32:56 +0000 http://johankanngard.net/?p=39 […]]]> So I don’t forget the next time I want to do this, and trust me, I got a short memory!

Dim xml As String
Dim rootElementNode As NotesDOMElementNode
xml = "YOUR XML HERE"
Set rootElementNode = parseXML(xml)._
getElementsByTagName("THE ROOTNODE").getItem(1)
Public Function parseXML(xml As String) _
As NotesDOMDocumentNode
' Parses the specified xml.
On Error Goto catch
Dim session As New NotesSession()
Dim stream As NotesStream
Set stream = session.createStream()
Call stream.writeText(xml)
Dim domparser As NotesDOMParser
Set domparser = session.createDOMParser(stream)
domParser.exitOnFirstFatalError = True
Call domParser.parse()
Set parseXML = domparser.Document
Exit Function
catch:
Error Err, _
Err & "|" & Error & "|" & Lsi_info(2) & ":" & Erl
End Function

]]>
http://johankanngard.net/2005/12/16/getting-the-root-element-node-in-xml-using-lotusscript/feed/ 3