Want more features on Pastebin? Sign Up, it's FREE!
Guest

How do I represent an XML file as a Java object using commons-digester

By: a guest on Feb 27th, 2012  |  syntax: None  |  size: 2.83 KB  |  views: 21  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. <MyConfig>
  2.     <ServerName>nile</ServerName>
  3.     <ServerPort>8079</ServerPort>
  4. </MyConfig>
  5.        
  6. package com.digestersample;
  7.  
  8. import java.io.File;
  9. import java.io.IOException;
  10.  
  11. import org.apache.commons.digester.Digester;
  12. import org.xml.sax.SAXException;
  13.  
  14. public class MyConfig {
  15.     private String serverName;
  16.     private String serverPort;
  17.  
  18.     public MyConfig(String configFile) throws IOException, SAXException
  19.     {
  20.         Digester digester = new Digester();
  21.         digester.setValidating(false);
  22.  
  23.         digester.addObjectCreate("MyConfig", MyConfig.class);
  24.  
  25.         digester.addCallMethod("MyConfig/ServerName", "setServerName", 0);
  26.         digester.addCallMethod("MyConfig/ServerPort", "setServerPort", 0);
  27.  
  28.         System.out.println("Creating MyConfig...");
  29.         MyConfig mc = (MyConfig) digester.parse(new File(configFile));
  30.         System.out.println("Done.");
  31.  
  32.         System.out.println("Port: " + mc.getServerName());
  33.         System.out.println("Port: " + mc.getServerPort());
  34.     }
  35.  
  36.     public String getServerName() {
  37.         return serverName;
  38.     }
  39.  
  40.     public void setServerName(String serverName) {
  41.         this.serverName = serverName;
  42.     }
  43.  
  44.     public String getServerPort() {
  45.         return serverPort;
  46.     }
  47.  
  48.     public void setServerPort(String serverPort) {
  49.         this.serverPort = serverPort;
  50.     }
  51.  
  52. }
  53.        
  54. package com.digestersample;
  55.  
  56. import java.io.IOException;
  57.  
  58. import org.xml.sax.SAXException;
  59.  
  60. public class MyOtherClass {
  61.  
  62.     public static void main(String[] args) {
  63.  
  64.         MyConfig mc;
  65.         try {
  66.  
  67.                 mc = new MyConfig("/home/user/MyConfig.xml");
  68.  
  69.                 System.out.println( mc.getServerName() );
  70.                 System.out.println( mc.getServerPort() );
  71.  
  72.         } catch (IOException e) {
  73.                 e.printStackTrace();
  74.         } catch (SAXException e) {
  75.                 e.printStackTrace();
  76.         }
  77.  
  78.     }
  79.  
  80. }
  81.        
  82. public class MyConfigFactory
  83.  
  84.     public MyConfig createMyConfig(String configFile) throws IOException, SAXException
  85.     {
  86.         Digester digester = new Digester();
  87.         digester.setValidating(false);
  88.  
  89.         digester.addObjectCreate("MyConfig", MyConfig.class);
  90.  
  91.         digester.addCallMethod("MyConfig/ServerName", "setServerName", 0);
  92.         digester.addCallMethod("MyConfig/ServerPort", "setServerPort", 0);
  93.  
  94.         MyConfig mc = (MyConfig) digester.parse(new File(configFile));
  95.  
  96.         return mc;
  97.     }
  98.  
  99. }
  100.        
  101. @Root
  102. public class MyConfig {
  103.  
  104.   @Element
  105.   private String serverName;
  106.  
  107.   @Element
  108.   private int serverPort
  109.  
  110.   public MyConfig() {
  111.      super();
  112.   }
  113.  
  114.   public MyConfig(File file) {
  115.      Style style = new CamelCaseStyle();
  116.      Persister persister = new Persister(style);
  117.  
  118.      persister.read(this, file);
  119.   }
  120.  
  121.   public String getServerName() {
  122.     return name;
  123.   }
  124.  
  125.   public int getServerPort() {
  126.     return port;
  127.   }
  128. }
  129.        
  130. MyConfig mc = new MyConfig("/home/user/MyConfig.xml");
  131.  
  132. System.out.println("Name: " + mc.getServerName());
  133. System.out.println("Port: " + mc.getServerPort());
clone this paste RAW Paste Data