37
37
import org .w3c .dom .NodeList ;
38
38
import org .xml .sax .SAXException ;
39
39
40
+ import javax .xml .XMLConstants ;
40
41
import javax .xml .parsers .DocumentBuilder ;
41
42
import javax .xml .parsers .DocumentBuilderFactory ;
42
43
import javax .xml .parsers .ParserConfigurationException ;
43
44
import javax .xml .xpath .XPath ;
44
45
import javax .xml .xpath .XPathConstants ;
45
46
import javax .xml .xpath .XPathExpressionException ;
46
47
import javax .xml .xpath .XPathFactory ;
48
+ import javax .xml .xpath .XPathFactoryConfigurationException ;
47
49
import java .io .File ;
48
50
import java .io .IOException ;
49
51
import java .util .Arrays ;
@@ -128,9 +130,11 @@ void parseXpath() {
128
130
// 加载 XML 文档
129
131
DocumentBuilder builder = factory .newDocumentBuilder ();
130
132
document = builder .parse (project );
131
-
133
+ XPathFactory xpathfactory = XPathFactory .newInstance ();
134
+ // 关闭限制 #2229
135
+ xpathfactory .setFeature (XMLConstants .FEATURE_SECURE_PROCESSING , false );
132
136
// 执行 XPath 查询
133
- XPath xpath = XPathFactory . newInstance () .newXPath ();
137
+ XPath xpath = xpathfactory .newXPath ();
134
138
NodeList nodeList = (NodeList ) xpath .evaluate (xpathExpr , document , XPathConstants .NODESET );
135
139
136
140
// 构造标签->值映射
@@ -151,7 +155,8 @@ void parseXpath() {
151
155
System .out .println (tag + " 未找到" );
152
156
}
153
157
}
154
- } catch (ParserConfigurationException | SAXException | IOException | XPathExpressionException e ) {
158
+ } catch (ParserConfigurationException | SAXException | IOException | XPathExpressionException |
159
+ XPathFactoryConfigurationException e ) {
155
160
throw new RuntimeException (e );
156
161
}
157
162
}
0 commit comments