0

Suppose I have two Match nodes, and I try to parent one to the other, and then I try to retrieve the child's parent node.

Match p = $("parent");
Match c = $("child");

p.append(c);

Object o = c.parent();

At this point, o seems to be an empty element list: "[]", which is not what I was expecting.

What is the correct way to parent a node to another, and then retrieve its new parent? Obviously, I know I could work directly with p, but assume that at runtime, I only have a reference to c. Thanks!

Jesús Zazueta
  • 1,160
  • 1
  • 17
  • 32

1 Answers1

1

You can try with append() after creating the elements, like:

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import static org.joox.JOOX.$;

public class Main {

    public static void main(String[] args) {
        Document document = $("<root/>").document();
        Element p = document.createElement("parent");
        Element c = document.createElement("child");
        $(p).append(c);
        $(document).append(p);
        System.out.println($(document).toString());
        System.out.println($(document).find("child").parent());
    }
}

It yields:

<root><parent><child/></parent></root>
<parent><child/></parent>
Birei
  • 35,723
  • 2
  • 77
  • 82