Dom4j 分离节点、Jython

发布于 2024-07-20 07:22:19 字数 1872 浏览 8 评论 0原文

我正在使用 Dom4j 分离节点,如下所示:

<div name="divName">
    Some Text Here
    <span>Some Text Here</span>
</div>

我按名称选择 div 节点,然后使用分离方法将其删除:

xpathValue = "//*[contains(@name, 'divName')]"
xpath = dom.createXPath(xpathValue)
    if xpath != None:
        nodes = xpath.selectNodes(dom)
        if len(nodes) > 0:
            for node in nodes:
                node.detach()

这似乎很好地删除了 div,我注意到它还删除了该 div 中的元素和文本还。 我想要实现的是删除 div 而不删除 div 内的元素和文本,从而导致:

Some Text Here
<span>Some Text Here</span>

是否可以使用 dom4j 实现此目的? 如果没有关于如何解决这个问题的任何建议?

干杯

Eef

更新:

@alamar

通过获取您的代码并对其进行一些编辑,我已经实现了我想要的目标,这就是我想到的:

   xpathValue = "//*[contains(@name, 'divName')]"
   xpath = dom.createXPath(xpathValue)
    if xpath != None:
        nodes = xpath.selectNodes(dom)
        if len(nodes) > 0:
            for node in nodes:
                parent = node.getParent()
                nodeContents = node.content()
                    if len(nodeContents) > 0:
                        for subNode in nodeContents:
                            parent.add(subNode.clone().detach())
            node.detach()

这似乎可行,但将节点添加到末尾在以下情况下的父节点:

<div name="parent">
    <div name="divName">
        Some Text Here
        <span>Some Text Here</span>
    </div>
    <div name="keep"></div>
</div>

结果是这样的:

<div name="parent">
    <div name="keep"></div>
    Some Text Here
    <span>Some Text Here</span>
</div>

我试图弄清楚如何让已删除节点的内容保留在其原始位置,在名为“keep”的 div 之前,而不是添加在div 名称为“keep”。 我尝试了一些方法,但似乎无法实现这一目标,有人可以帮忙吗?

埃夫

I am using Dom4j to detach a node, like below:

<div name="divName">
    Some Text Here
    <span>Some Text Here</span>
</div>

I am selecting the div node by name and then using the detach method to remove it:

xpathValue = "//*[contains(@name, 'divName')]"
xpath = dom.createXPath(xpathValue)
    if xpath != None:
        nodes = xpath.selectNodes(dom)
        if len(nodes) > 0:
            for node in nodes:
                node.detach()

This seems to remove the div fine, I noticed that it also removes elements and text within that div also. What I am looking to achive is removing the div without removing the elements and text inside the div, resulting in this:

Some Text Here
<span>Some Text Here</span>

Is it possible to achive this with dom4j? If not any suggestions on how to go about this?

Cheers

Eef

Update:

@alamar

I have achived what I wanted by taking your code and editing it a little and this is what I have come up with:

   xpathValue = "//*[contains(@name, 'divName')]"
   xpath = dom.createXPath(xpathValue)
    if xpath != None:
        nodes = xpath.selectNodes(dom)
        if len(nodes) > 0:
            for node in nodes:
                parent = node.getParent()
                nodeContents = node.content()
                    if len(nodeContents) > 0:
                        for subNode in nodeContents:
                            parent.add(subNode.clone().detach())
            node.detach()

This seems to work, but adds the nodes to the end of the parent node in the below situation:

<div name="parent">
    <div name="divName">
        Some Text Here
        <span>Some Text Here</span>
    </div>
    <div name="keep"></div>
</div>

The result is this:

<div name="parent">
    <div name="keep"></div>
    Some Text Here
    <span>Some Text Here</span>
</div>

I am trying to figure out how to get the contents of the removed node to stay in its original position, before thed div named "keep", instead of being added after the div with the name "keep". I have tried a few thing but can not seem achive this, could anyone help?

Eef

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

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

发布评论

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

评论(3

极度宠爱 2024-07-27 07:22:19

如果您想保持元素的顺序,您应该向 parent 询问其 content()
在该 content (这是一个由父元素支持的 List)集合中,您应该找到您的 div 并将其替换为该 div 的 content()

坦率地说,我不记得在 python 中执行此操作的惯用方法。

大概

if xpath != None:
    nodes = xpath.selectNodes(dom)
    if len(nodes) > 0:
        for node in nodes:
            parent = node.getParent()
            index = parent.indexOf(node)
            siblings = parent.content()
            nodeContents = node.content()
                if len(nodeContents) > 0:
                    for subNode in nodeContents:
                        siblings.add(subNode.clone().detach(), index++)
        node.detach()

If you want to keep the order of elements, you should really ask parent for its content().
In that content (which is a List backed by parent element) collection, you should find your div and replace it with that div's content().

I don't remember idiomatic way to do that in python, frankly.

probably

if xpath != None:
    nodes = xpath.selectNodes(dom)
    if len(nodes) > 0:
        for node in nodes:
            parent = node.getParent()
            index = parent.indexOf(node)
            siblings = parent.content()
            nodeContents = node.content()
                if len(nodeContents) > 0:
                    for subNode in nodeContents:
                        siblings.add(subNode.clone().detach(), index++)
        node.detach()
御守 2024-07-27 07:22:19

尝试:

if xpath != None:
    nodes = xpath.selectNodes(dom)
    if len(nodes) > 0:
        for div in nodes:
            parent = div.getParent()
            div.detach()
            for(child in node.content())
                child.detach()
                parent.add(child)

我相信它会成功。

即在分离每个 div 后,您应该将每个 div 的子级重新附加到 div 的父级中。

Try:

if xpath != None:
    nodes = xpath.selectNodes(dom)
    if len(nodes) > 0:
        for div in nodes:
            parent = div.getParent()
            div.detach()
            for(child in node.content())
                child.detach()
                parent.add(child)

I believe it would do the trick.

I.e. after detaching every div, you should reattach every div's child into div's parent.

清引 2024-07-27 07:22:19

我有一个类似的问题并用以下函数解决了它(对我来说效果很好)

它在做什么:它将简单地删除该父标签并将元素内的每个元素和节点包含到该位置的父元素。

   private void _replaceTagByContent(Element element) {
        Element parent = element.getParent();
        List elements = parent.elements();
        int insertPosition = elements.indexOf(element);

        // add them all to the parent again
        for (int i = 0, size = elements.size(); i < size; i++) {
            Node node = (Node) elements.get(i);
            if (i == insertPosition) {

                // if we are here, then this has to be an element, since
                // wo do only replace elements ...

                for (int j = element.nodeCount() - 1; j >= 0; j--) {
                    Node theNode = element.node(j);
                    theNode.detach();
                    elements.add(i, theNode);
                }

                // finally remove this node
                elements.remove(node);
            }
        }
    }

享受cnsntrk

i had a similar problem and solved it with the following function (works fine for me)

What is it doing: it will simply remove that parent tag and includes every element and node inside the element to the parent at that position.

   private void _replaceTagByContent(Element element) {
        Element parent = element.getParent();
        List elements = parent.elements();
        int insertPosition = elements.indexOf(element);

        // add them all to the parent again
        for (int i = 0, size = elements.size(); i < size; i++) {
            Node node = (Node) elements.get(i);
            if (i == insertPosition) {

                // if we are here, then this has to be an element, since
                // wo do only replace elements ...

                for (int j = element.nodeCount() - 1; j >= 0; j--) {
                    Node theNode = element.node(j);
                    theNode.detach();
                    elements.add(i, theNode);
                }

                // finally remove this node
                elements.remove(node);
            }
        }
    }

enjoy cnsntrk

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