I have a org.w3c.dom.Node object.
我有一个org.w3c.dom.Node对象。
I would like to see if it has any other siblings.
我想看看它是否还有其他兄弟姐妹。
Here's what I have tried:
这是我尝试过的:
Node sibling = node.getNextSibling();
if(sibling == null)
return true;
else
return false;
HOWEVER, for some reason (possibly due to identation or line spaces in the source XML) I am not getting the expected result.
但是,出于某种原因(可能是由于源XML中的标识或行空间),我没有得到预期的结果。
[Also
node.getParentNode().getChildNodes().getLength()
is giving a value higher higher than I would expect.]
给出的价值高于我的预期。]
I welcome your suggestions to improve this code.
我欢迎您提出改进此代码的建议。
EDIT
As suggested below it seems that blank nodes are thwarting my attempts to count the siblings.
如下所示,似乎空白节点阻碍了我计算兄弟姐妹的企图。
The xml looks something like this:
xml看起来像这样:
<a>
<b>
<c>I have this node:</c>
<c>I want to know how many of these there are.</c>
<c>This is another sibling.</c>
</b>
<b>
</b>
</a>
Starting from my node (the first <c></c> above), how do I find out the number of other siblings?
从我的节点(上面的第一个
4 个解决方案
#1
3
When you get child nodes from another node, you receive all direct children. This will include Element nodes, Text nodes, and Comment nodes. Most often, you'll only care about Element nodes. So you can check the result of getChildNodes to see if any are ELEMENT_NODE.
当您从另一个节点获取子节点时,您将收到所有直接子节点。这将包括Element节点,Text节点和Comment节点。通常,您只关心Element节点。因此,您可以检查getChildNodes的结果,看看是否有ELEMENT_NODE。
An example function for what you wanted to do:
您想要做的示例函数:
public function isSingleChild(Node node) {
boolean singleChild = true;
NodeList siblings = node.getParentNode().getChildNodes();
for(int i = 0, int len = siblings.getLength(); i < len; i++) {
if(siblings.item(i).getNodeType() == Node.ELEMENT_NODE) {
singleChild = false;
break;
}
}
return singleChild;
}
Just to see what each node type is like:
只是为了看看每个节点类型是什么样的:
<div>
<!-- Comment Node -->
<p>Element node</p>
a text node
</div>
Getting the div's childNodes would return 3 nodes, a comment node containing "Comment Node", an Element node of "P", and a Text node of "a text node".
获取div的childNodes将返回3个节点,包含“Comment Node”的注释节点,“P”的Element节点和“text node”的Text节点。
#2
2
you could count the number of childnodes in the items parent after filtering out the whitespace nodes. (which you probably don't want, but are also probably messing up your expected result).
您可以在过滤掉空白节点后计算子项中的子节点数。 (你可能不想要,但也可能搞砸你的预期结果)。
I can't put it in actual java real quick because i'm not familiar enough with it, but it should be pretty straightforward.
我不能真正把它放在实际的java中,因为我对它不够熟悉,但它应该非常简单。
#3
1
I'm not a Java guy, but in C# one way to test an element to see if it has siblings is to use an XPath query:
我不是Java人,但在C#中测试元素以查看它是否有兄弟姐妹的一种方法是使用XPath查询:
bool hasSiblings = elm.SelectNodes("../*").Count == 1;
The *
selector finds only elements, which keeps you from having to remember that "node" can mean element, text node, processing instruction, or comment. (I'd estimate that in 90% of cases, when someone talking about XML uses the word "node" they really mean "element".)
*选择器只查找元素,这使您不必记住“节点”可以表示元素,文本节点,处理指令或注释。 (我估计在90%的情况下,当有人谈论XML使用“节点”这个词时,他们的意思是“元素”。)
#4
0
Thanks to all for your input.
感谢大家的投入。
Here is the code that I used in the end, it is based on seanmonstar's code.
这是我最后使用的代码,它基于seanmonstar的代码。
public Boolean isSingleChild(Node node)
{
int elementNodes = 0;
NodeList siblings = node.getParentNode().getChildNodes();
for(int x = 0 ; x < siblings.getLength(); x++)
{
if(siblings.item(x).getNodeType() == Node.ELEMENT_NODE)
{
elementNodes++;
}
}
if(elementNodes == 1)
return true;
else
return false;
}
#1
3
When you get child nodes from another node, you receive all direct children. This will include Element nodes, Text nodes, and Comment nodes. Most often, you'll only care about Element nodes. So you can check the result of getChildNodes to see if any are ELEMENT_NODE.
当您从另一个节点获取子节点时,您将收到所有直接子节点。这将包括Element节点,Text节点和Comment节点。通常,您只关心Element节点。因此,您可以检查getChildNodes的结果,看看是否有ELEMENT_NODE。
An example function for what you wanted to do:
您想要做的示例函数:
public function isSingleChild(Node node) {
boolean singleChild = true;
NodeList siblings = node.getParentNode().getChildNodes();
for(int i = 0, int len = siblings.getLength(); i < len; i++) {
if(siblings.item(i).getNodeType() == Node.ELEMENT_NODE) {
singleChild = false;
break;
}
}
return singleChild;
}
Just to see what each node type is like:
只是为了看看每个节点类型是什么样的:
<div>
<!-- Comment Node -->
<p>Element node</p>
a text node
</div>
Getting the div's childNodes would return 3 nodes, a comment node containing "Comment Node", an Element node of "P", and a Text node of "a text node".
获取div的childNodes将返回3个节点,包含“Comment Node”的注释节点,“P”的Element节点和“text node”的Text节点。
#2
2
you could count the number of childnodes in the items parent after filtering out the whitespace nodes. (which you probably don't want, but are also probably messing up your expected result).
您可以在过滤掉空白节点后计算子项中的子节点数。 (你可能不想要,但也可能搞砸你的预期结果)。
I can't put it in actual java real quick because i'm not familiar enough with it, but it should be pretty straightforward.
我不能真正把它放在实际的java中,因为我对它不够熟悉,但它应该非常简单。
#3
1
I'm not a Java guy, but in C# one way to test an element to see if it has siblings is to use an XPath query:
我不是Java人,但在C#中测试元素以查看它是否有兄弟姐妹的一种方法是使用XPath查询:
bool hasSiblings = elm.SelectNodes("../*").Count == 1;
The *
selector finds only elements, which keeps you from having to remember that "node" can mean element, text node, processing instruction, or comment. (I'd estimate that in 90% of cases, when someone talking about XML uses the word "node" they really mean "element".)
*选择器只查找元素,这使您不必记住“节点”可以表示元素,文本节点,处理指令或注释。 (我估计在90%的情况下,当有人谈论XML使用“节点”这个词时,他们的意思是“元素”。)
#4
0
Thanks to all for your input.
感谢大家的投入。
Here is the code that I used in the end, it is based on seanmonstar's code.
这是我最后使用的代码,它基于seanmonstar的代码。
public Boolean isSingleChild(Node node)
{
int elementNodes = 0;
NodeList siblings = node.getParentNode().getChildNodes();
for(int x = 0 ; x < siblings.getLength(); x++)
{
if(siblings.item(x).getNodeType() == Node.ELEMENT_NODE)
{
elementNodes++;
}
}
if(elementNodes == 1)
return true;
else
return false;
}