1 package com.xxx.xxx.xxx.excel; 2 3 import java.io.File; 4 import java.io.FileInputStream; 5 import java.io.FileOutputStream; 6 import java.io.IOException; 7 import java.io.InputStream; 8 import java.io.OutputStreamWriter; 9 import java.io.StringWriter; 10 import java.util.ArrayList; 11 import java.util.Iterator; 12 import java.util.List; 13 14 import org.dom4j.Attribute; 15 import org.dom4j.Document; 16 import org.dom4j.DocumentHelper; 17 import org.dom4j.Element; 18 import org.dom4j.io.OutputFormat; 19 import org.dom4j.io.SAXReader; 20 import org.dom4j.io.XMLWriter; 21 22 public class XMLHandler { 23 24 /** 25 * xml ???????????? 26 */ 27 private static final String ENCODING = "UTF-8"; 28 29 /** 30 * SAXReader 31 */ 32 private SAXReader saxReader; 33 34 /** 35 * Document 36 */ 37 private Document document; 38 39 /** 40 * ????????? 41 */ 42 private Element root; 43 44 /** 45 * ?????????null??????????????? 46 */ 47 private List<String> nullValues = new ArrayList<String>(); 48 49 50 51 52 /** 53 * ???????????? 54 * ????????????saxReader.read(File file); ?????? The encoding "GBK" is not supported 55 * @param inputXml xml?????? 56 */ 57 public XMLHandler(File inputXml) { 58 String xml = file2String(inputXml); 59 try { 60 if (inputXml != null) { 61 document = DocumentHelper.parseText(xml); 62 root = document.getRootElement(); 63 } 64 } catch (Exception e) { 65 throw new RuntimeException("can???t parse xml, please check the xml format", e); 66 } 67 } 68 69 /** 70 * ???????????? 71 * @param inputXml xml?????? 72 * @param encoding xml ?????? 73 */ 74 public XMLHandler(File inputXml, String encoding) { 75 saxReader = new SAXReader(); 76 try { 77 if (inputXml != null) { 78 saxReader.setEncoding(encoding); 79 document = saxReader.read(inputXml); 80 root = document.getRootElement(); 81 } 82 } catch (Exception e) { 83 throw new RuntimeException("can???t parse xml, please check the xml format", e); 84 } 85 86 } 87 88 /** 89 * ???????????? 90 * ????????????saxReader.read(String str);?????????no protocol:????????? 91 * ??????DocumentHelper.parseText(String str);?????????????????? 92 * @param inputXml xml????????? 93 */ 94 public XMLHandler(String inputXml) { 95 saxReader = new SAXReader(); 96 try { 97 if (inputXml != null) { 98 document = DocumentHelper.parseText(inputXml); 99 root = document.getRootElement(); 100 } 101 } catch (Exception e) { 102 throw new RuntimeException("can???t parse xml, please check the xml format", e); 103 } 104 } 105 106 /** 107 * ???xml???document???????????????xml????????? 108 * @return xml????????? 109 */ 110 public String doc2Xml() { 111 StringWriter writer = new StringWriter(); 112 try { 113 OutputFormat format = OutputFormat.createPrettyPrint(); 114 format.setEncoding(ENCODING); 115 XMLWriter output = new XMLWriter(writer, format); 116 output.write(document); 117 output.close(); 118 } catch (Exception e) { 119 e.printStackTrace(); 120 } 121 return writer.toString(); 122 } 123 124 /** 125 * ???xml???document???????????????xml????????? 126 * @param encoding ??????????????? 127 * @return xml????????? 128 */ 129 public String doc2Xml(String encoding) { 130 StringWriter writer = new StringWriter(); 131 try { 132 OutputFormat format = OutputFormat.createPrettyPrint(); 133 format.setEncoding(encoding); 134 XMLWriter output = new XMLWriter(writer, format); 135 output.write(document); 136 output.close(); 137 } catch (Exception e) { 138 e.printStackTrace(); 139 } 140 return writer.toString(); 141 } 142 143 /** 144 * ??????root?????? 145 * @return root?????? 146 */ 147 public Element getRootElement() { 148 return root; 149 } 150 151 /** 152 * ????????????xpath?????????Text??? 153 * "//article/date" 154 * @param xpath xpath?????? 155 * @return Text??? 156 */ 157 // @SuppressWarnings("unchecked") 158 public String getElementText(String xpath) { 159 List<?> list = document.selectNodes(xpath); 160 Iterator<?> iter = list.iterator(); 161 String value = null; 162 if (iter.hasNext()) { 163 Element element = (Element) iter.next(); 164 value = element.getText().trim(); 165 } 166 167 if (value == null) { 168 if (!nullValues.contains(xpath)) { 169 nullValues.add(xpath); 170 } 171 value = ""; 172 } 173 return value; 174 } 175 176 /** 177 * ????????????xpath?????????Text??? 178 * "//article/date" 179 * "//article/date/book[n]" ?????????N????????? 180 * @param xpath xpath?????? 181 * @param value Text??? 182 */ 183 // @SuppressWarnings("unchecked") 184 public void setElementText(String xpath, String value) { 185 List<?> list = document.selectNodes(xpath); 186 Iterator<?> iter = list.iterator(); 187 if (iter.hasNext()) { 188 Element element = (Element) iter.next(); 189 element.setText(value); 190 } 191 } 192 193 /** 194 * ????????????xpath?????????Attribute??? 195 * "//article/@date" 196 * @param xpath xpath?????? 197 * @return Attribute??? 198 */ 199 // @SuppressWarnings("unchecked") 200 public String getElementAttribute(String xpath) { 201 List<?> list = document.selectNodes(xpath); 202 Iterator<?> iter = list.iterator(); 203 String value = null; 204 if (iter.hasNext()) { 205 Attribute attribute = (Attribute) iter.next(); 206 value = attribute.getValue().trim(); 207 } 208 if (value == null) { 209 if (!nullValues.contains(xpath)) { 210 nullValues.add(xpath); 211 } 212 value = ""; 213 } 214 return value; 215 } 216 217 /** 218 * ????????????xpath?????????Attribute??? 219 * "//article/@date" 220 * "//article/date/book[n]" ?????????N????????? 221 * @param xpath xpath?????? 222 * @param value Attribute??? 223 */ 224 // @SuppressWarnings("unchecked") 225 public void setElementAttribute(String xpath, String value) { 226 List<?> list = document.selectNodes(xpath); 227 Iterator<?> iter = list.iterator(); 228 if (iter.hasNext()) { 229 Attribute attribute = (Attribute) iter.next(); 230 attribute.setValue(value); 231 } 232 } 233 234 /** 235 * ??????xpath??????Element?????? 236 * @param xpath xpath?????? 237 * @return Element?????? 238 */ 239 // @SuppressWarnings("unchecked") 240 public List<?> getListElement(String xpath) { 241 return document.selectNodes(xpath); 242 } 243 244 /** 245 * ????????????Element???xpath??????Element?????? 246 * @param element ????????????Element 247 * @param xpath xpath?????? 248 * @return Element?????? 249 */ 250 // @SuppressWarnings("unchecked") 251 public List<?> getListElement(Element element, String xpath) { 252 return element.selectNodes(xpath); 253 } 254 255 /** 256 * ??????xpath??????Element???Attribute????????? 257 * @param xpath xpath?????? 258 * @return Attribute????????? 259 */ 260 // @SuppressWarnings("unchecked") 261 public List<String> getListElementAttribute(String xpath) { 262 List<String> value = new ArrayList<String>(); 263 List<?> list = document.selectNodes(xpath); 264 Iterator<?> iter = list.iterator(); 265 while (iter.hasNext()) { 266 Attribute attribute = (Attribute) iter.next(); 267 value.add(attribute.getValue().trim()); 268 } 269 return value; 270 } 271 272 /** 273 * ????????????Element???xpath??????Element???Attribute????????? 274 * @param element ????????????Element 275 * @param xpath xpath?????? 276 * @return Attribute????????? 277 */ 278 // @SuppressWarnings("unchecked") 279 public List<String> getListElementAttribute(Element element, String xpath) { 280 List<String> value = new ArrayList<String>(); 281 List<?> list = element.selectNodes(xpath); 282 Iterator<?> iter = list.iterator(); 283 while (iter.hasNext()) { 284 Attribute attribute = (Attribute) iter.next(); 285 value.add(attribute.getValue().trim()); 286 } 287 return value; 288 } 289 290 /** 291 * ??????xpath??????Element???Text????????? 292 * @param xpath xpath?????? 293 * @return Text????????? 294 */ 295 // @SuppressWarnings("unchecked") 296 public List<String> getListElementText(String xpath) { 297 List<String> value = new ArrayList<String>(); 298 List<?> list = document.selectNodes(xpath); 299 Iterator<?> iter = list.iterator(); 300 while (iter.hasNext()) { 301 Element ele = (Element) iter.next(); 302 value.add(ele.getText().trim()); 303 } 304 return value; 305 } 306 307 /** 308 * ????????????Element???xpath??????Element???Text????????? 309 * @param element ????????????Element 310 * @param xpath xpath?????? 311 * @return Text????????? 312 */ 313 // @SuppressWarnings("unchecked") 314 public List<String> getListElementText(Element element, String xpath) { 315 List<String> value = new ArrayList<String>(); 316 List<?> list = element.selectNodes(xpath); 317 Iterator<?> iter = list.iterator(); 318 while (iter.hasNext()) { 319 Element ele = (Element) iter.next(); 320 value.add(ele.getText().trim()); 321 } 322 return value; 323 } 324 325 /** 326 * ??????xpath????????????Element 327 * @param xpath xpath?????? 328 * @return ??????Element 329 */ 330 // @SuppressWarnings("unchecked") 331 public Element getElement(String xpath) { 332 List<?> list = document.selectNodes(xpath); 333 Iterator<?> iter = list.iterator(); 334 if (iter.hasNext()) { 335 return (Element) iter.next(); 336 } 337 return null; 338 } 339 340 /** 341 * ????????????Element???xpath????????????Element 342 * @param element ????????????Element 343 * @param xpath xpath?????? 344 * @return ??????Element 345 */ 346 // @SuppressWarnings("unchecked") 347 public Element getElement(Element element, String xpath) { 348 List<?> list = element.selectNodes(xpath); 349 Iterator<?> iter = list.iterator(); 350 if (iter.hasNext()) { 351 return (Element) iter.next(); 352 } 353 return null; 354 } 355 356 /** 357 * ????????????Element???xpath??????Element???Attribute??? 358 * @param element ????????????Element 359 * @param xpath xpath?????? 360 * @return Attribute??? 361 */ 362 // @SuppressWarnings("unchecked") 363 public String getElementAttribute(Element element, String xpath) { 364 List<?> list = element.selectNodes(xpath); 365 Iterator<?> iter = list.iterator(); 366 String value = null; 367 if (iter.hasNext()) { 368 Attribute attribute = (Attribute) iter.next(); 369 value = attribute.getValue().trim(); 370 } 371 if (value == null) { 372 String path = element.getUniquePath() + "/" + xpath; 373 if (!nullValues.contains(path)) { 374 nullValues.add(path); 375 } 376 value = ""; 377 } 378 return value; 379 } 380 381 /** 382 * ????????????Element???xpath??????Element???Attribute??? 383 * @param element ????????????Element 384 * @param xpath xpath?????? 385 * @param value Attribute??? 386 */ 387 // @SuppressWarnings("unchecked") 388 public void setElementAttribute(Element element, String xpath, String value) { 389 List<?> list = element.selectNodes(xpath); 390 Iterator<?> iter = list.iterator(); 391 if (iter.hasNext()) { 392 Attribute attribute = (Attribute) iter.next(); 393 attribute.setValue(value); 394 } 395 } 396 397 /** 398 * ????????????Element???xpath??????Element???Text??? 399 * @param element ????????????Element 400 * @param xpath xpath?????? 401 * @return Text??? 402 */ 403 // @SuppressWarnings("unchecked") 404 public String getElementText(Element element, String xpath) { 405 if (element == null) { 406 return ""; 407 } 408 List<?> list = element.selectNodes(xpath); 409 Iterator<?> iter = list.iterator(); 410 String value = null; 411 if (iter.hasNext()) { 412 Element ele = (Element) iter.next(); 413 value = ele.getText().trim(); 414 } 415 if (value == null) { 416 String path = element.getUniquePath() + "/" + xpath; 417 if (!nullValues.contains(path)) { 418 nullValues.add(path); 419 } 420 value = ""; 421 } 422 return value; 423 } 424 425 426 /** 427 * ????????????Element???xpath??????Element???Text??? 428 * @param element ????????????Element 429 * @param xpath xpath?????? 430 * @param value Text??? 431 */ 432 // @SuppressWarnings("unchecked") 433 public void setElementText(Element element, String xpath, String value) { 434 List<?> list = element.selectNodes(xpath); 435 Iterator<?> iter = list.iterator(); 436 if (iter.hasNext()) { 437 Element ele = (Element) iter.next(); 438 ele.setText(value); 439 } 440 } 441 442 /** 443 * ??????File????????? 444 * @param file ?????? 445 * @return ?????? 446 */ 447 public static String file2String(File file) { 448 try { 449 InputStream fs = new FileInputStream(file); 450 byte[] b = new byte[(int) file.length()]; 451 fs.read(b); 452 fs.close(); 453 return new String(b); 454 } catch (Exception e) { 455 e.printStackTrace(); 456 } 457 return null; 458 } 459 460 /** 461 * ??????xml??????????????????Document?????? 462 * @param xml xml????????? 463 * @return Document?????? 464 */ 465 public static Document xml2Doc(String xml) { 466 try { 467 return DocumentHelper.parseText(xml); 468 } catch (Exception e) { 469 e.printStackTrace(); 470 } 471 return null; 472 } 473 474 /** 475 * @return List 476 */ 477 public List<String> getNullValues() { 478 return nullValues; 479 } 480 481 public void writeXmlFile(String file,String code) { 482 try { 483 OutputFormat format = OutputFormat.createPrettyPrint(); 484 //format.setEncoding("UTF-8"); // ??????XML?????? 485 //XMLWriter writer = new XMLWriter(new FileWriter(file),format); 486 XMLWriter writer = new XMLWriter(new OutputStreamWriter(new FileOutputStream(file),"utf-8"),format); 487 writer.write(document); 488 writer.close(); 489 } catch (IOException e) { 490 491 e.printStackTrace(); 492 } 493 } 494 }