VBScript XML processing to extract JMX content
By jeanfrancoisdenise on Jul 11, 2008
In order to deal with XML namespaces, I wrote a really horrible function based on the knowledge of the namespace prefix name computation. Something that I presented as a very bad way, caused by my lack of expertise in VBScript.
Since this previous post, I have written a bunch of scripts and dived into some VBScript librairies details. I have found the right way to process XML and deal with Namespaces. This is done thanks to XPATH usage. By setting a set of properties on the object returned by
CreateObject("Microsoft.XMLDOM"), you can inject prefix/namespace associations. These associations are then usable when selecting XML nodes.
Retrieving the WS-Enumeration enumeration context of an MBean subscriptionWS-Man session creation and subscription invocation is detailed in the previous post. The enumeration context must be extracted as follow:
set reply = ... Dim objXMLDoc set objXMLDoc = CreateObject("Microsoft.XMLDOM") ' Make the reply to be loaded synchronously objXMLDoc.async = False ' Load the reply objXMLDoc.loadXML(reply) ' Make XPath the selection dialect objXMLDoc.setProperty "SelectionLanguage", "XPath" ' Associate "wsen" prefix to WS-Enumeration namespace. objXMLDoc.setProperty "SelectionNamespaces", "xmlns:wsen='http://schemas.xmlsoap.org/ws/2004/09/enumeration'" ' There is a single enumeration node, selectSingleNode returns the first one. Dim node set node = objXMLDoc.selectSingleNode("//wsen:EnumerationContext") ' node.text contains the enumeration context value dim enumContext set enumContext = node.text
Retrieving the MBean Notifications pulled from the serverNotification pulling is detailed in the previous post. Extracting all the Messages contained in the list of notifications is done as follow:
set reply = ... dim replyXml set replyXml= CreateObject("Microsoft.XMLDOM") replyXml.async = False replyXml.loadXML(reply) ' Make XPath the selection dialect replyXml.setProperty "SelectionLanguage", "XPath" ' Associate "jmx" prefix to JMX Web Services Connector namespace. replyXml.setProperty "SelectionNamespaces", "xmlns:jmx='http://jsr262.dev.java.net/jmxconnector'" ' Select all the Notification Message nodes text values set msgList = replyXml.selectNodes("//jmx:TargetedNotification/jmx:Message/text()") ' Display the Messages for i = 0 To (msgList.length - 1) set msg = msgList(i) ' Display the notification message WScript.echo "Notification : " & msg next
ConclusionVBScript API to extract XML content is efficient and simple to use. It is very helpful when extracting values from complex data structures (such as CompositeData or MBeanInfo). Obviously its usage is much wider than JMX interoperability.
Hope this help.