How to append child in the div parent node?

Learning

I need to append childnodes to their parent nodes, but parent node should be div.

I need to check, if parent node is not div then, check parent's parent node until I get div node. How to do it?

XML file

    <div class="parent">
      <div class="boxed-text">box text1</div>
      <li>
        <div class="boxed-text">box text2</div>
        <p>para</p>
      </li>
      <div class="boxed-text">box text3</div>
      <p>para</p>
    </div>

Expected output(should append to the div parent node)

    <div class="parent">
      <li>
        <p>para</p>
      </li>     
      <p>para</p>
      <div class="boxed-text">box text1</div>
      <div class="boxed-text">box text2</div>
      <div class="boxed-text">box text3</div>
    </div>

But I have got an output like below:(i.e. parent node of boxed-text 2 is litag so, it get appended there instead of div parent node. How to get only div parent node to append child nodes? )

    <div class="parent">
      <li>
        <p>para</p>
        <div class="boxed-text">box text2</div>
      </li>     
      <p>para</p>
      <div class="boxed-text">box text1</div>
      <div class="boxed-text">box text3</div>
    </div>

my code:

$dom = new DOMDocument;
$dom->load("new_test.xml", LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
$xp = new DOMXPath($dom);
$xp->registerNamespace("php", "http://php.net/xpath");
$div = $xp->query("//div[@class='boxed-text']");
foreach($div as $divv) {
    $divv->parentNode->appendChild($divv);
    }
$r = $dom->saveXML();
ThW

You append the node to their parent node, but this will just move them to the last position inside the parent - it will not change the parent.

Xpath has a concept of axes. The default axis is child - the children of the current context. But here a several others including ancestor which represents the path back to the document root.

$dom = new DOMDocument;
$dom->preserveWhiteSpace = false;
$dom->formatOutput = true;
$dom->load("new_test.xml", LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
$xp = new DOMXPath($dom);
$xp->registerNamespace("php", "http://php.net/xpath");

$divs = $xp->evaluate("//div[@class='boxed-text']");
foreach($divs as $div) {
  $ancestor = $xp->evaluate('ancestor::div[1]', $div)->item(0);
  if ($ancestor) {
    $ancestor->appendChild($div);
  }
}

echo $dom->saveXML();

Output:

<?xml version="1.0"?>
<div class="parent">
  <li>
    <p>para</p>
  </li>
  <p>para</p>
  <div class="boxed-text">box text1</div>
  <div class="boxed-text">box text2</div>
  <div class="boxed-text">box text3</div>
</div>

Another way would be to traverse the parent div elements and get the boxed-text descendants for each of them:

$parents = $xp->evaluate("//div[.//div[@class='boxed-text']]");
foreach ($parents as $parent) {
  foreach ($xp->evaluate(".//div[@class='boxed-text']", $parent) as $text) {
    $parent->appendChild($text);
  }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to append child div into a parent div using jquery on the click of a button

From Dev

Unable to append child elements to parent div

From Dev

Unable to append child elements to parent div

From Dev

How do I append a child node to an existing parent node passed to a function?

From Dev

SQL Server append XML child nodes to parent node

From Dev

How to place a child div behind the parent div?

From Dev

How to center a child div with a smaller parent div

From Dev

How to display a child div BEFORE the parent div?

From Dev

How to align 3 child div in a parent div

From Dev

How to include child div into parent div automatically?

From Dev

How to display a child div BEFORE the parent div?

From Dev

How to rotate parent SKSpriteNode only and not the child node

From Dev

How prevent retrieve a child in same parent node?

From Dev

How to access div in parent from child window

From Dev

How to hide parent div based on child name?

From Dev

How to fit resizable child div to his parent

From Dev

React: How to dynamically append child components of differing types into a parent component?

From Dev

Append Child Elements to a Child Element in the same parent

From Dev

How to add Parent node if Child node elements are equal?

From Dev

How to stop propagating event from parent div to child div

From Dev

How to apply margin to child div without affecting parent div?

From Dev

How do I force a child div to be inside parent div?

From Dev

How to use float and still have the child div inside the parent div?

From Dev

How to set child div border to parent div height

From Dev

How to make parent div activate styling of child div for hover and active

From Dev

How to assign margin to child div to not float out of the parent div?

From Dev

how to set child div width 80% to parent div

From Dev

How to ignore child DIV if parent div has some specific class

From Dev

How to make child div to take the size of parent div

Related Related

  1. 1

    How to append child div into a parent div using jquery on the click of a button

  2. 2

    Unable to append child elements to parent div

  3. 3

    Unable to append child elements to parent div

  4. 4

    How do I append a child node to an existing parent node passed to a function?

  5. 5

    SQL Server append XML child nodes to parent node

  6. 6

    How to place a child div behind the parent div?

  7. 7

    How to center a child div with a smaller parent div

  8. 8

    How to display a child div BEFORE the parent div?

  9. 9

    How to align 3 child div in a parent div

  10. 10

    How to include child div into parent div automatically?

  11. 11

    How to display a child div BEFORE the parent div?

  12. 12

    How to rotate parent SKSpriteNode only and not the child node

  13. 13

    How prevent retrieve a child in same parent node?

  14. 14

    How to access div in parent from child window

  15. 15

    How to hide parent div based on child name?

  16. 16

    How to fit resizable child div to his parent

  17. 17

    React: How to dynamically append child components of differing types into a parent component?

  18. 18

    Append Child Elements to a Child Element in the same parent

  19. 19

    How to add Parent node if Child node elements are equal?

  20. 20

    How to stop propagating event from parent div to child div

  21. 21

    How to apply margin to child div without affecting parent div?

  22. 22

    How do I force a child div to be inside parent div?

  23. 23

    How to use float and still have the child div inside the parent div?

  24. 24

    How to set child div border to parent div height

  25. 25

    How to make parent div activate styling of child div for hover and active

  26. 26

    How to assign margin to child div to not float out of the parent div?

  27. 27

    how to set child div width 80% to parent div

  28. 28

    How to ignore child DIV if parent div has some specific class

  29. 29

    How to make child div to take the size of parent div

HotTag

Archive