I have an XML Document:
我有一个XML文档:
<entities xmlns="urn:yahoo:cap">
<entity score="0.988">
<text end="4" endchar="4" start="0" startchar="0">Messi</text>
<wiki_url>http://en.wikipedia.com/wiki/Lionel_Messi</wiki_url>
<types>
<type region="us">/person</type>
</types>
</entity>
</entities>
I have a TreeMap<String,String> data
which stores the getTextContent()
for both the "text"
and "wiki_url"
element. Some "entity"
s will only have the "text"
element (no "wiki_url"
) so i need a way of finding out when there is only the text element as the child and when there is a "wiki_url"
. I could use document.getElementByTag("text")
& document.getElementByTag("wiki_url")
but then I would lose the relationship between the text and the url.
我有一个TreeMap
I'm trying to get the amount of elements within the "entity"
element by using:
我试图通过使用以下方法获取“entity”元素中的元素数量:
NodeList entities = document.getElementsByTagName("entity"); //List of all the entity nodes
int nchild; //Number of children
System.out.println("Number of entities: "+ entities.getLength()); //Prints 1 as expected
nchild=entities.item(0).getChildNodes().getLength(); //Returns 7
However as shows above this returns 7 (which I don't understand, surely its 3 or 4 if you include the grandchild) I was then going to use the number of children to cycle through them all to check if getNodeName().equals("wiki_url")
and save it to data if correct.
然而,正如上面的显示,这返回7(我不明白,如果你包括孙子肯定是3或4)我然后将使用子数来循环遍历它们以检查是否getNodeName()。equals( “wiki_url”)并将其保存到数据中,如果正确的话。
Why is it that i am getting the number of children as 7 when I can only count 3 children and 1 grandchild?
为什么当我只能算上3个孩子和1个孙子时,我的孩子数量为7?
1 个解决方案
#1
3
The white-spaces following >
of <entity score="0.988">
also count for nodes, similarly end of line chararcter between the tags are also parsed to nodes. If you are interested in a particular node with a name, add a helper method like below and call wherever you want.
Node getChild(final NodeList list, final String name)
{
for (int i = 0; i < list.getLength(); i++)
{
final Node node = list.item(i);
if (name.equals(node.getNodeName()))
{
return node;
}
}
return null;
}
and call
final NodeList childNodes = entities.item(0).getChildNodes();
final Node textNode = getChild(childNodes, "text");
final Node wikiUrlNode = getChild(childNodes, "wiki_url");
Normally when working with DOM, comeup with helper methods like above to simplify main processing logic.
通常在使用DOM时,请使用上面的辅助方法来简化主处理逻辑。
#1
3
The white-spaces following >
of <entity score="0.988">
also count for nodes, similarly end of line chararcter between the tags are also parsed to nodes. If you are interested in a particular node with a name, add a helper method like below and call wherever you want.
Node getChild(final NodeList list, final String name)
{
for (int i = 0; i < list.getLength(); i++)
{
final Node node = list.item(i);
if (name.equals(node.getNodeName()))
{
return node;
}
}
return null;
}
and call
final NodeList childNodes = entities.item(0).getChildNodes();
final Node textNode = getChild(childNodes, "text");
final Node wikiUrlNode = getChild(childNodes, "wiki_url");
Normally when working with DOM, comeup with helper methods like above to simplify main processing logic.
通常在使用DOM时,请使用上面的辅助方法来简化主处理逻辑。