简体   繁体   English

如何将PHP数组转换为DOM XML nodeList?

[英]How to convert PHP array to DOM XML nodeList?

I'm looking for a DOM XML function to convert PHP array like this: 我正在寻找一个DOM XML函数来转换PHP数组,如下所示:

<?php
$errors = array("A", "B", "C", "D");
?>

to DOM XML NodeList 到DOM XML NodeList

<?xml version="1.0" standalone="no"?>
<error>
    <missing>A</missing>
    <missing>B</missing>
    <missing>C</missing>
    <missing>D</missing>
</error>

Thank you very much for your help :) 非常感谢您的帮助 :)

I tried the following code: 我尝试了以下代码:

<?php
$basedoc = new DomDocument();
$basedoc->Load("Standard.svg"); //Fichier SVG de base
$baseroot = $basedoc->documentElement; //On prend l'élément racine
$errorgroup = $basedoc->createElement('error'); //On crée le groupe de base
foreach($erreurs as $erreur) {
    $missinggroup = $errorgroup->createElement('missing'); //On crée le groupe de base
    $errorgroup->appendChild($missinggroup);
}
$baseroot->appendChild($errorgroup);
?>

I think this is too simple structure to use DomXML functions. 我认为这太简单了,无法使用DomXML函数。 I think you should create simple view template for this XML - it'll look something like that: 我认为您应该为此XML创建简单的视图模板-它看起来像这样:

<?xml version="1.0" standalone="no"?>
<error>
    <?php foreach ($errors as $error):?>
        <missing><?php echo $error;?></missing>
    <?php endforeach;?>
</error>

The exact template structure of course depends on your framework - in zend it'll be $this->errors instead of $errors for example. 确切的模板结构当然取决于您的框架-在zend中,它将是$ this-> errors而不是$ errors。

Od use the SimpleXML as sugested by @bassem-ala in first link. Od在第一个链接中使用@ bassem-ala表示的SimpleXML。

UPDATE 更新

Here's the 'recursive' function to generate XML based on table. 这是基于表生成XML的“递归”函数。 Something like that 像这样

function generateXMLElement($elements, $rootNode = null, $rootNodeName = 'xml')
{
    if (!$rootNode)
    {
        $rootNode = new SimpleXMLElement('<'.$rootNodeName.'/>');
    }
    foreach ($elements as $key => $val)
    {
        if (is_array($val))
        {
            $childElem = $rootNode->addChild($key);
            generateXMLElement($val, $childElem);
        }
        else
        {
            $childElem = $rootNode->addChild($key, $val);
        }
    }
    return $rootNode;
}

You get the XML using $xml = generateXMLElement($errors, null, 'error'); 您可以使用$xml = generateXMLElement($errors, null, 'error');获得XML $xml = generateXMLElement($errors, null, 'error'); then you can print it using print($xml->asXML()); 然后可以使用print($xml->asXML());进行print($xml->asXML());

You are not not using $erreur anywhere in the creation code so obviously it wont be in the resulting XML. 您不会在创建代码中的任何地方使用$erreur ,因此显然不会在生成的XML中使用$erreur Also, you cannot create an Element from a DOMElement but must create it from the DOMDocument . 同样,您不能从DOMElement创建Element,而必须从DOMDocument创建它。 Your code will give a Fatal Error. 您的代码将出现致命错误。

Change 更改

$missinggroup = $errorgroup->createElement('missing');

to

$missinggroup = $basedoc->createElement('missing', $erreur);

and then it will work: http://codepad.org/GmCIBIQW 然后它将起作用: http : //codepad.org/GmCIBIQW

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM