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

  • SEARCH
  • Home
  • 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 8096297
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T21:24:31+00:00 2026-06-05T21:24:31+00:00

Consider the following code: $xml = <<<XML <root> <region id=’thisRegion’></region> <region id=’thatRegion’></region> </root> XML;

  • 0

Consider the following code:

$xml = <<<XML
<root>
<region id='thisRegion'></region>
<region id='thatRegion'></region>
</root>
XML;

$partials['thisRegion'] = "<p>Here's this region</p>";
$partials['thatRegion'] = "<p>Here's that region</p>";
$DOM = new DOMDocument;
$DOM->loadXML($xml);

$regions = $DOM->getElementsByTagname('region');

foreach( $regions as $region )
{
    $id = $region->getAttribute('id');
    $partial = $DOM->createDocumentFragment();
    $partial->appendXML( $partials[$id] );
    $region->parentNode->replaceChild($partial, $region);

}

echo $DOM->saveXML();

The output is:

<root>
<p>Here's this region</p>
<region id="thatRegion"/>
</root>

I cannot for the life of me figure out why all of the region tags aren’t being replaced. This is a problem in my project, and at first I thought that it wasn’t replacing elements I appended after the loadXML, but with some experimenting I haven’t been able to narrow down the pattern here.

I would appreciate a code correction to allow me to replace all tags in a DOMDocument with a given Element Node. I also wouldn’t mind any input into a more efficient/practical way to execute this if I haven’t found it.

Thanks in advance!

[edit] PHP 5.3.13

  • 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-06-05T21:24:34+00:00Added an answer on June 5, 2026 at 9:24 pm

    NodeLists are live.
    So when you remove an item inside the document, the NodeList also will be modified. Avoid using a reference to the NodeList and start replacing at the last item:

    $DOM = new DOMDocument;
    $DOM->loadXML($xml);
    $regions = $DOM->getElementsByTagname('region');
    
    $regionsCount = $DOM->getElementsByTagName('region')->length;
    for($i= $regionsCount;$i>0;--$i)
    {   
        $region=$DOM->getElementsByTagName('region')->item($i-1);
        $id = $region->getAttribute('id');
        $partial = $DOM->createDocumentFragment();
        $partial->appendXML( $partials[$id] );
        $region->parentNode->replaceChild($partial, $region);
    }
    
    echo $DOM->saveXML();
    ?>
    

    http://codepad.org/gTjYC4hr

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider this code: import scala.xml.{Node,HasKeyValue} def domatch(x:Node): Node = { val hasBar = new
Consider the following code : $dom = new DOMDocument(); $dom->loadXML($file); $xmlPath = new DOMXPath($dom);
Consider the following script for outputting some XML code: var xmlAsString = '<?xml version=1.0?><person><name
Consider the following snippet of code: function parseXml(xml) { xmlObject= xml; alert(xmlObject.xml); } function
Consider the following code: Class Demo { Person person = new Person(foo); // assume
Consider the following code: static void Main(string[] args) { using (MemoryStream memoryStream = new
This is rather interesting, I think. Consider following code, both the window.onload and body
Consider the following code snippet private void ProcessFile(string fullPath) { XmlTextReader rdr = new
Consider following code: ArrayList<Integer> aList = new ArrayList<Integer>(); aList.add(2134); aList.add(3423); aList.add(4234); aList.add(343); String tmpString
Consider the following situation: There is an xml that contains data @XmlData The @XmlData

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.