XOM getting attribute from Node?

混江龙づ霸主 提交于 2020-01-16 19:23:00

问题


Shouldn't something like this work?

Assuming a document formatted as such:

<root>
   <element id = "a"></element>
</root>

Node node = doc.query("/root/element").get(0);
String id = node.getDocument().getRootElement().getAttribute("id");

When I print the value of the root element, it looks as if this should work. What's failing, here?


回答1:


Cast your node to an Element, and you're good to go.




回答2:


node.getDocument().getRootElement() at this point you have the element which does not have an attribute "id".

Try node.getAttribute("id") instead ? (assuming node is not null)



来源:https://stackoverflow.com/questions/2363599/xom-getting-attribute-from-node

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!