【问题标题】:php xml_encode set attribute to specific tag namephp xml_encode 将属性设置为特定的标签名称
【发布时间】:2016-03-26 05:59:18
【问题描述】:

我使用此代码对我的数组进行 xml_encode。

public function xml_encode($mixed, $domElement=null, $DOMDocument=null) {
    if (is_null($DOMDocument)) {
        $DOMDocument =new DOMDocument;
        $DOMDocument->formatOutput = true;
        $this->xml_encode($mixed, $DOMDocument, $DOMDocument);
        echo $DOMDocument->saveXML();
    }
    else {
        // To cope with embedded objects 
        if (is_object($mixed)) {
          $mixed = get_object_vars($mixed);
        }
        if (is_array($mixed)) {
            foreach ($mixed as $index => $mixedElement) {
                if (is_int($index)) {
                    if ($index === 0) {
                        $node = $domElement;
                    }
                    else {
                        $node = $DOMDocument->createElement($domElement->tagName);
                        $domElement->parentNode->appendChild($node);
                    }
                }
                else {
                    $plural = $DOMDocument->createElement($index);
                    $domElement->appendChild($plural);
                    $node = $plural;
                    if (!(rtrim($index, 's') === $index)) {
                        $singular = $DOMDocument->createElement(rtrim($index, 's'));
                        $plural->appendChild($singular);
                        $node = $singular;
                    }
                }

                $this->xml_encode($mixedElement, $node, $DOMDocument);
            }
        }
        else {
            $mixed = is_bool($mixed) ? ($mixed ? 'true' : 'false') : $mixed;
            $domElement->appendChild($DOMDocument->createTextNode($mixed));
        }
    }
}

https://www.darklaunch.com/2009/05/23/php-xml-encode-using-domdocument-convert-array-to-xml-json-encode

而且效果很好。现在,我想为特定标签设置一个属性。

例如,假设这是我的 xml。

<book>
     <title>PHP Programming</title>
     <description>Learn how to code in PHP</description>
</book>

我想为 title 标签设置一个属性并使其成为

<title name=attributeValue>
       PHP Programming
</title>

我怎样才能做到这一点?

谢谢!

【问题讨论】:

    标签: php arrays json xml domdocument


    【解决方案1】:

    只是和大家分享一下,如果其他人也有这样的问题。 我刚刚解决了我的问题。 在这部分

    if (is_null($DOMDocument)) {
            $DOMDocument =new DOMDocument;
            $DOMDocument->formatOutput = true;
            $this->xml_encode($mixed, $DOMDocument, $DOMDocument);
            //echo $DOMDocument->saveXML();
            return $DOMDocument;
        }
    

    我返回了 $DOMDocument。然后我就这样做了

    $xml = $this->xml_encode($data);
         $xmlstring = $xml->saveXML();
            $dom = new DOMDocument();
            $dom->loadXML($xmlstring);
    
            foreach ($dom->getElementsByTagName('title') as $item) {
                $item->setAttribute('name', 'attributeValue');
                echo $dom->saveXML();
            }
    

    我得到了正确的结果。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多