SimpleXML kullanarak ekleme çocuk düğümler

0 Cevap php

Ben bir XML belgesine sahip ve SimpleXML kullanarak belirli bir noktada yeni bir düğüm eklemek istiyorum.

Orijinal XML şudur:

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

sonra <domain:create> Ben şu düğümü eklemek gerekir:

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

Bunu nasıl yapabilirim? Ben denedim:

$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');

Bu ilk satırda, ben bu uyarıyı alıyorum:

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

İkinci satırda, ve bu nedenle, ben alıyorum:

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

Şimdiden teşekkürler.

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

0 Cevap