SimpleXML-使用先前声明的名称空间添加新节点-如何? [英] SimpleXML - add a new node using a namespace previously declared - how?

查看:71
本文介绍了SimpleXML-使用先前声明的名称空间添加新节点-如何?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在<domain:create>节点的非常特定的位置添加一个孩子(因此,我也使用DOM不仅是simpleXML).

I would like to add a child, on a very specific place (so I'm also using DOM and not only simpleXML) for <domain:create> node.

我试图在simpleXML构造上使用$ ns属性.

I have tried to use the $ns attribute on simpleXML construct.

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

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

我尝试在创建元素后注册前缀, 但是在此之后我不使用xpath,所以这是一个毫无用处的尝试...

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

由于xml是从文件加载的,并且是ns声明的那个文件,也许我们应该从该文件中获取它?

Since the xml is loaded from a file, and that file as this ns declared, maybe we should grab it from that file?

这里是以上内容的全部,以便我们可以更好地理解上下文: 我们正在加载一个包含总体结构的XML文件:

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

我们将获取一个用作目标的元素:

They we will grab an element that we will use as a target:

//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文件的其余部分.

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

请咨询, MEM

推荐答案

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

这篇关于SimpleXML-使用先前声明的名称空间添加新节点-如何?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆