Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6913253
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T09:11:40+00:00 2026-05-27T09:11:40+00:00

This code is only appending 3 of the 5 name nodes. Why is that?

  • 0

This code is only appending 3 of the 5 name nodes. Why is that?
Here is the original XML:
It has 5 name nodes.

<?xml version='1.0'?>
<products>
<product>
<itemId>531670</itemId>
<modelNumber>METRA ELECTRONICS/MOBILE AUDIO</modelNumber>
<categoryPath>
<category><name>Buy</name></category>
<category><name>Car, Marine &amp; GPS</name></category>
<category><name>Car Installation Parts</name></category>
<category><name>Deck Installation Parts</name></category>
<category><name>Antennas &amp; Adapters</name></category>
</categoryPath>
</product>

</products>  

Then is run this PHP code. which is suppossed to appened ALL name nodes into the product node.

<?php

// load up your XML
$xml = new DOMDocument;
$xml->load('book.xml');

// Find all elements you want to replace. Since your data is really simple,
// you can do this without much ado. Otherwise you could read up on XPath.
// See http://www.php.net/manual/en/class.domxpath.php
//$elements = $xml->getElementsByTagName('category');

// WARNING: $elements is a "live" list -- it's going to reflect the structure
// of the document even as we are modifying it! For this reason, it's
// important to write the loop in a way that makes it work correctly in the
// presence of such "live updates".


foreach ($xml->getElementsByTagName('product') as $product ) {
foreach($product->getElementsByTagName('name') as $name ) {
    $product->appendChild($name );
}
$product->removeChild($xml->getElementsByTagName('categoryPath')->item(0));
}

// final result:
$result = $xml->saveXML();
echo $result;
?>

The end result is this and it only appends 3 of the name nodes:

<?xml version="1.0"?>
<products>
<product>
<itemId>531670</itemId>
<modelNumber>METRA ELECTRONICS/MOBILE AUDIO</modelNumber>
<name>Buy</name>
<name>Antennas &amp; Adapters</name>
<name>Car Installation Parts</name>
</product>
</products>

Why is it only appending 3 of the name nodes?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T09:11:41+00:00Added an answer on May 27, 2026 at 9:11 am

    You can temporarily add the name elements to an array before appending them, owing to the fact that you’re modifying the DOM in real time. The node list generated by getElementsByTagName() may change as you are moving nodes around (and indeed that appears to be what’s happening).

    <?php
    
    // load up your XML
    $xml = new DOMDocument;
    $xml->load('book.xml');    
    
    // Array to store them
    $append = array();
    foreach ($xml->getElementsByTagName('product') as $product ) {
    foreach($product->getElementsByTagName('name') as $name ) {
      // Stick $name onto the array
      $append[] = $name;
    }
    // Now append all of them to product
    foreach ($append as $a) {
        $product->appendChild($a);
    }
    
    $product->removeChild($xml->getElementsByTagName('categoryPath')->item(0));
    }
    
    // final result:
    $result = $xml->saveXML();
    echo $result;
    ?>
    

    Output, with all values appended:

    <?xml version="1.0"?>
    <products>
    <product>
    <ItemId>531670</ItemId>
    <modelNumber>METRA ELECTRONICS/MOBILE AUDIO</modelNumber>
    
    <name>Buy</name><name>Car, Marine &amp; GPS</name><name>Car Installation Parts</name><name>Deck Installation Parts</name><name>Antennas &amp; Adapters</name></product>
    
    </products>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a pattern that I could apply to refactor this code? The only
This feels like the kind of code that only fails in-situ, but I will
In this thread some one commented that the following code should only be used
How to make this css code cross browser compatible using jquery. this code only
Can any one explain why the output of this code is only 'hello' and
How could I refactor this code to use only one Dir[ ] call? Dir[
I have this code to run a slideshow (this is only part of what
Is there anyway this code can be refactored? The only difference is the order
i made a windows service & add project installer.in which only contain this code.
Trying to figure out this pseudo code. The following is assumed.... I can only

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.