如何知道XML元素是否包含Java编程的数组



我有一个XML响应:

<abc:parent>
<abc:item>
...
</abc:item>
<abc:item>
...
</abc:item>
</abc:parent>

也可能只有一个子元素:

<abc:parent>
<abc:item>
...
</abc:item>
</abc:parent>

以前,父级有一个属性"SOAP-ENC:arrayType",可以轻松地确定它包含一个子元素数组,但现在响应采用前面描述的格式。

以前的例子:

<abc:parent xsi:type="SOAP-ENC:Array" SOAP-ENC:arrayType="Struct[1]" SOAP-ENC:offset="[0]">
<abc:item>
...
</abc:item> 
</abc:parent>

以前的检查示例:

private void parseNode(DomNode node, String xpath) {
boolean isLeaf = true;
for (DomNode childNode : node.getChildren()) {
if (DomNode.TEXT_NODE != childNode.getNodeType()) {
isLeaf = false;
}
}
if (!isLeaf) {
if (node instanceof DomElement) {
DomElement eNode = (DomElement)node;
String arrayType = eNode.getAttribute("SOAP-ENC:arrayType");
if (!PkUtil.isEmpty(arrayType)) {
// we have a node with an array of children
}
}
}
}

我无法控制回复消息。

如何确定父元素是否包含子元素数组?在我看来,有了这些结果和信息,这似乎是不可能的。

您可以通过检查您拥有的item的数量来做到这一点,如果它有多个item,您就知道它是一个数组:

DomElement eNode = (DomElement) node;
NodeList childList = eNode.getElementsByTagName("item");
if (childList.getLength() > 1) {
// we have a node with an array of children
}