Help on Adding child element to XML with nameSpaces using simpleXML

Hello,

This is what I have:


<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:name>asda33sd.pt</domain:name>
                    <domain:period unit="y">1</domain:period>
                    <domain:registrant>F-AA-140384-FCCN</domain:registrant>

                    <domain:contact type="tech">JAAD-912064-FCCN</domain:contact>
                    <domain:authInfo> 
                        <domain:pw/>
                    </domain:authInfo>
                </domain:create>
            <ns><hostAttr><hostName/><hostAddr ip="v4"/></hostAttr></ns></create>
            <extension>
                <ptdomain:create xmlns:ptdomain="http://eppdev.dns.pt/schemas/ptdomain-1.0" xsi:schemaLocation="http://eppdev.dns.pt/schemas/ptdomain-1.0 ptdomain-1.0.xsd">
                <ptdomain:legitimacy type="1"/><ptdomain:registration_basis type="6A"/><ptdomain:autoRenew>false</ptdomain:autoRenew></ptdomain:create>

            </extension>
            <clTRID>criacao-dominio-4c59446420c90</clTRID>
        </command>
    </epp>

This is what I should have:

<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:name>asda33sd.pt</domain:name>
                    <domain:period unit="y">1</domain:period>
                    <domain:ns>
	               <domain:hostAttr> 
		          <domain:hostName>ns3.dns.pt</domain:hostName>
		          <domain:hostAddr ip="v4">193.136.192.6</domain:hostAddr>
	</domain:hostAttr>
        </domain:ns>
                    <domain:registrant>F-AA-140384-FCCN</domain:registrant>

                    <domain:contact type="tech">JAAD-912064-FCCN</domain:contact>
                    <domain:authInfo> 
                        <domain:pw/>
                    </domain:authInfo>
                </domain:create>
            <ns><hostAttr><hostName/><hostAddr ip="v4"/></hostAttr></ns></create>
            <extension>
                <ptdomain:create xmlns:ptdomain="http://eppdev.dns.pt/schemas/ptdomain-1.0" xsi:schemaLocation="http://eppdev.dns.pt/schemas/ptdomain-1.0 ptdomain-1.0.xsd">
                <ptdomain:legitimacy type="1"/><ptdomain:registration_basis type="6A"/><ptdomain:autoRenew>false</ptdomain:autoRenew></ptdomain:create>

            </extension>
            <clTRID>criacao-dominio-4c59446420c90</clTRID>
        </command>
    </epp>

So, this:


<domain:ns>
	               <domain:hostAttr> 
		          <domain:hostName>ns3.dns.pt</domain:hostName>
		          <domain:hostAddr ip="v4">193.136.192.6</domain:hostAddr>
	</domain:hostAttr>
        </domain:ns>

Should be placed after the


<domain:period unit="y">1</domain:period>

I’ve tried to place it like this:


//load the xml "content-empty" file:
$xmlObj = simplexml_load_file('RepositorioXml/CreateDomain.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->addAttribute('ip', 'v4');

           $hostName = $dominioVo->getNsNome();
           $hostAddr = $dominioVo->getNsEndereco();

          //... other xml elements filled...

          $xmlString = $xmlObj->asXML();

When I do it, I do get:

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

Can I please, once again, (I’m feeling bad already) request your help please?

Regards,
Marcio

ps- any of you know how to highlight a specific line or lines, on a code snipped, here on sitepoint forums?

AHHH!!! :injured::injured::injured::injured::injured::injured::injured:

Found it. As anyone else can see as well. Those uris are different. :s

Ok… I have found the switch, but I haven’t seen anything else… still:

This works:


$nsNode = $xmlObj->command->create->children('urn:ietf:params:xml:ns:domain-1.0')->create->addChild('domain:ns');

This doesn’t, and throws me the warning:


$nsNode = $xmlObj->command->create->children(self::OBJ_URI_DOMAIN)->create->addChild('domain:ns');

I have this up on my class:

const OBJ_URI_DOMAIN = 'urn:ietf:params:xml:ns:epp:domain-1.0';

What could lead to such a behaviour?

Thanks a lot,
Márcio

Oh yeah? oh yeah?!! Then!!!
I will do it. :slight_smile:

I’m so stuck here. If it was you, what will you do to debug it?

What would I do?

Distill the problem down to the smallest use-case I can make it. If in doing that I zoom in on the real problem/gap in knowldge then fine - I learn and I fix it.

If not I have something really short to paste into SP so that others can very quickly grasp my meaning.

My bad: the first line is wrong, it gives the warning and it’s legit.

The line I was struggling is different from the one posted:

$nsNode = $xmlObj->command->create->children(self::OBJ_URI_DOMAIN)->create->addChild('domain:ns');

Here, we are not accessing the children list and try to add to that list, a child. Here, so I believe, we are trying to add a child to a given child retrieved by that list.

Sorry again. My bad.

Ok… simplifying:

By having 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>

We need to add new child nodes into <domain:create>, precisely those:


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

How can we do that by using simpleXML ?

This haven’t work with me:


$nsNode = $xmlObj->command->create->children(self::OBJ_URI_DOMAIN)->addChild('domain:ns');

$hostAttr = $nsNode->addChild('domain:hostAttr');
$hostName = $hostAttr->addChild('domain:hostName');
$hostAddr = $hostAttr->addChild('domain:hostAddr');
$hostAddr->addAtribute('ip', 'v4');

This first line on the code above, gives the following warning:

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

after this, and because of this, on the second line, we get a fatal error:

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

Hope it’s more clear now.

k. regards,
Marcio

See, I would never have spotted that.

Perhaps if you’d have kept that url as a var then it would only be kept in one place and, oh, I should not repeat myself.

Glad you got it :slight_smile:

Better. I have it on a constant. :slight_smile: But for some reason, all other commands worked, but this doesn’t. So, I end up changing the URI for all others, this one included, and all worked. :smiley:

I’m so stuck here. If it was you, what will you do to debug it? :s

(please don’t start like “Unit testing” or something because I’m so newbie that I can’t even understand what it is… :s ) but anything else, even if archaic. I would be glad.

:sick:

Thanks cups. :smiley: