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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T17:03:34+00:00 2026-05-26T17:03:34+00:00

//create a simple xml object $xml = new SimpleXMLElement(‘some valid xml’); //add a my_elements

  • 0
//create a simple xml object
$xml = new SimpleXMLElement('some valid xml');

//add a my_elements node
$xml->addChild('my_elements');

//an array of elements that will be added to to my_elments
$my_elements = array('element_1', 'element_2', 'element_3');

foreach($my_elements as $element){
   //my_element is only added once as shown below
   $xml->my_elements->addChild('my_element');

   $xml->my_elements->my_element->addChild('something', $element);
}

What I would like this to look like is something like this(shown in xml for ease of reading)

<xml>
   <my_elements>
      <my_element>
          <something>element_1</something>
      </my_element>
      <my_element>
          <something>element_2</something>
      </my_element>
      <my_element>
          <something>element_3</something>
      </my_element>
   </my_elements>
</xml>

But what I am getting is something more like this

<xml>
   <my_elements>
      <my_element>
         <something>element_1</something>
         <something>element_2</something>
         <something>element_3</something>
      </my_element>
   </my_elements>
</xml>

Is it possible to make a separate child node when using addChild with the same name?

  • 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-26T17:03:35+00:00Added an answer on May 26, 2026 at 5:03 pm

    Yes, you only need to add the childs to the new added element, not to the existing one. See http://php.net/manual/en/simplexmlelement.addchild.php :

    foreach($my_elements as $element){
       //my_element is only added once as shown below
       $new_my_element = $xml->my_elements->addChild('my_element');
       $new_my_element->addChild('something', $element);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Heres a simple php script that generates some XML: <?php // create doctype $dom
I know how to create simple object and add dynamically properties to it. object
Is it possible to use PHP's SimpleXML functions to create an XML object from
Is there a pattern, Xml structure, architecture technique we can use to create simple
I'm creating an XML file using Perl and XML::Simple module. I successfully create the
I need to create simple reusable javascript object publishing several methods and parameterized constructor.
I would like to create simple objects at runtime (textbox, label, etc) and add
I have an XML document and want to insert a new node at a
Note: I'm using ConvertTo-XML and cannot use Export-Clixml : I create a simple PSObjec
I am trying to deserialize an object from XML in Ruby. Something simple like:

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.