【问题标题】:Add node and atributes after root node to Array2XML将根节点后的节点和属性添加到 Array XML
【发布时间】:2017-04-25 18:27:48
【问题描述】:

我正在使用来自this link 的 Array2XML,效果很好!

但我需要在输出之前添加一些节点。我需要我的结构是这样的:

<clients>
   <client>             -->Need to add
      <id>myid</id>     -->Need to add
      <name>name</name> -->Need to add
      <items>           -->Need to add
         <item>
            <title>itemtitle</title>
            <date>itemdate</date>
         </item>
      </items>
    </client>
<clients>

但我能得到的只有:

<clients>
   <item>
      <title>itemtitle</title>
      <date>itemdate</date>
    </item>
 <clients>

ROOT NODE clients 和节点item 可以输出,但是如何在节点item 之前添加节点client 和属性idname 和子节点items

这是我想我需要进行更改的 php 函数,但没有成功:

public static function &createXML($node_name, $arr=array()) {

    $xml = self::getXMLRoot();
    $xml->appendChild(self::convert($node_name, $arr));     

    self::$xml = null;    // clear the xml node in the class for 2nd time use.
    return $xml;
}

我试过了,但是不行……

public static function &createXML($node_name, $arr=array()) {

    $xml = self::getXMLRoot();
    $clientname='client';
    $client = $xml->createElement($clientname);
    $xml->appendChild(self::convert($node_name, $arr));     

    self::$xml = null;    // clear the xml node in the class for 2nd time use.
    return $xml;
}

如何在项目循环之前添加此节点和属性?

非常感谢!

【问题讨论】:

    标签: php xml nodes


    【解决方案1】:

    好吧,我摸了摸头就知道了……

    我只需要对此进行编辑:

    public static function &createXML($node_name, $arr=array()) {
    
        $xml = self::getXMLRoot();
    
        $clients = $xml->createElement("clients");
        $xml->appendChild($clients);
    
        $client = $xml->createElement("client");
        $clients->appendChild($client);
    
        $id = $xml->createElement('id', 'myid');
        $client->appendChild($id);
        $name = $xml->createElement('name', 'myname');
        $client->appendChild($name);
    
        $client->appendChild(self::convert($node_name, $arr));      
    
        self::$xml = null;    // clear the xml node in the class for 2nd time use.
        return $xml;
    }
    

    【讨论】:

      猜你喜欢
      • 2012-09-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-04
      相关资源
      最近更新 更多