Parsing XML and Converting to JSON using Golang

Introduction:

Recently I started to learn Golang for solving an interesting engineering problem and finally solved it successfully. Hence thought I can share the problem and solution with others who may also be interested to know. If this help’s someone that would be great.

Problem

Here is the sample XML which we are interested to decode using Golang,

<?xml version="1.0" encoding="utf-8"?>
<testreport id="Product Monitoring" name="Product Monitoring">
    <testclass id="Product Installation State" name="PRODUCT_INST_STATE">
        <testcase id="Product Installation State" name="PRODUCT_INST_STATE" tracker="INT">
            <result>Pass</result>
            <message>Script completed with no issues</message>
            <durationInSecs>32</durationInSecs>
        </testcase>
    </testclass>
    <testclass id="Product Windows Service State" name="PRODUCT_WIN_STATE">
        <testcase id="Product Windows Service State" name="PRODUCT_WIN_STATE" tracker="INT">
            <result>Pass</result>
            <message>Service running with no issues</message>
            <durationInSecs>80</durationInSecs>
        </testcase>
    </testclass>
    <testclass id="Product UI State" name="PRODUCT_UI_STATE">
        <testcase id="Product UI State" name="PRODUCT_UI_STATE" tracker="INT">
            <result>Pass</result>
            <message>Product UI state ON with no issues</message>
            <durationInSecs>7</durationInSecs>
        </testcase>
    </testclass>
</testreport>

How to solve it

The first and foremost we had to create struct’s to Unmarshal the xml to object. Here we have testreport as a root xml node along with testclass and testcase as sub node’s inside it. Here is how we can define structs for each node in child-to-parent hierarchy.

type testcase struct {
		Id             string `xml:"id,attr"`
		Name           string `xml:"name,attr"`
		Tracker        string `xml:"tracker,attr"`
		Result         string `xml:"result"`
		Message        string `xml:"message"`
		DurationInSecs string `xml:"durationInSecs"`
	}
	type testclass struct {
		Id       string   `xml:"id,attr"`
		Name     string   `xml:"name,attr"`
		TestCase testcase `xml:"testcase"`
	}
type testreport struct {
		Id        string      `xml:"id,attr"`
		Name      string      `xml:"name,attr"`
		TestClass []testclass `xml:"testclass"`
	}

Opening the xml in golang, xml file or any file for that matter can be opened using os.Open API available in “OS” package. Quick example on opening the xml file.

package main
import (
	"fmt"
	"io/ioutil"
	"os"
)
func main() {
	xmlFileName := "session.xml"
	xmlFile, err := os.Open(xmlFileName)
	if err != nil {
		fmt.Println(err)
	}
	fmt.Println("Successfully Opened file : ", xmlFileName)
	byteData, err := ioutil.ReadAll(xmlFile)
	if err != nil {
		fmt.Println(err)
	}
	fmt.Printf("Overall file content :\n %s \n", byteData)
}

And the final part is Unmarshal the xml using the golang xml library and converting it to JSON. Golang provides xml support via package “encoding/xml” and JSON support via “encoding/json”.

Here is the final code putting together.

package main

import (
	"encoding/json"
	"encoding/xml"
	"fmt"
	"io/ioutil"
	"os"
)

func main() {
	type testcase struct {
		Id             string `xml:"id,attr"`
		Name           string `xml:"name,attr"`
		Tracker        string `xml:"tracker,attr"`
		Result         string `xml:"result"`
		Message        string `xml:"message"`
		DurationInSecs string `xml:"durationInSecs"`
	}
	type testclass struct {
		Id       string   `xml:"id,attr"`
		Name     string   `xml:"name,attr"`
		TestCase testcase `xml:"testcase"`
	}
	type testreport struct {
		Id        string      `xml:"id,attr"`
		Name      string      `xml:"name,attr"`
		TestClass []testclass `xml:"testclass"`
	}

	xmlFileName := "autosession.xml"
	xmlFile, err := os.Open(xmlFileName)
	if err != nil {
		fmt.Println(err)
	}
	fmt.Println("Successfully Opened file", xmlFileName)
	byteData, err := ioutil.ReadAll(xmlFile)
	if err != nil {
		fmt.Println(err)
	}
	v := testreport{}
	err = xml.Unmarshal(byteData, &v)
	if err != nil {
		fmt.Printf("error: %v", err)
		return
	}
	result, err := json.Marshal(v)
	if nil != err {
		fmt.Println("Error marshalling to JSON", err)
		return
	}
	fmt.Printf("%s\n", result)
}

Here is the final input and output generated using code,

{
    "Id": "Product Monitoring",
    "Name": "Product Monitoring",
    "TestClass": [
        {
            "Id": "Product Installation State",
            "Name": "PRODUCT_INST_STATE",
            "TestCase": {
                "Id": "Product Installation State",
                "Name": "PRODUCT_INST_STATE",
                "Tracker": "INT",
                "Result": "Pass",
                "Message": "Script completed with no issues",
                "DurationInSecs": "32"
            }
        },
        {
            "Id": "Product Windows Service State",
            "Name": "PRODUCT_WIN_STATE",
            "TestCase": {
                "Id": "Product Windows Service State",
                "Name": "PRODUCT_WIN_STATE",
                "Tracker": "INT",
                "Result": "Pass",
                "Message": "Service running with no issues",
                "DurationInSecs": "80"
            }
        },
        {
            "Id": "Product UI State",
            "Name": "PRODUCT_UI_STATE",
            "TestCase": {
                "Id": "Product UI State",
                "Name": "PRODUCT_UI_STATE",
                "Tracker": "INT",
                "Result": "Pass",
                "Message": "Product UI state ON with no issues",
                "DurationInSecs": "7"
            }
        }
    ]
}

Conclusion

As newbie to Golang am really surprised how fast we can prototype something using Golang in short span of time. The tools required are already available and can make our life so much easy for modern programming challenges.

Will see you again, thanks

Using Boost Library in Eclipse CDT with Cygwin and MinGW

Using Boost Library with Eclipse

Introduction

The purpose of this blog is to show how to use the boost library in Eclipse CDT plug-in with Cygwin and MinGW compiler.

About Boost Library:

Boost is an Internet community dedicated to building and reviewing C++ libraries. The initial members of Boost were C++ Standards Committee members who wanted to create a forum for developing libraries that might some day become part of the C++ Standard. Boost emphasize libraries that work well with the C++ Standard Library. Boost libraries are intended to be widely useful, and usable across a broad spectrum of applications. The Boost license encourages both commercial and non-commercial use. Boost works on almost any modern operating system, including UNIX and Windows variants. Follow the Getting Started Guide to download and install Boost. Popular Linux and Unix distributions such as Fedora, Debian, and NetBSD include pre-built Boost packages.

Eclipse CDT:

The CDT Project provides a fully functional C and C++ Integrated Development Environment based on the Eclipse platform. Features include: support for project creation and managed build for various toolchains, standard make build, source navigation, various source knowledge tools, such as type hierarchy, call graph, include browser, macro definition browser, code editor with syntax highlighting, folding and hyperlink navigation, source code refactoring and code generation, visual debugging tools, including memory, registers, and disassembly viewers.

( click here to download eclipse for C++)

Cygwin:

Cygwin is a Linux-like environment for Windows. It consists of two parts:

  • A DLL (cygwin1.dll) which acts as a Linux API emulation layer providing substantial Linux API functionality.
  • A collection of tools which provide Linux look and feel.

The Cygwin DLL currently works with all recent, commercially released x86 32 bit and 64 bit versions of Windows, with the exception of Windows CE.

MinGW:

  • MinGW, a contraction of “Minimalist GNU for Windows”, is a minimalist development environment for native Microsoft Windows applications.
  • MinGW provides a complete Open Source programming tool set which is suitable for the development of native MS-Windows applications, and which do not depend on any 3rd-party C-Runtime DLLs (only the Microsoft C runtime, MSVCRT).

Downloading and Installing Cyginwin:

Note: click here to download cygwin

Follow the following steps to install cygwin,

  1. Run the Cyginwin setup.
  2. Select install from Internet
  3. Set the root directory as C:\cygwin
  4. Set the local directory for the installation files
  5. Configure the internet settings, Direction connection or Proxy settings.
  6. Select the mirror site
  7. Select the core components or allow the default selection and give next.

Download and Install MinGW compiler:

Click here to download MinGW compiler

Download the MinGW compiler and accept the default settings and install the MinGW compiler

Make sure to select the GCC compler

Download boost Source and bjam tool:

Click here to download the boost source and bjam tool.

Installing Boost library:

  • Add the path of the cygwin in environment variable
  • Add the path of minGW compiler in environment variable
  • Extract the Eclipse in C:\Eclipse directory
  • Extract the boost package in C:\Program Files\boost\
  • Extract the bjam tool in C:\Program Files\boost\boostxxx.\
  • Go to the command prompt give the following command
C:\Program Files\boost\boost_1_44_0>bjam --toolset=gcc --build-type=complete

Note: build process will take lot of time to finish. Wait patiently.

Using Boost lib in Eclipse

  • Open Eclipse->File -> New -> C++ project
  • Empty project and accept other default options
  • Right click the project and add new Source file ( shown in the screen shot )

 

 

 

 

 

 

 

 

 

  • Select the project Properties
  • Select the path and Symbol’s under C/C++ General category.
  • Select GNU C and click add button

Now you can enter the boost path or explore the path using File System button. it show like as follows.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Repeat the same process for GNU C++

 

 

 

 

 

 

 

 

 

 

 

 

 

Adding the code:

Add the following code in main.cpp file

#include <iostream>
#include <boost/foreach.hpp>
using namespace std;

int main() {
 cout<<"Boost Demo"<<endl;
 int a[10];
 for(int i=0;i<10;i++)
 {
 a[i]=i+10;
 }
 BOOST_FOREACH(int k, a)
 {
 cout<<"\t"<<k;
 }
 return 0;
}

Build and Run the Project:

Click project-> Build Project

Click Run Command to run the project

See the output in Console tab

Getting IP Address of Machine in VC++

Getting IP Address of machine in VC++

Introduction:

Here we can see how to get the multiple ip address available in machine and display it in Multi line text box in MFC.

How To start:

here am used MFC dialog based application you can change it accordingly.

note: make sure to select Socket support in Application Wizard

How to make Multi line text Box in MFC:

To make a Editbox as multi line text box follow this procedure .

  • Select the edit box and select the properties
  • Select Multiline checked
  • Select want return checked

Selection will look like as follows

  • Create member variable as Value type using Class Wizard. to insert a new line in edit box we should use “\r\n“.
  • for example if the member variable is m_Text we can use as follows
m_Text="\r\n Hi \r\n This \r\n Multiline \r\n Sample";

Code To Get the IP Address:

The following code gets and displays the IP Address of the machine in the edit box.

void CDispalyIPAddressDlg::OnGetIP()
{
 UpdateData(TRUE);
 char HostName[128];
 //Get host name and Check is there was any error
 if (gethostname(HostName, sizeof(HostName)) == SOCKET_ERROR)
 {
 AfxMessageBox("Error in Getting Host Info");
 CDialog::OnCancel();
 }
 //Get the ip address of the machine using gethostbyname function
 struct hostent *IpList = gethostbyname(HostName);
 m_List+="Host Name is:";
 m_List+=HostName;
 m_List+="\r\n";
 if (IpList == 0)
 {
 AfxMessageBox("Yow! Bad host lookup.");
 CDialog::OnCancel();
 }
 m_List+="\r\n\r\nIP Address Available are:\r\n";
 m_List+="----------------------------------------------------------\r\n";
 //Enumerate list of Ip Address available in the machine
 for (int i = 0; IpList->h_addr_list[i] != 0; ++i)
 {
 struct in_addr addr;
 memcpy(&addr, IpList->h_addr_list[i], sizeof(struct in_addr));
 CString str=inet_ntoa(addr);
 m_List=m_List+ str;
 m_List+="\r\n" ;
 }
 m_List+="-----------------------------------------------------------\r\n";
 UpdateData(FALSE);
}

Final Output

Final looks like in the following screen shot.

Read more of this post

Shutdown System using Java Native Interface (JNI)

Shutdown System using Java Native Interface (JNI)

What is JNI?

The JavaTM Native Interface (JNI) is a powerful feature of the Java platform. Applications that use the JNI can incorporate native code written in programming languages such as C and C++, as well as code written in the Java programming language. The JNI allows programmers to take advantage of the power of the Java platform, without having to abandon their investments in legacy code. Because the JNI is a part of the Java platform, programmers can address interoperability issues once, and expect their solution to work with all implementations of the Java platform.

Who need’s JNI?

JNI is useful for the programmer who wants to perform a low level programming activity’s with System. For example performing Native IPC or Kernel/Shell related operations.

What i Should learn to be JNI programmer?

Knowledge in Core java and Hand on any low level( C or C++ ) programming language is enough.

Find best resources on C and C++ click here

What are the tools/ software required?

For compiling the Java program and generating JNI headers we need

For Compiling and generating Dll

Here i am going to demonstrate it with the help of two most popular C/ C++ compilers Microsoft Visual studio and MinWG( minimal GNU compiler for Windows)

How to create JNI program

  1. Create Java program which should export the Native methods
  2. Create .h file using javah tool ( ie. javah class-name )
  3. Create .c/cpp file to implement the methods.
  4. Compile and like as DLL ( Dynamic Link Library )
  5. Run and test the program.

Creating Java Program:

// JavaShutdown.java
class JavaShutdown
{
    private native void Shutdown();
    static
    {
          System.loadLibrary("ShutdownImpl");
    }
    public static void main(String args[])
    {
          new JavaShutdown().Shutdown();
    }
}

Compiling Java program:

Generating class file,

javac JavaShutdown.java

Generating Header file

javah JavaShutdown

To know more about javah click here

Generating DLL:

Here am going to create DLL in two ways one is using Microsoft Visual Studio and Using MinGW. First we can see the steps to create a DLL using MVS

Creating DLL using Microsoft Visual Studio ( VC++ Compiler )

Here am going to use Microsoft Visual C++ 6.0 which i acquainted very well,

The following show step by step process of creating DLL in VC++:
  • Create a new Project in VC++ File > New > Projects -> Select Win32 Dynamic Link Library > Enter the Project Name (ie. ShutdownImpl )

  • Select Empty Dll Project then Finish Button Give OK in Project Information Dialog
  • Add New Source File into the Project by clicking File> New > Source File > C / Cpp header files ( ie. ShutdownImpl.c ) Make sure to give

  • Implement the JNI functions in ShutdownImpl.c Copy and paste the following code into the ShutdownImpl.c file
#include<stdio.h>
#include<windows.h>
#include "JavaShutdown.h"
JNIEXPORT void JNICALL Java_JavaShutdown_Shutdown(JNIEnv *env, jobject obj)
{
 printf("Shuting Down....");
 ExitWindows(EWX_POWEROFF,NULL);
}

ExitWindows function is a native method used for Shutdown the system in windows Environment. To know more about ExitWindows Click here

You can also use EWX_FORCE along with EWX_POWEROFF to force power off (ie. ExitWindows( EWX_FORCE || EWX_POWEROFF, NULL);

Also see ExitWindowsEx

  • Set the Path of the JNI headers and Compiled JNI Header file by Tools > Options > Directory tab and make sure Include files selected

  1. Add the path of Java Include Library
  2. Add the path of Java WIN32 Header file
  3. Add the path contains your Java header file ( directory contains the JavaShutdown.h ” )
  • Compile and Build the DLL
  • Copy the Dll to the directory which contains the Java Source files and Run the java Application.

java JavaShutdown

Creating a DLL using MinGW( best option for me )

  • Create a new Text file in notepad or any editors. Copy and paste the following source code. Save As ShutdownImpl.c
#include<stdio.h>
#include<windows.h>
#include "JavaShutdown.h"
JNIEXPORT void JNICALL Java_JavaShutdown_Shutdown(JNIEnv *env, jobject obj)
{
 printf("Shuting Down....");
 ExitWindows(EWX_POWEROFF,NULL);
}
  • Compile the C program using the following command. ( note: class path for MinGW compiler is set properly)
    • Give it in single line
gcc -Wall -D_JNI_IMPLEMENTAION_ -Wl,--kill-at -I "C:\Program Files\Java\jdk1.6.0_13\include" -I "C:\Program Files\Java\jdk1.6.0_13\include\win32" -shared -o ShutdownImpl.dll ShutdownImpl.c
  • Now Run the java Application.

java JavaShutdown

Please post your comments

Thank you

elangovan