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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T03:58:40+00:00 2026-05-16T03:58:40+00:00

I have an XML document and want to insert a new node at a

  • 0

I have an XML document and want to insert a new node at a specific spot using SimpleXML.

The original XML is this:

<epp 
  xmlns="urn:ietf:params:xml:ns:epp-1.0" 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="urn:ietf:params:xml:ns:epp-1.0 epp-1.0.xsd"
>
  <command>
    <create>
      <domain:create 
        xmlns:domain="urn:ietf:params:xml:ns:domain-1.0"
        xsi:schemaLocation="urn:ietf:params:xml:ns:domain-1.0 domain-1.0.xsd"
      >
        <domain:period unit="y"></domain:period>
      </domain:create>
    </create>
  </command>
</epp>

after <domain:create> I need to add the following node:

<domain:ns>
  <domain:hostAttr>
    <domain:hostName></domain:hostName>
    <domain:hostAddr ip="v4"></domain:hostAddr>
  </domain:hostAttr>
</domain:ns>

How can I do that? I have tried this:

$xmlObj = simplexml_load_file('myXMLFile.xml');
$nsNode = $xmlObj->command->create->children(self::OBJ_URI_DOMAIN)->create->addChild('domain:ns');
$hostAttr = $nsNode->addChild('domain:hostAttr');
$hostName = $hostAttr->addChild('domain:hostName');
$hostAddr = $hostAttr->addChild('domain:hostAddr');
$hostAddr->addAtribute('ip', 'v4');

On this first line, I’m getting this warning:

Warning: SimpleXMLElement::addChild()
[simplexmlelement.addchild]: Cannot
add child. Parent is not a permanent
member of the XML tree

On the second line, and because of this, I’m getting:

Fatal error: Call to a member function
addChild() on a non-object

Thanks in advance.

Additional notes:
– The php version is higher then 5.1;
– I have successfully added child nodes later on this same XML.

  • 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-16T03:58:41+00:00Added an answer on May 16, 2026 at 3:58 am

    I cannot reproduce the first error

    <?php
    echo phpversion(), "\n";
    // $xmlObj = simplexml_load_file('myXMLFile.xml');
    $xmlObj = getDoc();
    
    $nsNode = $xmlObj->command->create->children('urn:ietf:params:xml:ns:domain-1.0')->create->addChild('domain:ns');
    $nsNode->addChild('foo', 'Mary had a little lamb...');
    echo $xmlObj->asxml();
    
    function getDoc() {
      return new SimpleXMLElement('<epp 
        xmlns="urn:ietf:params:xml:ns:epp-1.0" 
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="urn:ietf:params:xml:ns:epp-1.0 epp-1.0.xsd"
      >
        <command>
          <create>
            <domain:create 
              xmlns:domain="urn:ietf:params:xml:ns:domain-1.0"
              xsi:schemaLocation="urn:ietf:params:xml:ns:domain-1.0 domain-1.0.xsd"
            >
              <domain:period unit="y"></domain:period>
            </domain:create>
          </create>
        </command>
      </epp>');
    }
    

    prints

    5.3.2
    <?xml version="1.0"?>
    <epp xmlns="urn:ietf:params:xml:ns:epp-1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:ietf:params:xml:ns:epp-1.0 epp-1.0.xsd">
        <command>
          <create>
            <domain:create xmlns:domain="urn:ietf:params:xml:ns:domain-1.0" xsi:schemaLocation="urn:ietf:params:xml:ns:domain-1.0 domain-1.0.xsd">
              <domain:period unit="y"/>
            <domain:ns><domain:foo>Mary had a little lamb...</domain:foo></domain:ns></domain:create>
          </create>
        </command>
      </epp>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this XML document and I want to find an specific GitHubCommiter using
I have a DOM and I want to insert a new node using Hpricot.
I have an XML document that has been created using utf-8 encoding. I want
I have an XML document, and I want to print the tag names and
Within Symphony CMS, I want to be able to have an XML document which
I have a given XML document (structure can not be changed) and want to
I have an XML file and parse it into my PHP document. I want
I have an XML template document: <TriadMessage xmlns=http://www.myco.com/02/11/2008/V1/TriadMessage.xsd xmlns:triad=http://www.myco.com/02/11/2008/V1/TriadTypes.xsd xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance > <TriadRouteInfo> <triad:RoutingCorrelationId>new value
I have an XML document and I want to index it to a Lucene
I have one XML document which I want to store it inside ViewState 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.