forked from wujun728/jun_java_plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathXmlUtil.java
More file actions
897 lines (846 loc) · 27.8 KB
/
Copy pathXmlUtil.java
File metadata and controls
897 lines (846 loc) · 27.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
package org.myframework.util;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.Reader;
import java.io.StringReader;
import java.util.List;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentFactory;
import org.dom4j.DocumentType;
import org.dom4j.Element;
import org.dom4j.QName;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;
/**
* POJO -> XML
* @author Administrator
*
*/
public class XmlUtil {
/**
* 将POJO 转化为 XML字符串
* @param pojo
* @return
*/
public static String getXmlFromPojo(Object pojo) {
XStream xstream = new XStream(new DomDriver());
xstream.registerConverter(new SingleValueCalendarConverter());
String strxml = xstream.toXML(pojo);
return strxml;
}
/**
* 缺省字符集
* */
public static final String DEFAULT_ENCODING = "UTF-8";
/**
* 私有构造函数,阻止非法调用构造函数
* */
private XmlUtil() {
}
/**
* Return the child element with the given name. The element must be in
* the same name space as the parent element.
* @param element The parent element
* @param name The child element name
* @return The child element
*/
public static Element child(Element element, String name) {
return element.element(new QName(name, element.getNamespace()));
}
/**
* 得到给定结点下的孩子节点
* @param element 节点
* @param name 子节点名称
* @param optional 是否是可选的
* @return 子节点
* @throws XMLDocException
*/
public static Element child(Element element,
String name,
boolean optional)
throws XMLDocException {
Element child = element.element(new QName(name, element.getNamespace()));
if (child == null && !optional) {
throw new XMLDocException(name + " element expected as child of " +
element.getName() + ".");
}
return child;
}
/** Return the child elements with the given name. The elements must be in
the same name space as the parent element.
@param element The parent element
@param name The child element name
@return The child elements
*/
public static List children(Element element, String name) {
return element.elements(new QName(name, element.getNamespace()));
}
/**
* 得到某个节点下的属性信息
* @param element 节点
* @param name 属性名
* @param optional 是否是可选的
* @return 值
* @throws XMLDocException
*/
public static String getAttribute(Element element,
String name,
boolean optional)
throws XMLDocException {
Attribute attr = null;
if(element!=null)
attr = element.attribute(name);
if (attr == null && !optional) {
if(element!=null)
throw new XMLDocException("Attribute " + name + " of " +
element.getName() + " expected.");
else
return null;
} else if (attr != null) {
return attr.getValue();
}
else {
return null;
}
}
/**
* 得到节点属性值,并且作为日期型返回
* @param element 节点
* @param name 属性名
* @param optional 是否是可选的
* @return 值
* @throws XMLDocException
*/
public static java.util.Date getAttributeAsDate(Element element,
String name,
boolean optional)
throws XMLDocException {
String value = getAttribute(element, name, optional);
if ( (optional) && ( (value == null) || (value.equals("")))) {
return null;
}
else {
try {
//如果可选就不抛出异常
return DateUtil.parse(value );
}
catch ( Exception exception) {
throw new XMLDocException(element.getName() + "/@" + name +
" attribute: value format error.",
exception);
}
}
}
/**
* 得到某个节点下的属性信息,值以字符串的形式返回
* @param element 节点
* @param name 属性名
* @param optional 是否是可选的
* @return 值
* @throws XMLDocException
*/
public static String getAttributeAsString(Element element,
String name,
boolean optional)
throws XMLDocException {
return getAttribute(element, name, optional);
}
/**
* 得到某个节点下的属性信息,值以整数的形式返回。
* 如果没有值或是转化为整形,那么抛出异常。
* @param element 节点
* @param name 属性名
* @param optional 是否是可选的
* @return 值
* @throws XMLDocException
*/
public static int getAttributeAsInt(Element element,
String name,
boolean optional)
throws XMLDocException {
try {
return Integer.parseInt(getAttribute(element, name, optional));
}
catch (NumberFormatException exception) {
throw new XMLDocException(element.getName() + "/@" + name +
" attribute: value format error.",
exception);
}
}
/**
* 得到某个节点下的属性信息,值以整数的形式返回。
* 如果该值是可选的,并且没有该值的话,就返回调用者提供缺省值。
* @param element 节点
* @param name 属性名
* @param defaultValue 缺省值
* @param optional 是否是可选的
* @return 值
* @throws XMLDocException
*/
public static int getAttributeAsInt(Element element,
String name,
int defaultValue,
boolean optional)
throws XMLDocException {
String value = getAttribute(element, name, optional);
if ( (optional) && ( (value == null) || (value.equals("")))) {
return defaultValue;
}
else {
try {
return Integer.parseInt(value);
}
catch (NumberFormatException exception) {
throw new XMLDocException(element.getName() + "/@" + name +
" attribute: value format error.",
exception);
}
}
}
/**
* 得到某个节点下的属性信息,值以float的形式返回。
* 如果没有值或是转化为float,那么抛出异常。
* @param element 节点
* @param name 属性名
* @param optional 是否是可选的
* @return 值
* @throws XMLDocException
*/
public static float getAttributeAsFloat(Element element,
String name,
boolean optional)
throws XMLDocException {
try {
return Float.parseFloat(getAttribute(element, name, optional));
}
catch (NumberFormatException exception) {
throw new XMLDocException(element.getName() + "/@" + name +
" attribute: value format error.",
exception);
}
}
/**
* 得到某个节点下的属性信息,值以float的形式返回。
* 如果没有值,返回缺省值;如果有,那么转化为float,如果不能转化那么抛出异常。
* @param element 节点
* @param name 属性名
* @param defaultValue 缺省值
* @param optional 是否是可选的
* @return 值
* @throws XMLDocException
*/
public static float getAttributeAsFloat(Element element,
String name,
float defaultValue,
boolean optional)
throws XMLDocException {
String value = getAttribute(element, name, optional);
if ( (optional) && ( (value == null) || (value.equals("")))) {
return defaultValue;
}
else {
try {
return Float.parseFloat(value);
}
catch (NumberFormatException exception) {
throw new XMLDocException(element.getName() + "/@" + name +
" attribute: value format error.",
exception);
}
}
}
/**
* 得到某个节点下的属性信息,值以长整数的形式返回。
* 如果没有值或是转化为整形,那么抛出异常。
* @param element 节点
* @param name 属性名
* @param optional 是否是可选的
* @return 值
* @throws XMLDocException
*/
public static long getAttributeAsLong(Element element,
String name,
boolean optional)
throws XMLDocException {
try {
return Long.parseLong(getAttribute(element, name, optional));
}
catch (NumberFormatException exception) {
throw new XMLDocException(element.getName() + "/@" + name +
" attribute: value format error.",
exception);
}
}
/**
* 得到某个节点下的属性信息,值以整数的形式返回。
* 如果该值是可选的,并且没有该值的话,就返回调用者提供缺省值。
* @param element 节点
* @param name 属性名
* @param defaultValue 缺省值
* @param optional 是否是可选的
* @return 值
* @throws XMLDocException
*/
public static long getAttributeAsLong(Element element,
String name,
long defaultValue,
boolean optional)
throws XMLDocException {
String value = getAttribute(element, name, optional);
if ( (optional) && ( (value == null) || (value.equals("")))) {
return defaultValue;
}
else {
try {
return Long.parseLong(value);
}
catch (NumberFormatException exception) {
throw new XMLDocException(element.getName() + "/@" + name +
" attribute: value format error.",
exception);
}
}
}
/**
* 得到某个节点下的某名字的第一个孩子节点
* @param element 节点
* @param name 子节点名称
* @param optional 是否是可选的
* @return 值
* @throws XMLDocException
*/
public static Element getFirstChild(Element element,
String name,
boolean optional)
throws XMLDocException {
java.util.List list = element.elements(new QName(name,
element.getNamespace()));
//如果数目大于0,那么直接取第一个就可以了
if (list.size() > 0) {
return (Element) list.get(0);
}
else {
if (!optional) {
throw new XMLDocException(name +
" element expected as first child of " +
element.getName() + ".");
}
else {
return null;
}
}
}
/**
* 得到同名兄弟节点,同名的第一个节点,可以是自己
* @param element 节点
* @param optional 是否是可选的
* @return 节点
* @throws XMLDocException
*/
public static Element getSibling(Element element, boolean optional)
throws XMLDocException {
return getSibling(element, element.getName(), optional);
}
/**
* 按名称得到兄弟节点
* @param element 节点
* @param name 子节点名称
* @param optional 是否是可选的
* @return 节点
* @throws XMLDocException
*/
public static Element getSibling(Element element,
String name,
boolean optional)
throws XMLDocException {
java.util.List list = element.getParent().elements(name);
if (list.size() > 0) {
return (Element) list.get(0);
}
else {
if (!optional) {
throw new XMLDocException(name + " element expected after " +
element.getName() + ".");
}
else {
return null;
}
}
}
/**
* 得到给定节点的值,以字符串返回
* @param element 节点
* @param optional 是否是可选的
* @return 值
* @throws XMLDocException
*/
public static String getContent(Element element, boolean optional)
throws XMLDocException {
String content = null;
if(element!=null)
content =element.getText();
if (content == null && !optional) {
if(element!=null)
throw new XMLDocException(element.getName() +
" element: content expected.");
else
return null;
} else {
return content;
}
}
/**
* 得到给定节点的值,以字符串返回
* @param element 节点
* @param optional 是否是可选的
* @return 值
* @throws XMLDocException
*/
public static String getContentAsString(Element element, boolean optional)
throws XMLDocException {
return getContent(element, optional);
}
/**
* 得到给定节点的值,以整数类型返回
* @param element 节点
* @param optional 是否是可选的
* @return 值
* @throws XMLDocException
*/
public static int getContentAsInt(Element element, boolean optional)
throws XMLDocException {
try {
return Integer.parseInt(getContent(element, optional));
}
catch (NumberFormatException exception) {
throw new XMLDocException(element.getName() +
" element: content format error.",
exception);
}
}
/**
* 得到给定节点的值,以整数类型返回
* @param element 节点
* @param defaultValue 缺省值
* @param optional 是否是可选的
* @return 值
* @throws XMLDocException
*/
public static int getContentAsInt(Element element,
int defaultValue,
boolean optional)
throws XMLDocException {
String value = getContent(element, optional);
if ( (optional) && (value == null || value.equals(""))) {
return defaultValue;
}
else {
try {
return Integer.parseInt(value);
}
catch (NumberFormatException exception) {
throw new XMLDocException(element.getName() +
" element: content format error.",
exception);
}
}
}
/**
* 得到给定节点的值,以长整类型返回
* @param element 节点
* @param optional 是否是可选的
* @return 值
* @throws XMLDocException
*/
public static long getContentAsLong(Element element, boolean optional)
throws XMLDocException {
try {
return Long.parseLong(getContent(element, optional));
}
catch (NumberFormatException exception) {
throw new XMLDocException(element.getName() +
" element: content format error.",
exception);
}
}
/**
* 得到给定节点的值,以整数类型返回
* @param element 节点
* @param defaultValue 缺省值
* @param optional 是否是可选的
* @return 值
* @throws XMLDocException
*/
public static long getContentAsLong(Element element,
long defaultValue,
boolean optional)
throws XMLDocException {
String value = getContent(element, optional);
if ( (optional) && (value == null || value.equals(""))) {
return defaultValue;
}
else {
try {
return Long.parseLong(value);
}
catch (NumberFormatException exception) {
throw new XMLDocException(element.getName() +
" element: content format error.",
exception);
}
}
}
/**
* 得到给定节点的值,以浮点类型返回
* @param element 节点
* @param optional 是否是可选的
* @return 值
* @throws XMLDocException
*/
public static float getContentAsFloat(Element element, boolean optional)
throws XMLDocException {
try {
return Float.parseFloat(getContent(element, optional));
}
catch (NumberFormatException exception) {
throw new XMLDocException(element.getName() +
" element: content format error.",
exception);
}
}
/**
* 得到给定节点的值,以浮点类型返回
* @param element 节点
* @param defaultValue 缺省值
* @param optional 是否是可选的
* @return 值
* @throws XMLDocException
*/
public static float getContentAsFloat(Element element,
float defaultValue,
boolean optional)
throws XMLDocException {
String value = getContent(element, optional);
if ( (optional) && (value == null || value.equals(""))) {
return defaultValue;
}
else {
try {
return Float.parseFloat(value);
}
catch (NumberFormatException exception) {
throw new XMLDocException(element.getName() +
" element: content format error.",
exception);
}
}
}
/**
* 得到给定节点的值,以日期类型返回
* @param element 节点
* @param optional 是否是可选的
* @return 值
* @throws XMLDocException
*/
public static java.util.Date getContentAsDate(Element element,
boolean optional)
throws XMLDocException {
String value = getContent(element, optional);
if ( (optional) && (value == null || value.equals(""))) {
return null;
}
else {
try {
return DateUtil.parse(value );
}
catch ( Exception exception) {
throw new XMLDocException(element.getName() +
" element: content format error.",
exception);
}
}
}
/**
* 给定父节点和子节点名称,得到子节点值
* @param root 父节点
* @param subTagName 子节点
* @return 值
*/
public static String getSubTagValue(Element root, String subTagName) {
String returnString = root.elementText(subTagName);
return returnString;
}
/**
* 给定父节点,子节点名称,孙节点名称;得到值
* @param root 父节点
* @param tagName 子节点名称
* @param subTagName 孙节点名称
* @return 值
*/
public static String getSubTagValue(Element root,
String tagName,
String subTagName) {
Element child = root.element(tagName);
String returnString = child.elementText(subTagName);
return returnString;
}
/**
* 新Element节点,值为String类型
* @param parent 父节点
* @param name 新节点名称
* @param value 新节点值
* @return element
* @throws XMLDocException
*/
public static Element appendChild(Element parent,
String name,
String value) {
Element element = parent.addElement(new QName(name, parent.getNamespace()));
if (value != null) {
element.addText(value);
}
return element;
}
/**
* 增加新Element节点,无值
* @param parent 父节点
* @param name 新节点名称
* @return Element 新建节点
* @throws XMLDocException
*/
public static Element appendChild(Element parent, String name) {
return parent.addElement(new QName(name, parent.getNamespace()));
}
/**
* 增加新Element节点,值为int类型
* @param parent 父节点
* @param name 新节点名称
* @param value 新节点值
* @return element
* @throws XMLDocException
*/
public static Element appendChild(Element parent,
String name,
int value) {
return appendChild(parent, name, String.valueOf(value));
}
/**
* 增加新Element节点,值为长整形
* @param parent 父节点
* @param name 新节点名称
* @param value 新节点值
* @return element
* @throws XMLDocException
*/
public static Element appendChild(Element parent,
String name,
long value) {
return appendChild(parent, name, String.valueOf(value));
}
/**
* 新加一个float值类型的节点,值为浮点型
* @param parent 父节点
* @param name 新节点的名称
* @param value 新节点的值
* @return element
* @throws XMLDocException
*/
public static Element appendChild(Element parent,
String name,
float value) {
return appendChild(parent, name, String.valueOf(value));
}
/**
* 增加新Element节点,值为日期型
* @param parent 父节点
* @param name 新节点名称
* @param value 新节点值
* @return element
* @throws XMLDocException
*/
public static Element appendChild(Element parent,
String name,
java.util.Date value) {
return appendChild(parent, name,
DateUtil.getInstance().format(value));
}
/**
* 检查文档dtd定义是否正确
* @param document 文档节点
* @param dtdPublicId dtd定义
* @return boolean 相同返回true,否则false
*/
public static boolean checkDocumentType(Document document,
String dtdPublicId) {
DocumentType documentType = document.getDocType();
if (documentType != null) {
String publicId = documentType.getPublicID();
return publicId != null && publicId.equals(dtdPublicId);
}
return true;
}
/**
* 新建文档
* @return Document 文档节点
* @throws XMLDocException
*/
public static Document createDocument()
throws XMLDocException {
DocumentFactory factory = new DocumentFactory();
Document document = factory.createDocument();
return document;
}
/**
* 通过Reader读取Document文档
* 如果encodingStr为null或是"",那么采用缺省编码GB2312
* @param in Reader器
* @param encoding 编码器
* @return documment
* @throws XMLDocException
*/
public static Document fromXML(Reader in, String encoding)
throws XMLDocException {
try {
if (encoding == null || encoding.equals("")) {
encoding = DEFAULT_ENCODING;
}
SAXReader reader = new SAXReader();
Document document = reader.read(in, encoding);
return document;
}
catch (Exception ex) {
throw new XMLDocException(ex);
}
}
/**
* 给定输入流读取XML的Document。
* 如果encodingStr为null或是"",那么采用缺省编码GB2312
* @param inputSource 输入源
* @param encoding 编码器
* @return document
* @throws XMLDocException
*/
public static Document fromXML(InputStream inputSource, String encoding)
throws XMLDocException {
try {
if (encoding == null || encoding.equals("")) {
encoding = DEFAULT_ENCODING;
}
SAXReader reader = new SAXReader();
Document document = reader.read(inputSource, encoding);
return document;
}
catch (Exception ex) {
throw new XMLDocException(ex);
}
}
/**
* 直接从字符串得到XML的Document
* @param source 把一个字符串文本转化为XML的Document对象
* @param encoding 编码器
* @return <code>Document</code>
* @throws XMLDocException
*/
public static Document fromXML(String source, String encoding)
throws XMLDocException {
return fromXML(new StringReader(source), encoding);
}
/**
* 把XML的Document转化为java.io.Writer输出流
* 不支持给定Schema文件的校验
* @param document XML文档
* @param outWriter 输出写入器
* @param encoding 编码类型
* @throws XMLDocException 如果有任何异常转化为该异常输出
*/
public static void toXML(Document document, java.io.Writer outWriter,
String encoding)
throws XMLDocException {
//
OutputFormat outformat = OutputFormat.createPrettyPrint();
if (encoding == null || encoding.trim().equals("")) {
encoding = DEFAULT_ENCODING;
}
//设置编码类型
outformat.setEncoding(encoding);
XMLWriter xmlWriter = null;
try {
xmlWriter = new XMLWriter(outWriter, outformat);
xmlWriter.write(document);
xmlWriter.flush();
}
catch (java.io.IOException ex) {
throw new XMLDocException(ex);
}
finally {
if (xmlWriter != null) {
try {
xmlWriter.close();
}
catch (java.io.IOException ex) {
}
}
}
}
/**
* 把XML的Document转化为java.io.Writer输出流
* 不支持给定Schema文件的校验
* @param document XML文档
* @param outStream 输出写入器
* @param encoding 编码类型
* @throws XMLDocException 如果有任何异常转化为该异常输出
*/
public static void toXML(Document document, java.io.OutputStream outStream,
String encoding)
throws XMLDocException {
//
OutputFormat outformat = OutputFormat.createPrettyPrint();
if (encoding == null || encoding.trim().equals("")) {
encoding = DEFAULT_ENCODING;
}
//设置编码类型
outformat.setEncoding(encoding);
XMLWriter xmlWriter = null;
try {
xmlWriter = new XMLWriter(outStream, outformat);
xmlWriter.write(document);
xmlWriter.flush();
}
catch (java.io.IOException ex) {
throw new XMLDocException(ex);
}
finally {
if (xmlWriter != null) {
try {
xmlWriter.close();
}
catch (java.io.IOException ex) {
}
}
}
}
/**
* 把XML文档转化为String返回
* @param document 要转化的XML的Document
* @param encoding 编码类型
* @return <code>String</code>
* @throws XMLDocException 如果有任何异常转化为该异常输出
*/
public static String toXML(Document document, String encoding)
throws XMLDocException {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
toXML(document, stream, encoding);
if (stream != null) {
try {
stream.close();
}
catch (java.io.IOException ex) {
}
}
return stream.toString();
}
}