SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    SitePoint Wizard
    Join Date
    Feb 2009
    Posts
    1,006
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Thumbs down Help on Adding child element to XML with nameSpaces using simpleXML

    Hello,

    This is what I have:
    Code XML:
    <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:
    Code XML:
    <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:
    Code XML:
    <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
    Code XML:
    <domain:period unit="y">1</domain:period>

    I've tried to place it like this:
    PHP Code:
    //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?

  2. #2
    SitePoint Wizard
    Join Date
    Feb 2009
    Posts
    1,006
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.


  3. #3
    SitePoint Wizard
    Join Date
    Feb 2009
    Posts
    1,006
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok... simplifying:

    By having this:
    Code XML:
    <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:

    Code XML:
    <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:
    PHP Code:
    $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

  4. #4
    SitePoint Wizard
    Join Date
    Feb 2009
    Posts
    1,006
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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:
    PHP Code:
    $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.

  5. #5
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    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.

  6. #6
    SitePoint Wizard
    Join Date
    Feb 2009
    Posts
    1,006
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Cups View Post
    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.
    Oh yeah? oh yeah?!! Then!!!!
    I will do it.

  7. #7
    SitePoint Wizard
    Join Date
    Feb 2009
    Posts
    1,006
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok... I have found the switch, but I haven't seen anything else... still:

    This works:
    PHP Code:
    $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:
    PHP Code:
    $nsNode $xmlObj->command->create->children(self::OBJ_URI_DOMAIN)->create->addChild('domain:ns'); 
    I have this up on my class:
    PHP Code:
    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

  8. #8
    SitePoint Wizard
    Join Date
    Feb 2009
    Posts
    1,006
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    AHHH!!!!!!!!!!!!

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

  9. #9
    SitePoint Wizard
    Join Date
    Feb 2009
    Posts
    1,006
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks cups.

  10. #10
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    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

  11. #11
    SitePoint Wizard
    Join Date
    Feb 2009
    Posts
    1,006
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Cups View Post
    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.
    Better. I have it on a constant. 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.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •