Parsing XML to Map

package com.open.jacekkowalczyk;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.apache.log4j.Logger;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
/**
 * Xml2Map!
 */
public class Xml2Map {
    private static Logger LOGGER = Logger.getLogger(Xml2Map.class);
    public List<Map<String, String>> getListOfLevel1MapElelements(final File file, final String rootElemName,
        final String firstLevelElemName) {
        final List<Map<String, String>> elements = new ArrayList<Map<String, String>>();
        try {
            final DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db;
            db = dbf.newDocumentBuilder();
            final Document doc = db.parse(file);
            doc.getDocumentElement().normalize();
            // System.out.println(“Root element ” + doc.getDocumentElement().getNodeName());
            final NodeList rootNodeList = doc.getElementsByTagName(rootElemName);
            LOGGER.debug(“all elements”);
            for (int i = 0; i < rootNodeList.getLength(); i++) {
                final Node rootXmlNode = rootNodeList.item(i);
                LOGGER.debug(“root: ” + rootXmlNode.getNodeName());
                if (rootXmlNode.getNodeType() == Node.ELEMENT_NODE) {
                    final Element rootXmlElem = (Element)rootXmlNode;
                    final NodeList level1Nodes = rootXmlElem.getElementsByTagName(firstLevelElemName);
                    for (int j = 0; j < level1Nodes.getLength(); j++) {
                        final Node level1Node = level1Nodes.item(j);
                        LOGGER.debug(“1stlevel:    ” + level1Node.getNodeName());
                        if (level1Node.getNodeType() == Node.ELEMENT_NODE) {
                            final Map<String, String> level1XmlElementsMap = new HashMap<String, String>();
                            final Element level1NodeElem = (Element)level1Node;
                            final NodeList level2Nodes = level1NodeElem.getChildNodes();
                            for (int k = 0; k < level2Nodes.getLength(); k++) {
                                final Node level2Node = level2Nodes.item(k);
                                // System.out.println(“2ndlevelNode:    ” + level2Node.getNodeName());
                                if (level2Node.getNodeType() == Node.ELEMENT_NODE) {
                                    final Element level2NodeElem = (Element)level2Node;
                                    LOGGER.debug(“2ndlevelElem:    ” + level2NodeElem.getNodeName() + “: “
                                        + level2NodeElem.getTextContent());
                                    level1XmlElementsMap.put(level2NodeElem.getNodeName(), level2NodeElem.getTextContent());
                                }
                            }
                            elements.add(level1XmlElementsMap);
                        }
                    }
                }
            }
            return elements;
        } catch (final ParserConfigurationException e) {
            e.printStackTrace();
            return elements;
        } catch (final SAXException e) {
            e.printStackTrace();
            return elements;
        } catch (final IOException e) {
            e.printStackTrace();
            return elements;
        }
    }
}

creating issues in bugzilla from java

final HttpClient httpClient = new HttpClient();
final XmlRpcClient rpcClient = new XmlRpcClient();
final XmlRpcCommonsTransportFactory factory = new XmlRpcCommonsTransportFactory(rpcClient);
final XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl();

factory.setHttpClient(httpClient);
rpcClient.setTransportFactory(factory);
config.setServerURL(new URL(“http://192.168.12.13/xmlrpc.cgi&#8221;));
rpcClient.setConfig(config);

// map of the login data
final Map<String, String> loginMap = new HashMap<String, String>();

loginMap.put(“login”, userName);

loginMap.put(“password”, password);
loginMap.put(“rememberlogin”, “Bugzilla_remember”);

// login to bugzilla
final Object loginResult = rpcClient.execute(“User.login”, new Object[] {loginMap});
System.out.println(“loginResult=” + loginResult);

final Map issueParamsMap = new HashMap<String, String>();
issueParamsMap.put(“product”, “TestProduct”);
issueParamsMap.put(“component”, “testComponent”);
issueParamsMap.put(“summary”, “Bug created from java xmlrpc”);
issueParamsMap.put(“description”, “This is text including stacktrace”);
issueParamsMap.put(“version”, “unspecified”);
issueParamsMap.put(“op_sys”, “Linux”);
issueParamsMap.put(“platform”, “PC”);
issueParamsMap.put(“priority”, “Highest”);
issueParamsMap.put(“severity”, “Normal”);
issueParamsMap.put(“status”, “NEW”);
issueParamsMap.put(“estimated_time”, “2”);

final Object createResult = rpcClient.execute(“Bug.create”, new Object[] {issueParamsMap});

System.out.println(“createResult = ” + createResult);

pom dependencies and plugins:

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>commons-httpclient</groupId>
<artifactId>commons-httpclient</artifactId>
<version>3.1</version>
</dependency>
<dependency>
<groupId>org.apache.xmlrpc</groupId>
<artifactId>xmlrpc-client</artifactId>
<version>3.1.3</version>
</dependency>
<dependency>
<!– com/open/jacekkowalczyk/xml-to-map/1.0-SNAPSHOT –>
<groupId>com.open.jacekkowalczyk</groupId>
<artifactId>xml-to-map</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>commons-cli</groupId>
<artifactId>commons-cli</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.16</version>
</dependency>

</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.0.2</version>
<configuration>
<encoding>UTF-8</encoding>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.2-beta-5</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>com.tomtom.cpu.coredb.xmlrpc.bugzilla.BugzillaIssuesCreator</mainClass>
</manifest>
<manifestEntries>
<buildVersion>${project.version}</buildVersion>
<SCM-Revision>${buildNumber}</SCM-Revision>
<SCM-Timestamp>${timestamp}</SCM-Timestamp>
</manifestEntries>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>

</plugins>
</build>

 

Running liquibase from command line

Create liquibase.properties file next to the changeLogFile (which by default is named install.xml)

promptOnNonLocalDatabase false
url jdbc:postgresql://hostname/db_name
username my_user
password my_pass
driver org.postgresql.Driver
defaultSchemaName localizer

Execute commands:

$ cd my_project/src/main/resources

$ ~/liquibase-3.0.0-bin/liquibase –driver=org.postgresql.Driver –classpath=/path/to/postgresql-9.0-801.jdbc4.jar –changeLogFile=install.xml –defaultsFile=liquibase.properties –logLevel=debug –logFile=/tmp/localizer-liquibase.log update -DschemaName=my_schema

Liquibase Home is not set.
Liquibase Home: /my/userhomedir/liquibase-3.0.0-bin
Liquibase update Failed: Validation Failed:
8 change sets check sum
….

….

For more information, use the –logLevel flag