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 8488195
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T21:27:57+00:00 2026-06-10T21:27:57+00:00

Below is the piece of code that I am using to create a bunch

  • 0

Below is the piece of code that I am using to create a bunch of xml nodes and trying to attach to parent that is passed to it.

Not sure whats going wrong, Parent node is not getting augmented.

input.xml

<?xml version="1.0"?>
<root>
    <steps> This is a step </steps>
    <steps> This is also a step </steps>
</root>

output.xml should be

<?xml version="1.0">
<root>
    <steps>
        <step>
            <step_number>1</step_number>
            <step_detail>Step detail goes here</step_detail>
        </step> 
    </steps>
    <steps>
        <step>
            <step_number>2</step_number>
            <step_detail>Step detail of the 2nd step</step_detail>
        </step> 
    </steps>
</root>


    <?php
    $xml = simplexml_load_file( 'input.xml' );

    $domxml  = dom_import_simplexml( $xml );

    //Iterating through all the STEPS node
    foreach ($steps as $stp ){
        createInnerSteps( &$stp, $stp->nodeValue , 'Expected Result STring' );

        echo 'NODE VAL  :-----' . $stp->nodeValue;// Here it should have the new nodes, but it is not
    }

    function createInnerSteps( &$parent )
    {   
        $dom  = new DOMDocument();
        // Create Fragment, where all the inner childs gets appended
        $frag = $dom->createDocumentFragment();

        // Create Step Node
        $stepElm = $dom->createElement('step');
        $frag->appendChild( $stepElm );

        // Create Step_Number Node and CDATA Section
        $stepNumElm = $dom->createElement('step_number');
        $cdata = $dom->createCDATASection('1');
        $stepNumElm->appendChild ($cdata );

        // Append Step_number to Step Node
        $stepElm->appendChild( $stepNumElm );

        // Create step_details and append to Step Node 
        $step_details = $dom->createElement('step_details');
        $cdata = $dom->createCDATASection('Details');
        $step_details->appendChild( $cdata );

        // Append step_details to step Node
        $stepElm->appendChild( $step_details );

        // Add Parent Node to step element
            //  I get error PHP Fatal error:  
            //  Uncaught exception 'DOMException' with message 'Wrong Document Error'
        $parent->appendChild( $frag );

        //echo 'PARENT : ' .$parent->saveXML(); 
    }

?>
  • 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-10T21:27:59+00:00Added an answer on June 10, 2026 at 9:27 pm

    You are getting the Wrong Document Error because you create a new DOMDocument object within createInnerSteps(), each time it is called.

    When the code reaches the line $parent->appendChild($frag), the $frag belongs to the document created in the function and the $parent belongs to the main document that you are trying to manipulate. You cannot move nodes (or fragments) between documents like this.

    The simplest solution is to only ever use the one document by replacing:

    $dom  = new DOMDocument();
    

    with

    $dom = $parent->ownerDocument;
    

    See a running example.

    Finally, to get your desired output, you will need to remove the existing text within each <steps> element.

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

Sidebar

Related Questions

I have a piece of code (below) that can get the text of an
I have a piece of code similar to the below that I have been
This is a small piece of code shown below which is using do while
I'm trying to write a piece of code that periodically tries to connect to
In the below piece of code, theta x and y are varying smoothly for
I really can't understand how the below piece of code is working... options.each {
Firstly sorry for the long piece of code pasted below. This is my first
Answer solved in edit below I had this piece of code Dictionary<Merchant, int> remaingCards
Im getting the following error for the piece of code below : expression list
I have a piece of matlab code below which reads data from a table.

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.