我想在<domain:create>
节点的一个非常特定的地方添加一个孩子(所以我也使用DOM而不仅仅是simpleXML)。
我试图在simpleXML构造上使用$ ns属性。
$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);
但我得到了:
I / O警告:无法加载外部 实体
"<domain:ns>"
我在创建元素后尝试注册前缀, 但是在此之后我没有使用xpath,所以这是一次无用的尝试......
//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');
由于xml是从一个文件加载的,而且这个文件正如ns所声明的那样,也许我们应该从那个文件中获取它?
以上是上述内容,以便我们更好地了解上下文: 我们正在加载一个包含整体结构的XML文件:
$xmlObj = simplexml_load_file('EppCreateDomain.xml');
他们我们将抓住一个我们将用作目标的元素:
//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);
现在我们将节点放在合适的位置。 并转换为simpleXML,我们现在可以轻松添加一些子节点并填充其余的xml文件..
$hostAttr = $simpleXmlNsNode->addChild('domain:hostAttr');
$hostName = $hostAttr->addChild('domain:hostName');
请指教, MEM
答案 0 :(得分:2)
由于xml是从一个文件加载的,而且这个文件正如ns所声明的那样,也许我们应该从那个文件中获取它?
如果该文件是XML文件,是的,您应该加载整个文件,而不仅仅是一部分。
声明命名空间后,添加命名空间元素很简单:
<?php
$xml = <<<XML
<epp>
<domain:create xmlns:domain="urn:someurn" xmlns:ietf="urn:thaturn">
<domain:name></domain:name>
<domain:registrant></domain:registrant>
<domain:contact></domain:contact>
</domain:create>
</epp>
XML;
$sxml = new SimpleXMLElement($xml);
$sxml->children("domain", true)->create->addChild("newElem", "value", "urn:thaturn");
echo $sxml->saveXML();
给出
<?xml version="1.0"?>
<epp>
<domain:create xmlns:domain="urn:someurn" xmlns:ietf="urn:thaturn">
<domain:name/>
<domain:registrant/>
<domain:contact/>
<ietf:newElem>value</ietf:newElem></domain:create>
</epp>
答案 1 :(得分:0)
<?php
// test document, registrant as first/last element and somewhere in between
$xmlObj = new SimpleXMLElement('<epp>
<domain:create xmlns:domain="urn:someurn">
<domain:name></domain:name>
<domain:registrant></domain:registrant>
<domain:contact></domain:contact>
</domain:create>
<domain:create xmlns:domain="urn:someurn">
<domain:name></domain:name>
<domain:contact></domain:contact>
<domain:registrant></domain:registrant>
</domain:create>
<domain:create xmlns:domain="urn:someurn">
<domain:registrant></domain:registrant>
<domain:name></domain:name>
<domain:contact></domain:contact>
</domain:create>
</epp>');
foreach( $xmlObj->children("urn:someurn")->create as $create ) {
$registrant = $create->registrant;
insertAfter($registrant, 'domain:ns', 'some text');
}
echo $xmlObj->asXML();
function insertAfter(SimpleXMLElement $prevSibling, $qname, $val) {
$sd = dom_import_simplexml($prevSibling);
$newNode = $sd->ownerDocument->createElement($qname, $val);
$newNode = $sd->parentNode->insertBefore($newNode, $sd->nextSibling);
return simplexml_import_dom($newNode);
}
打印
<?xml version="1.0"?>
<epp>
<domain:create xmlns:domain="urn:someurn">
<domain:name/>
<domain:registrant/><domain:ns>some text</domain:ns>
<domain:contact/>
</domain:create>
<domain:create xmlns:domain="urn:someurn">
<domain:name/>
<domain:contact/>
<domain:registrant/><domain:ns>some text</domain:ns>
</domain:create>
<domain:create xmlns:domain="urn:someurn">
<domain:registrant/><domain:ns>some text</domain:ns>
<domain:name/>
<domain:contact/>
</domain:create>
</epp>