shikeying
2024-01-11 3b67e947e36133e2a40eb2737b15ea375e157ea0
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
package com.walker.infrastructure.core.domx;
 
import com.walker.infrastructure.core.DomParser;
import com.walker.infrastructure.utils.StringUtils;
import org.jdom2.Document;
import org.jdom2.input.SAXBuilder;
import org.xml.sax.InputSource;
 
import java.io.InputStream;
import java.io.StringReader;
 
public class Jdom2Parser implements DomParser<Document> {
 
    @Override
    public Document getDocumentFromXml(String xml) {
        if (StringUtils.isEmpty(xml)) {
            return null;
        }
        SAXBuilder builder = new SAXBuilder();
        StringReader read = new StringReader(xml);
        InputSource source = new InputSource(read);
        try {
            Document document = builder.build(source);
            return document;
        } catch (Exception e) {
            throw new RuntimeException("异常,getDocumentFromXml:" + xml, e);
        } finally {
            read.close();
        }
    }
 
    @Override
    public Document getDocumentFromStream(InputStream inputStream) {
        SAXBuilder saxBuilder = new SAXBuilder();
        try {
            Document document = saxBuilder.build(inputStream);
            return document;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
 
    @Override
    public boolean isSupported(Class<Object> clazz) {
        return false;
    }
}