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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T10:51:40+00:00 2026-06-06T10:51:40+00:00

This is my xml file. I want to add a new element to all

  • 0

This is my xml file. I want to add a new element to all books called ‘price’. I don’t know how to loop through all the books and append a child in them in PHP.
Please help.

<book>
<title>title 1</title>
<author>author 1</author>
</book>

<book>
<title>title 2</title>
<author>author 2</author>
</book>

Expected output:

<book>
<title>title 1</title>
<author>author 1</author>
<price>20</price>
</book>

<book>
<title>title 2</title>
<author>author 2</author>
<price>20</price>
</book>

This is what I have written so far:

            $dom = new DomDocument;

    $dom->preserveWhiteSpace = FALSE;
    $dom->loadXML($xmlFile);
    $books = $dom->getElementsByTagName('book'); // Find Books

    foreach ($books as $book) //go to each book 1 by 1
    {
        $price = $dom->createElement('price');
        $price  = $dom->appendChild($price );

        $text = $dom->createTextNode('20');
        $text = $dom->appendChild($text);
    }
     $dom->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-06T10:51:43+00:00Added an answer on June 6, 2026 at 10:51 am

    The PHP library for DOM manipulation is object oriented not functional/ procedural.

    You don’t generate a DOMDocument then use it everywhere to call every method, each node has it’s own methods. You use the DOMDocument to creat new elements but after that you use the element’s methods to append an element to another element.

    Try to look at the following classes DOMDocument, DOMElement, DOMAttr and DOMText, and their common ancestor DOMNode from the PHP docs. That should clear some things up about which kind of object you’re dealing with and what methods you can use with it.

    $xmlFile = '/path/to/filename.xml';
    $xmlString = file_get_contents($xmlFile);
    
    $dom = new DomDocument;
    $dom->preserveWhiteSpace = FALSE;
    $dom->loadXML($xmlString);
    $books = $dom->getElementsByTagName('book'); // Find Books
    
    foreach ($books as $book) //go to each book 1 by 1
    {
        $price = $dom->createElement('price');
        $book->appendChild($price);
    
        $text = $dom->createTextNode('20');
        $price->appendChild($text);
    }
    $dom->saveXML();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im new to this.I have a cron file called xml.php and i want to
this is my XML file <atm> i want to know how can i verify
This is my Xml file.i want to transform this xml file into another customized
I have thread where downloading xml file a this xml file i want to
Hi I can't understand this error I want read this XML file: <?xml version=1.0
I want to parse a xml file using xquery in Ruby, I found this
I want to read an xml file placed in assets/ directory. I've tried this
I have a on memory Datatable like this. I want to produce xml file
I Want to add the data to the xml file from my asp.net GUI.So
I want to parse the xml file to get the root element. Then append

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.