1
zj
2025-06-23 dc9bd22833255bc602dd42c7f603ecb50842ab35
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
package kernel.util;
 
 
 
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.io.StringReader;
import java.io.StringWriter;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.URL;
import java.util.Iterator;
import java.util.List;
 
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParserFactory;
 
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentFactory;
import org.dom4j.Element;
import org.dom4j.QName;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.SAXValidator;
import org.dom4j.io.XMLWriter;
import org.dom4j.util.XMLErrorHandler;
import org.xml.sax.EntityResolver;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
 
/**
 * Dom4j 的一个工具类实现
 
 * @author 
 * @version 1.0 changed.log  添加一些增删改属性的静态方法
 */
public abstract class Dom4jUtils {
 
    public static final String DEFAULT_ENCODING = "GB2312";
    
    public static void validateDocument(Document document, final String xsdName, final InputStream xsdStream)
            throws SAXException, ParserConfigurationException {
        // 读取mib-mapping验证文件
        EntityResolver resolver = new EntityResolver() {
            public InputSource resolveEntity(String publicId, String systemId) {
                if (systemId.endsWith(xsdName)) {
                    return new InputSource(xsdStream);
                }
                return null;
            }
        };
 
        // 创建默认的XML错误处理器
        XMLErrorHandler errorHandler = new XMLErrorHandler();
 
        // 获取基于 SAX 的解析器的实例
        SAXParserFactory factory = SAXParserFactory.newInstance();
        // 解析器在解析时验证 XML 内容。
        factory.setValidating(true);
        // 指定由此代码生成的解析器将提供对 XML 名称空间的支持。
        factory.setNamespaceAware(true);
        // 使用当前配置的工厂参数创建 SAXParser 的一个新实例。
        XMLReader parser = factory.newSAXParser().getXMLReader();
        parser.setEntityResolver(resolver);
        parser.setFeature("http://xml.org/sax/features/validation", true);
        parser.setFeature("http://apache.org/xml/features/validation/schema", true);
        parser.setFeature("http://apache.org/xml/features/validation/schema-full-checking", true);
 
        // 创建一个SAXValidator校验工具,并设置校验工具的属性
        SAXValidator validator = new SAXValidator(parser);
        // 设置校验工具的错误处理器,当发生错误时,可以从处理器对象中得到错误信息。
        validator.setErrorHandler(errorHandler);
        // 校验
        validator.validate(document);
 
        // 如果错误信息不为空,说明校验失败,打印错误信息
        if (errorHandler.getErrors().hasContent()) {
            throw new SAXException(errorHandler.getErrors().asXML());
        }
    }
 
//    public static void validateDocument(Document document) throws SAXException, ParserConfigurationException {
//        
//
//        // 创建默认的XML错误处理器
//        XMLErrorHandler errorHandler = new XMLErrorHandler();
//
//        // 获取基于 SAX 的解析器的实例
//        SAXParserFactory factory = SAXParserFactory.newInstance();
//        // 解析器在解析时验证 XML 内容。
//        factory.setValidating(true);
//        // 指定由此代码生成的解析器将提供对 XML 名称空间的支持。
//        factory.setNamespaceAware(true);
//        // 使用当前配置的工厂参数创建 SAXParser 的一个新实例。
//        XMLReader parser = factory.newSAXParser().getXMLReader();
//        parser.setEntityResolver(resolver);
//        parser.setFeature("http://xml.org/sax/features/validation", true);
//        parser.setFeature("http://apache.org/xml/features/validation/schema", true);
//        parser.setFeature("http://apache.org/xml/features/validation/schema-full-checking", true);
//
//        // 创建一个SAXValidator校验工具,并设置校验工具的属性
//        SAXValidator validator = new SAXValidator(parser);
//        // 设置校验工具的错误处理器,当发生错误时,可以从处理器对象中得到错误信息。
//        validator.setErrorHandler(errorHandler);
//        // 校验
//        validator.validate(document);
//
//        // 如果错误信息不为空,说明校验失败,打印错误信息
//        if (errorHandler.getErrors().hasContent()) {
//            throw new SAXException(errorHandler.getErrors().asXML());
//        }
//    }
 
    /**
     * Create dom4j document from xmlSource
     * 
     * @param xmlSource
     *            Object
     * @param validate
     *            boolean
     * @param encoding
     *            String
     * @throws DocumentException
     * @throws IOException
     * @return Document
     */
    public static Document createDOM4jDocument(Object xmlSource, boolean validate, String encoding)
            throws DocumentException, IOException {
        // Use xerces and validate XML file
        Document doc = null;
        SAXReader saxReader = new SAXReader(true);
        saxReader.setValidation(validate);
        if (encoding == null || encoding.equals("")) {
            encoding = DEFAULT_ENCODING;
        }
 
        // Check input source type
        if (xmlSource instanceof String) {
            if (((String) xmlSource).startsWith("<")) {
                // Treat the String as XML code
                StringReader reader = new StringReader(xmlSource.toString());
                doc = saxReader.read(reader, encoding);
            }
            else {
                doc = saxReader.read(new File((String) xmlSource));
            }
        }
        else if (xmlSource instanceof File) {
            doc = saxReader.read((File) xmlSource);
        }
        else if (xmlSource instanceof InputStream) {
            doc = saxReader.read((InputStream) xmlSource);
        }
        else if (xmlSource instanceof Reader) {
            doc = saxReader.read((Reader) xmlSource);
        }
        else if (xmlSource instanceof URL) {
            doc = saxReader.read((URL) xmlSource);
        }
        else if (xmlSource instanceof Document) { // 080908 added
            doc = (Document) xmlSource;
        }
 
        return doc;
    }
 
    /**
     * 新建文档
     * 
     * @return Document 文档节点
     * @added by jiayc
     */
    public static Document createDocument() {
        DocumentFactory factory = new DocumentFactory();
        Document document = factory.createDocument();
        return document;
    }
 
    /**
     * This method will generate XML file in a StringBuffer based on the given
     * Dom4j object.
     * 
     * @param dom4jObj
     *            Object
     * @param encoding
     *            String
     * @throws IOException
     * @return StringBuffer
     */
    public static/* synchronized closed by cxy */StringBuffer generateXMLStringBuffer(Object dom4jObj, String encoding)
            throws IOException {
        StringWriter writer = new StringWriter();
        OutputFormat outformat = OutputFormat.createPrettyPrint();
        // 设置编码类型
        if (encoding == null || encoding.trim().equals("")) {
            encoding = DEFAULT_ENCODING;
        }
        outformat.setEncoding(encoding);
        // dom4j 支持直接输出OBJECT
        XMLWriter xmlWriter = null;
        xmlWriter = new XMLWriter(writer, outformat);
        xmlWriter.write(dom4jObj);
        xmlWriter.flush();
 
        return writer.getBuffer();
    }
 
    /**
     * 反射调用DOM4J的某方法,生成BUFFER,不是很有用?只能调一层?
     * 
     * @param dom4jObj
     *            Object
     * @param encoding
     *            String
     * @param className
     *            String
     * @param methodName
     *            String
     * @param parameterTypes
     *            Class[]
     * @param parameterValues
     *            Object[]
     * @throws IOException
     * @throws ClassNotFoundException
     * @throws SecurityException
     * @throws NoSuchMethodException
     * @throws IllegalArgumentException
     * @throws IllegalAccessException
     * @throws InvocationTargetException
     * @return StringBuffer
     */
    public static/** synchronized */
    StringBuffer generateXMLStringBufferByMethodInvoke(Object dom4jObj, String encoding, String className,
            String methodName, Class<?>[] parameterTypes, Object[] parameterValues) throws IOException,
            ClassNotFoundException, SecurityException, NoSuchMethodException, IllegalArgumentException,
            IllegalAccessException, InvocationTargetException {
        Class<?> componentClass = null;
        componentClass = Class.forName(className);
        Method method;
        method = componentClass.getMethod(methodName, parameterTypes);
        Object o = method.invoke(dom4jObj, parameterValues);
        return generateXMLStringBuffer(o, encoding);
    }
 
    /**
     * 插入节点,不是好方法,可有好方法替代?
     * 
     * @param parent
     *            Element param insert
     * @param insertBefore
     *            Element
     * @return Element
     */
    public static Element insertChild(Element parent, Element toInsert, Element insertBefore) {
        String str = parent.asXML();
        String before = insertBefore.asXML();
        String insert = toInsert.asXML();
        int pos = -1;
        if ((pos = str.indexOf(before)) >= 0) {
            str = str.substring(0, pos) + insert + str.substring(pos);
        }
 
        try {
            parent = Dom4jUtils.createDOM4jDocument(str, false, "").getRootElement();
        } catch (Exception ex) {
        }
 
        return parent;
    }
 
    /**
     * 插入节点,不是好方法,可有好方法替代?
     * 
     * @param parent
     *            Element param insert
     * @param insertBefore
     *            Element
     * @return Element
     */
    public static Element insertChildAfter(Element parent, Element toInsert, Element insertAfter) {
        String str = parent.asXML();
        String after = insertAfter.asXML();
        String insert = toInsert.asXML();
        int pos = -1;
        if ((pos = str.indexOf(after)) >= 0) {
            pos += after.length();
            str = str.substring(0, pos) + insert + str.substring(pos);
        }
 
        try {
            parent = Dom4jUtils.createDOM4jDocument(str, false, "").getRootElement();
        } catch (Exception ex) {
        }
 
        return parent;
    }
 
    /**
     * 添加新Element节点,值为String类型
     * 
     * @param parent
     *            父节点
     * @param name
     *            新节点名称
     * @param value
     *            新节点值
     * @return element
     * @throws XMLDocException
     */
    public static Element appendChild(Element parent, String name, String value) {
        if (parent == null) {
            return null;
        }
 
        Element element = parent.addElement(new QName(name, parent.getNamespace()));
        if (value != null) {
            element.addText(value);
        }
        return element;
    }
 
    /**
     * 删除子节点
     * 
     * @param parent
     *            Element 父节点
     * @param name
     *            String 删除节点名
     * @param value
     *            String 删除节点值
     * @return Element
     */
    public static Element removeChild(Element parent, String name, String value) {
        if (parent == null) {
            return null;
        }
 
        Iterator<?> iter = parent.elementIterator();
        Element del = null;
        while (iter.hasNext()) {
            Element tmp = (Element) iter.next();
            if (tmp.getName().equals(name) && tmp.getText().equals(value)) {
                del = tmp;
            }
        }
        if (del != null) {
            parent.remove(del);
        }
        return parent;
    }
 
    /**
     * 添加属性
     * 
     * @param ele
     *            Element
     * @param attributeName
     *            String
     * @param attributeValue
     *            String
     * @return Element
     */
    public static Element appendAttribute(Element ele, String attributeName, String attributeValue) {
        if (ele == null) {
            return null;
        }
 
        ele.addAttribute(attributeName, attributeValue);
        return ele;
    }
 
    /**
     * 删除属性
     * 
     * @param ele
     *            Element
     * @param attributeName
     *            String
     * @return Element
     */
    public static Element removeAttribute(Element ele, String attributeName) {
        if (ele == null) {
            return null;
        }
 
        Attribute att = ele.attribute(attributeName);
        ele.remove(att);
        return ele;
    }
 
    /**
     * 修改属性
     * 
     * @param ele
     *            Element
     * @param attributeName
     *            String
     * @param attributeValue
     *            String
     * @return Element
     */
    public static Element setAttribute(Element ele, String attributeName, String attributeValue) {
        if (ele == null) {
            return null;
        }
 
        Attribute att = ele.attribute(attributeName);
        if (att != null) {
            att.setText(attributeValue);
        }
        else {
            Dom4jUtils.appendAttribute(ele, attributeName, attributeValue);
        }
        return ele;
    }
 
    /**
     * 取Element节点下的所有子节点内容,转换成字符串
     * 
     * @param element
     *            Element
     * @return String
     */
    public static String getElementContext(Element element) {
        if (element == null) {
            return null;
        }
        String str = element.getText();
        for (Iterator<?> i = element.elementIterator(); i.hasNext();) {
            Element elementTmp = (Element) i.next();
            str = str + elementTmp.asXML();
            // do something
        }
        return str;
    }
 
    /**
     * 
     * @param element
     *            Element
     * @param path
     *            String
     * @param attr
     *            String
     * @return String
     */
    public static String getElementContext(Element element, String path, String attr) {
        Element el = element.element(path);
        if (attr == null || attr.trim().equals("")) {
            return el.getText();
        }
        else {
            return el.attributeValue(attr);
        }
    }
 
    /**
     * 根据XPATH 获取元素内容,text。 "/path/@seq" 获取属性值 "/path/" 获取元素
     * 
     * @param element
     *            Element
     * @param path
     *            String
     * @return String
     */
    public static String getElementContext(Element element, String path) {
        if (element == null || path == null) {
            return null;
        }
 
        Object o = element.selectSingleNode(path);
        if (o == null) { // 无此节点
            return null;
        }
 
        if (o instanceof Element) { // 1、元素 Element
            Element e = (Element) o;
            if (e.isTextOnly()) { // text only
                return e.getText();
            }
            else { // element
                try {
                    return Dom4jUtils.generateXMLStringBuffer(e, "").toString();
                } catch (IOException ex1) {
                    return null;
                }
            }
        }
        else if (o instanceof Attribute) { // 2、属性 Attribute
            return ((Attribute) o).getValue();
        }
        else { // 3、其他 Other node
            try {
                return Dom4jUtils.generateXMLStringBuffer(o, "").toString();
            } catch (IOException ex) {
                return null;
            }
        }
    }
 
    /**
     * 根据XPATH 获取元素内容,text。 "/path/@seq" 获取属性值 "/path/" 获取元素
     * 
     * @param element
     *            Element
     * @param path
     *            String
     * @return String
     */
    public static String[] getElementContextArray(Element element, String path) {
        if (element == null || path == null) {
            return null;
        }
        List<?> nodes = element.selectNodes(path);
        String[] eleContext = new String[nodes.size()];
        Iterator<?> iter = nodes.iterator();
        int length = 0;
        while (iter.hasNext()) {
            Object o = (Object) iter.next();
            // Object o = element.selectNodes(path);
            if (o instanceof Element) { // 1、元素 Element
                Element e = (Element) o;
                if (e.isTextOnly()) { // text only
                    // return e.getText();
                    eleContext[length] = e.getText();
                    length++;
                }
                else { // element
                    try {
                        eleContext[length] = Dom4jUtils.generateXMLStringBuffer(e, "").toString();
                        length++;
                        // return Dom4jUtil.generateXMLStringBuffer(e,
                        // "").toString();
                    } catch (IOException ex1) {
                        return null;
                    }
                }
            }
            else if (o instanceof Attribute) { // 2、属性 Attribute
                // return ( (Attribute) o).getValue();
                eleContext[length] = ((Attribute) o).getValue();
                length++;
            }
            else { // 3、其他 Other node
                try {
                    eleContext[length] = Dom4jUtils.generateXMLStringBuffer(o, "").toString();
                    length++;
                    // return Dom4jUtil.generateXMLStringBuffer(o,
                    // "").toString();
                } catch (IOException ex) {
                    return null;
                }
            }
        }
        return eleContext;
    }
 
    /**
     * 添加新Element节点,值为String类型
     * 
     * @param root
     *            根节点
     * @param xpath
     *            新节点路径 eg:/root/a/a1 or /root/b/b1/@seq
     * @param value
     *            新节点值
     * @param isGroup
     *            boolean
     * @return element
     */
    public static Element appendElement(Element root, String xpath, String value, boolean isGroup) {
        if (root == null) {
            return null;
        }
        int j = xpath.lastIndexOf("/");
        String path = xpath.substring(0, j);
        Element node = (Element) root.selectSingleNode(path);
        String[] paths = xpath.split("/");
        if (isGroup) {
            if (node == null) {
                for (int i = 2; i < paths.length; i++) {
                    root = root.addElement(new QName(paths[i], root.getNamespace()));
                }
            }
            else {
                root = node.addElement(new QName(paths[paths.length - 1], root.getNamespace()));
            }
        }
        else {
            for (int i = 2; i < paths.length; i++) {
                root = root.addElement(new QName(paths[i], root.getNamespace()));
            }
        }
 
        if (value != null) {
            root.addText(value);
 
        }
 
        return root;
    }
 
    /**
     * 
     * @param args
     *            String[]
     */
    public static void main(String[] args) throws IOException {
        Document document = Dom4jUtils.createDocument();
        Element root = document.addElement(new QName("root"));
 
        Dom4jUtils.appendChild(root, "hi", "hi");
        Dom4jUtils.appendChild(root, "hi1", "1");
        Dom4jUtils.appendChild(root, "hi2", "2");
 
        root.addElement("kaka");
 
        root = Dom4jUtils.insertChild(root, (Element) root.selectSingleNode("hi1"),
                (Element) root.selectSingleNode("hi"));
        root = Dom4jUtils.insertChildAfter(root, (Element) root.selectSingleNode("hi2"),
                (Element) root.selectSingleNode("hi"));
 
        System.out.println(Dom4jUtils.generateXMLStringBuffer(root, ""));
    }
}