我想在一个非常具体的地方添加一个孩子(所以我也使用 DOM 而不仅仅是 simpleXML)<domain:create>
node.
我尝试在 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');
请指教,
模模