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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T13:20:06+00:00 2026-05-23T13:20:06+00:00

I figured out how to create a DOM object for XML with the following

  • 0

I figured out how to create a DOM object for XML with the following code (part of it):

$xml_item = $xml->createElement('item');
$xml_location = $xml->createElement('location');

$xml_item->setAttribute('item-id', 'abcd');
$xml_item->appendChild($xml_location);
$xml_location->setAttribute('location-id', '1234');
$xml_location->appendChild($xml_quantity);
$xml_quantity = $xml->createElement('quantity', '0');

Gives:

<item item-id="abcd">       
    <location location-id="1234">
        <quantity>0</quantity>             
    </location>
</item>

I want to keep adding more item elements of different attributes to obtain something like this:

<item item-id="abcd">       
    <location location-id="1234">
        <quantity>99</quantity>             
    </location>
</item>
<item item-id="qwer">       
    <location location-id="1234">
        <quantity>55</quantity>             
    </location>
</item>

But I’m having a hard time figuring this out. How do I use the same variable $xml_item to create multiple entries of “item” element with different attribute as above (i.e. abcd and qwer)? It seems to just over write the first one when I do another $xml_item->setAttribute(‘item-id’, ‘qwer’) after creating “abcd.”

Am I supposed to create multiple copies of “$xml_item” with different variable names (e.g. $xml_item1, _item2, etc. but this seems unreasonably tedious) or can I somehow reuse the same variable ($xml_item) to create multiple entries? The idea is to create as many of those “item” element as I need with different attributes.

  • 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-23T13:20:06+00:00Added an answer on May 23, 2026 at 1:20 pm

    From the php.net page for createElement,

    This node will not show up in the
    document unless it is inserted with
    (e.g.) DOMNode->appendChild().

    So just make sure to keep appending $xml_item to your DomDocument object between createElement() calls.
    ie) $xml->appendChild($xml_item);

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

Sidebar

Related Questions

I have the following regular expression : I figured out most of the part
$xml = new DOMDocument(); $xml_store_inventory = $xml->createElement('store-inventory'); // highest layer $xml_item = $xml->createElement('item'); $xml_quantity
I've figured out how to set VC++ to compile code into a .lib file
Is it possible to use PHP's SimpleXML functions to create an XML object from
I've been googling for a while now, and can't figure out how to create
I figured out through trial n error how to link an unmanaged static library
So I figured out that by adding the ResetWebServer=FALSE attribute to the solution manifest
I did some tests a while ago and never figured out how to make
I want to do this, but haven't figured it quite out yet... $(document).ready(function() {
(I had already written this question out when I figured out the answer, so

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.