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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T20:28:27+00:00 2026-06-05T20:28:27+00:00

this may sound pretty straight forward, but still I want to post this question

  • 0

this may sound pretty straight forward, but still I want to post this question in the forum. I have a xml file, which needs to be appended with data after the main element and save the xml file without overwriting the existing xml file but to append the data to already existing data and update the xml file.

For example my xml data looks something similar to this:

<maincontent>
    <headercontent>
        <product num="2102">
            <name>MSG</name>
            <category>Wellness</category>
            <available content="YES"></available>
        </product>
        <product num="2101">
            <name>YSD</name>
            <category>Music</category>
            <available content="NO"></available>
        </product>
        <product num="2100">
            <name>RCS</name>
            <category>Media</category>
            <available content="YES"></available>
        </product>
    </headercontent>
</maincontent>

I want to add another product with all the info and append the newly added data at the top so that the newly added data should come after the headercontent.

Data to be added:

        <product num="2103">
            <name>AGB</name>
            <category>Movies</category>
            <available content="YES"></available>
        </product>

The updated xml file should be looking like this as shown below:

<maincontent>
    <headercontent>
        <product num="2103">
                <name>AGB</name>
                <category>Movies</category>
                <available content="YES"></available>
    </product>
        <product num="2102">
            <name>MSG</name>
            <category>Wellness</category>
            <available content="YES"></available>
        </product>
        <product num="2101">
            <name>YSD</name>
            <category>Music</category>
            <available content="NO"></available>
        </product>
        <product num="2100">
            <name>RCS</name>
            <category>Media</category>
            <available content="YES"></available>
        </product>
    </headercontent>
</maincontent>

Any useful advice or a piece of example code would be really helpful.

Edit:

sorry guys I haven’t posted any php code, my fault. Here is the code which I have been working on:

Thanks

<?php

 $xmldoc = new DomDocument();    
    $xmldoc->formatOutput = true;

    $productNum = "2103";
    $name = "AGB";
    $category = "Movies";
    $content = "YES";

    if($xml = file_get_contents('main.xml')){
        $xmldoc->loadXML($xml);

        $root = $xmldoc->firstChild;        

        $newElement = $xmldoc->createElement('product');
        $root->appendChild($newElement);
        $numAttribute = $xmldoc->createAttribute("num");
        $numAttribute->value = $productNum;
        $newElement->appendChild($numAttribute);   

        $nameElement = $xmldoc->createElement('name');
        $root->appendChild($nameElement);
        $nameText = $xmldoc->createTextNode($name);
        $nameElement->appendChild($nameText);

        $categoryElement = $xmldoc->createElement('category');
        $root->appendChild($categoryElement);
        $categoryText = $xmldoc->createTextNode($category);
        $categoryElement->appendChild($categoryText);

        $availableElement = $xmldoc->createElement('available');
        $root->appendChild($availableElement);
        $availableAttribute = $xmldoc->createAttribute("content");
        $availableAttribute->value = $content;
        $availableElement->appendChild($availableAttribute);   


        $xmldoc->save('main.xml');
    }
?>

My xml file gets updated but the data is added to the firstchild and that too at the bottom, instead I want to add data after and in the beginning as shown above.
Here is my output:

<maincontent>
    <headercontent>
        <product num="2102">
            <name>MSG</name>
            <category>Wellness</category>
            <available content="YES"/>
        </product>
        <product num="2101">
            <name>YSD</name>
            <category>Music</category>
            <available content="NO"/>
        </product>
        <product num="2100">
            <name>RCS</name>
            <category>Media</category>
            <available content="YES"/>
        </product>
    </headercontent>
<product num="2103"/><name>AGB</name><category>Movies</category><available content="YES"/></maincontent>

Any advice?

  • 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-05T20:28:30+00:00Added an answer on June 5, 2026 at 8:28 pm

    This will work.

    <?php
    $xmldoc = new DomDocument( '1.0' );
    $xmldoc->preserveWhiteSpace = false;
    $xmldoc->formatOutput = true;
    
    $productNum = "2103";
    $name = "AGB";
    $category = "Movies";
    $content = "YES";
    
    if( $xml = file_get_contents( 'main.xml') ) {
        $xmldoc->loadXML( $xml, LIBXML_NOBLANKS );
    
        // find the headercontent tag
        $root = $xmldoc->getElementsByTagName('headercontent')->item(0);
    
        // create the <product> tag
        $product = $xmldoc->createElement('product');
        $numAttribute = $xmldoc->createAttribute("num");
        $numAttribute->value = $productNum;
        $product->appendChild($numAttribute);
    
        // add the product tag before the first element in the <headercontent> tag
        $root->insertBefore( $product, $root->firstChild );
    
        // create other elements and add it to the <product> tag.
        $nameElement = $xmldoc->createElement('name');
        $product->appendChild($nameElement);
        $nameText = $xmldoc->createTextNode($name);
        $nameElement->appendChild($nameText);
    
        $categoryElement = $xmldoc->createElement('category');
        $product->appendChild($categoryElement);
        $categoryText = $xmldoc->createTextNode($category);
        $categoryElement->appendChild($categoryText);
    
        $availableElement = $xmldoc->createElement('available');
        $product->appendChild($availableElement);
        $availableAttribute = $xmldoc->createAttribute("content");
        $availableAttribute->value = $content;
        $availableElement->appendChild($availableAttribute);
    
        $xmldoc->save('main.xml');
    }
    ?>
    

    Hope this helps.

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

Sidebar

Related Questions

This may sound like a very generic question but here it goes. I have
I think this may sound pretty simple, but still I can not get it
This may sound silly but I didn't want to hijack a question. I recently
This may sound trivial, but I'm pretty sure this question hasn't been asked, or
This may sound like a stupid question but I can't seem to find an
This may sound like a stupid question, perhaps it is. But I'm only trying
This may sound like a bit of a rhetorical question, but I ask it
This may sound like an unusual question, but I am curious as to how
This may sound stupid but... When I create big SQL commands I want to
This may sound like a stupid question but I'm a beginner not just to

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.