XOM 从 Node 获取属性?

发布于 2024-08-23 15:44:41 字数 290 浏览 4 评论 0原文

像这样的东西不应该起作用吗?

假设文档格式如下:

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

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

当我打印根元素的值时,看起来这应该可行。这里出了什么问题?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

还如梦归 2024-08-30 15:44:41

将您的节点转换为元素,然后就可以开始了。

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

暗喜 2024-08-30 15:44:41

node.getDocument().getRootElement() 此时您拥有没有属性“id”的元素。

尝试使用 node.getAttribute("id") 代替? (假设节点不为空)

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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文