XML Parser

Započeo maxogm, 26.12.2010, 23:30

prethodna tema - sledeća tema

maxogm

Primer kako napraviti XML parser

config.xml

<?xml version="1.0" encoding="UTF-8"?>
<ROOT>
<DATABASE>
<DATABASE_URL>jdbc:mysql://localhost:3306/ime_neke_baze</DATABASE_URL>
<DATABASE_DRIVERCLASS>com.mysql.jdbc.Driver</DATABASE_DRIVERCLASS>
<DATABASE_USERNAME>username</DATABASE_USERNAME>
<DATABASE_PASSWORD>password</DATABASE_PASSWORD>
</DATABASE>
</ROOT>


DOM Pareser:


import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class ReadConfiguration {

private Document dom;

public ReadConfiguration() {
parseXmlFile();
}

private void parseXmlFile() {
// get the factory
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
try {
// Using factory get an instance of document builder
DocumentBuilder db = dbf.newDocumentBuilder();
// parse using builder to get DOM representation of the XML file
dom = db.parse("config.xml");
} catch (ParserConfigurationException pce) {
pce.printStackTrace();
} catch (SAXException se) {
se.printStackTrace();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}

public String getValue(String element, String tag) {
String retVal = "ERROR";
Element docEle = dom.getDocumentElement();
NodeList nl = docEle.getElementsByTagName(element);
if (nl != null && nl.getLength() > 0) {
// get the employee element
Element el = (Element) nl.item(0);
// get the Employee object
retVal = getTextValue(el, tag);
}
return retVal;
}

private String getTextValue(Element ele, String tagName) {
String textVal = null;
NodeList nl = ele.getElementsByTagName(tagName);
if (nl != null && nl.getLength() > 0) {
Element el = (Element) nl.item(0);
textVal = el.getFirstChild().getNodeValue();
}
return textVal;
}
}


Primer test programa:



public class Main {

public static void main(String[] args) {

ReadConfiguration configuration = new ReadConfiguration();

String databaseUrl = configuration.getValue("DATABASE", "DATABASE_URL");
String databaseDriver = configuration.getValue("DATABASE", "DATABASE_DRIVERCLASS");
String databaseUsername = configuration.getValue("DATABASE", "DATABASE_USERNAME");
String databasePassword = configuration.getValue("DATABASE", "DATABASE_PASSWORD");

System.out.println(databaseUrl);
System.out.println(databaseDriver);
System.out.println(databaseUsername);
System.out.println(databasePassword);
}
}

maxogm

Pored DOM Parser-a, na veoma jednostavan nacin XML se moze citati i koriscenjem:

Apache Commons Configuration

http://commons.apache.org/configuration/

Primer klase za citanje XML iz prethodnog posta


public class ReadConfigurationCommonsConnfiguraton {

private XMLConfiguration config;

public ReadConfigurationCommonsConnfiguraton() {
parseXmlFile();
}

private void parseXmlFile() {
try {
config = new XMLConfiguration("config.xml");
} catch (Exception e) {
e.printStackTrace();
}
}

/**
* Description: get string value from xml
*
* @param element
* @param tagName
* @return String
*/
public String getStringValue(String ele, String tagName) {
String textVal = null;
textVal = config.getString(ele + "." + tagName);
return textVal;
}

/**
* Description: get integer value from xml
*
* @param element
* @param tagName
* @return
*/
public Integer getIntegerValue(String ele, String tagName) {
Integer retVal = null;
retVal = config.getInt(ele + "." + tagName);
return retVal;
}

/**
* Description: get boolean value from xml
*
* @param element
* @param tagName
* @return
*/
public Boolean getBooleanValue(String ele, String tagName) {
Boolean retVal = null;
retVal = config.getBoolean(ele + "." + tagName);
return retVal;
}
}


Primer test programa


public class Main {

   public static void main(String[] args) {
     
      ReadConfigurationCommonsConnfiguraton configuration = new ReadConfigurationCommonsConnfiguraton();
     
      String databaseUrl = configuration.getStringValue("DATABASE", "DATABASE_URL");
      String databaseDriver = configuration.getStringValue("DATABASE", "DATABASE_DRIVERCLASS");
      String databaseUsername = configuration.getStringValue("DATABASE", "DATABASE_USERNAME");
      String databasePassword = configuration.getStringValue("DATABASE", "DATABASE_PASSWORD");
     
      System.out.println(databaseUrl);
      System.out.println(databaseDriver);
      System.out.println(databaseUsername);
      System.out.println(databasePassword);
   }
}


Za Apache Commons Configuration potrebno je preuzeti par jar-ova sa gore navedenog linka, ili na jednostavniji nacin tako sto ce te u pom.xml u vasem Maven projektu dodati:


  <dependency>
  <groupId>commons-configuration</groupId>
  <artifactId>commons-configuration</artifactId>
  <version>1.6</version>
  </dependency>