NamedNodeMap 的通用 foreach 迭代
在 Java 中,查看 NamedNodeMap接口,如何用泛型迭代它?它似乎使用 Node 而不是 String,但我不太确定如何使用 Node 对象...
NamedNodeMap namedNodeMap = doc.getAttributes();
Map<String, String> stringMap = (Map<String, String>) namedNodeMap;
for (Map.Entry<String, String> entry : stringMap.entrySet()) {
//key,value stuff here
}
是的,我可以看到如何在不使用泛型和常规 for 循环的情况下进行迭代,但我想使用上面?成语?对于地图。当然,问题似乎是,尽管名称如此,NamedNodeMap 实际上并没有实现 Map 接口! :(
我猜你必须硬着头皮做一些类似的事情:
/*
* Iterates through the node attribute map, else we need to specify specific
* attribute values to pull and they could be of an unknown type
*/
private void iterate(NamedNodeMap attributesList) {
for (int j = 0; j < attributesList.getLength(); j++) {
System.out.println("Attribute: "
+ attributesList.item(j).getNodeName() + " = "
+ attributesList.item(j).getNodeValue());
}
}
没有什么比这更好的了?
In Java, looking at the NamedNodeMap interface, how do you iterate it with generics? It seems to use Node rather than String, but I'm not so sure how to use Node objects...
NamedNodeMap namedNodeMap = doc.getAttributes();
Map<String, String> stringMap = (Map<String, String>) namedNodeMap;
for (Map.Entry<String, String> entry : stringMap.entrySet()) {
//key,value stuff here
}
Yes, I can see how to iterate without using generics and with a regular for loop, but I'd like to use the above ?idiom? for maps. Of course, the problem would appear to be that, despite the name, NamedNodeMap doesn't actually implement the Map interface! :(
Guess you just gotta bite the bullet here and do something like:
/*
* Iterates through the node attribute map, else we need to specify specific
* attribute values to pull and they could be of an unknown type
*/
private void iterate(NamedNodeMap attributesList) {
for (int j = 0; j < attributesList.getLength(); j++) {
System.out.println("Attribute: "
+ attributesList.item(j).getNodeName() + " = "
+ attributesList.item(j).getNodeValue());
}
}
there's nothing nicer?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以为
NamedNodeMap
创建自己的Iterable
包装器,然后在 foreach 循环中使用它。例如,这可能是一个简单的实现:
在这种情况下,这将是用法:
使用类似的方法,您可以通过
Map.Entry
创建一个Iterable
。代码>实例。You can create your own
Iterable
wrapper forNamedNodeMap
and then use it in a foreach loop.For example, this could be a simple implementation:
In this case, this would be the usage:
With a similar approach you could create an
Iterable
overMap.Entry<String, String>
instances.我认为没有更好的方法来使用这些 API。 (更新:好的 - 也许 https://stackoverflow.com/a/28626556/139985 也算不错。
)请注意,W3C DOM Java API 是在 Java 拥有泛型或新的
for
语法,甚至Iterator
接口之前指定的。另请记住,用于 Java 的 W3C DOM API 实际上是将 IDL 规范映射到 Java 的结果。如果您想要更好的 API 来在内存中操作 XML 等,也许您应该看看 JDOM。
I don't think there is a nicer way to use those APIs. (Update: OK - maybe https://stackoverflow.com/a/28626556/139985 counts as nice.)
Bear in mind that the W3C DOM Java APIs were specified before Java had generics or the new
for
syntax, or even theIterator
interface. Also bear in mind that the W3C DOM APIs for Java are actually the result of mapping an IDL specification to Java.If you want nicer APIs for manipulating XML, etc in memory, maybe you should look at JDOM.
因为您无法投射 NamedNodeMap到 Map,我建议循环使用像这样的经典 for 循环:
As you can't cast NamedNodeMap to a Map, I suggest to loop using a classic for loop like that :
来自 Java 8 解决方案:
From Java 8 solution: