SimpleXML - nasıl - önceden bildirilmiş bir ad kullanarak yeni bir düğüm eklemek?

0 Cevap php
<epp>
    <domain:create xmlns:domain="urn:someurn">
       <domain:name></domain:name>
       <domain:registrant></domain:registrant>
       <domain:contact></domain:contact>
    </domain:create>
</epp>

Ben çok özel bir yerde, bir çocuk eklemek istiyorum (yani ben de ve sadece SimpleXML DOM kullanıyorum) <domain:create> düğüm için.

Ben $ ns SimpleXML yapı özniteliğini kullanmayı denediler.

$nsNode = new SimpleXMLElement('<domain:ns>', $options = 0, $ns='urn:ietf:params:xml:ns:domain-1.0');

//transform the target into dom object for manipulation
$nodeRegistrantDom = dom_import_simplexml($nodeRegistrant);

Ama ben alıyorum:

I/O warning : failed to load external entity "<domain:ns>"

I've tried to register the prefix after creating the element, but I use no xpath after this, so this was quite a useless try...

//creates the simpleXML object node to be inserted.
$nsNode = new SimpleXMLElement('<ns/>');

//this will not work, because we will not use xpath after it :s
$nsNode->registerXPathNamespace('domain', 'urn:ietf:params:xml:ns:domain-1.0');

Bu ns olarak ilan xml bir dosyadan yüklenen ve bu dosya olduğundan, belki o dosyanın onu kapmak gerekir?

Here is an overall of the above, so that we can better understand the context: We are loading a XML file that contains an overall structure:

 $xmlObj = simplexml_load_file('EppCreateDomain.xml');

Onlar biz bir hedef olarak kullanacağı bir öğe çekecek:

//grab the target.
    $nodeRegistrant = $xmlObj->command->create->children(self::OBJ_URI_DOMAIN)->create->registrant;

    //transform the target into a dom object for later manipulation
    $nodeRegistrantDom = dom_import_simplexml($nodeRegistrant);

//we try to use simpleXML to create the node that we want to add after our target.
    $nsNode = new SimpleXMLElement('<domain:ns>');


//grabs the node and all his children (none in this case), by importing the node we want to add,
//into the root object element that contains the <domain:registrant> node.
$nsNodeDom = $nodeRegistrantDom->ownerDocument->importNode(dom_import_simplexml($nsNode), true);

$nodeRegistrantDom->parentNode->insertBefore($nsNodeDom, $nodeRegistrantDom->nextSibling);

$simpleXmlNsNode = simplexml_import_dom($nsNodeDom);

Now we have our node placed on a proper place. And converted to simpleXML so, we can now easily add some children and fill the rest of the xml file..

$hostAttr = $simpleXmlNsNode->addChild('domain:hostAttr');
$hostName = $hostAttr->addChild('domain:hostName');

Please advice, MEM

0 Cevap