在Java中判断xml集合是否为空,可以使用以下方法:
XPath xPath = XPathFactory.newInstance().newXPath();
String expression = "count(/root/*)";
Double count = (Double) xPath.evaluate(expression, xmlDocument, XPathConstants.NUMBER);
if (count == 0) {
// xml集合为空
} else {
// xml集合不为空
}
NodeList nodeList = xmlDocument.getElementsByTagName("elementName");
if (nodeList.getLength() == 0) {
// xml集合为空
} else {
// xml集合不为空
}
其中,xmlDocument
表示XML文档对象,elementName
表示要判断的元素名称。
辰迅云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
推荐阅读: java中jlabel如何设置为透明背景