是否可以使用 Jython 和 dom4j 在元素之间添加元素?

发布于 2024-07-07 10:16:56 字数 376 浏览 4 评论 0原文

刚刚在玩 dom4j,非常棒。 我做了一年半的 PHP 开发人员,刚刚找到一份新工作并开始使用 Jython,现在我有空闲时间,它是一门很棒的语言。 正在考虑尝试在元素之间添加一个元素,如下所示:

<div id="content">
    <p>Some text in here</p>
    <!--New p tag here-->
    <p>Some text in here</p>
</div>

是否可以在两个元素之间插入另一个 p 标签,而不将 DOM 转换为字符串并再次返回 DOM,因为这是我能做到的唯一方法是什么?

Just playing with dom4j, excellent. I was a PHP developer for a year and half, just got a new job and started playing around with Jython now I have spare time, its a great language. Was thinking about trying to add a element in between element, example below:

<div id="content">
    <p>Some text in here</p>
    <!--New p tag here-->
    <p>Some text in here</p>
</div>

Is it possible to insert another p tag in between the two without converting the DOM to a string and back to DOM again as this is the only way I have been able to do it?

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

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

发布评论

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

评论(1

只为一人 2024-07-14 10:16:56

我对 dom4j 具体了解不多,但我会这样做:

  1. div#content 的所有子节点复制到列表中,然后删除其子节点。
  2. 将第一个节点插回到 div#content 节点中。
  3. 将新的 p 节点插入到 div#content 节点中。
  4. 将最后一个节点插回到 div#content 节点中。

我不知道这在 DOM 方面是什么,但如果节点表示为 Jythonic 对象,那么应该很容易做到这一点。

I don't know much about dom4j specifically, but I would do it like this:

  1. Copy all child nodes of div#content into a list, then delete its children.
  2. Insert the first node back into the div#content node.
  3. Insert the new p node into the div#content node.
  4. Insert the last node back into the div#content node.

I don't know what this would be in terms of the DOM, but if nodes are represented as Jythonic objects, then it should be easy to do this.

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