Math2mat
|
00001 00019 package m2m.backend.utils; 00020 00021 import org.w3c.dom.Document; 00022 import org.w3c.dom.Element; 00023 import org.w3c.dom.NodeList; 00024 import org.w3c.dom.Text; 00025 00030 public class XMLUtils { 00037 static public String getTextValue(Element ele, String tagName,String defaultValue) { 00038 String textVal = defaultValue; 00039 NodeList nl = ele.getElementsByTagName(tagName); 00040 if(nl != null && nl.getLength() > 0) { 00041 Element el = (Element)nl.item(0); 00042 if (el!=null) 00043 if (el.getFirstChild()!=null) 00044 textVal = el.getFirstChild().getNodeValue(); 00045 } 00046 00047 return textVal; 00048 } 00049 00050 00051 static public boolean getBoolValue(Element ele, String tagName,boolean defaultValue) { 00052 String textVal = ""; 00053 NodeList nl = ele.getElementsByTagName(tagName); 00054 if(nl != null && nl.getLength() > 0) { 00055 Element el = (Element)nl.item(0); 00056 if (el!=null) 00057 if (el.getFirstChild()!=null) { 00058 textVal = el.getFirstChild().getNodeValue(); 00059 00060 if (textVal.equalsIgnoreCase("true")) 00061 return true; 00062 else 00063 return false; 00064 } 00065 } 00066 return defaultValue; 00067 } 00068 00069 00073 static public int getIntValue(Element ele, String tagName,int defaultValue) { 00074 //in production application you would catch the exception 00075 try { 00076 return Integer.parseInt(getTextValue(ele,tagName,"")); 00077 } 00078 catch (NumberFormatException e) { 00079 return defaultValue; 00080 } 00081 } 00082 00086 static public float getFloatValue(Element ele, String tagName,float defaultValue) { 00087 //in production application you would catch the exception 00088 try { 00089 return Float.parseFloat(getTextValue(ele,tagName,"")); 00090 } 00091 catch (NumberFormatException e) { 00092 return defaultValue; 00093 } 00094 } 00095 00096 00100 static public double getDoubleValue(Element ele, String tagName,double defaultValue) { 00101 //in production application you would catch the exception 00102 try { 00103 return Double.parseDouble(getTextValue(ele,tagName,"")); 00104 } 00105 catch (NumberFormatException e) { 00106 return defaultValue; 00107 } 00108 } 00109 00110 static public Element createTextElement(Document dom,String tagName,String value) { 00111 Text text; 00112 if (tagName==null) 00113 System.out.println("Error in XML creation. Empty tagname\n"); 00114 else if (tagName.isEmpty()) 00115 System.out.println("Error in XML creation. Empty tagname\n"); 00116 Element El = dom.createElement(tagName); 00117 if (value==null) 00118 text=dom.createTextNode(""); 00119 else 00120 text=dom.createTextNode(value); 00121 El.appendChild(text); 00122 return El; 00123 } 00124 00125 static public Element createIntElement(Document dom,String tagName,int value) { 00126 if (tagName==null) 00127 System.out.println("Error in XML creation. Empty tagname\n"); 00128 if (tagName.isEmpty()) 00129 System.out.println("Error in XML creation. Empty tagname\n"); 00130 Element El = dom.createElement(tagName); 00131 String s=""; 00132 s += value; 00133 Text text=dom.createTextNode(s); 00134 El.appendChild(text); 00135 return El; 00136 } 00137 00138 static public Element createFloatElement(Document dom,String tagName,float value) { 00139 if (tagName==null) 00140 System.out.println("Error in XML creation. Empty tagname\n"); 00141 if (tagName.isEmpty()) 00142 System.out.println("Error in XML creation. Empty tagname\n"); 00143 Element El = dom.createElement(tagName); 00144 String s=""; 00145 s += value; 00146 Text text=dom.createTextNode(s); 00147 El.appendChild(text); 00148 return El; 00149 } 00150 00151 static public Element createDoubleElement(Document dom,String tagName,double value) { 00152 if (tagName==null) 00153 System.out.println("Error in XML creation. Empty tagname\n"); 00154 if (tagName.isEmpty()) 00155 System.out.println("Error in XML creation. Empty tagname\n"); 00156 Element El = dom.createElement(tagName); 00157 String s=""; 00158 s += value; 00159 Text text=dom.createTextNode(s); 00160 El.appendChild(text); 00161 return El; 00162 } 00163 00164 00165 static public Element createBoolElement(Document dom,String tagName,boolean value) { 00166 if (tagName==null) 00167 System.out.println("Error in XML creation. Empty tagname\n"); 00168 if (tagName.isEmpty()) 00169 System.out.println("Error in XML creation. Empty tagname\n"); 00170 Element El = dom.createElement(tagName); 00171 00172 String sValue; 00173 if (value) 00174 sValue="true"; 00175 else 00176 sValue="false"; 00177 Text text=dom.createTextNode(sValue); 00178 El.appendChild(text); 00179 return El; 00180 } 00181 }