php - 通过php在xml文件中添加节点

标签 php xml

我有一个 php 数组,我需要将其转换为 xml 文件,但我无法获得我想要的格式。

数组:

Array
(
    [1] => Array
        (
            [2000307] => Array
                (
                    [eventid] => 2000307
                    [eveseq] => 100
                    [fee] => 200
                )

            [2000310] => Array
                (
                    [eventid] => 2000310
                    [eveseq] =>101
                    [fee] => 300
                )

        )

)

将数组转换为 xml:

$xml = new SimpleXMLElement('<Event/>');
event_to_xm($array, $xml);

function event_to_xml($array, &$xml) {
        $array = json_decode($array,TRUE);
        foreach($array as $key => $value) {
            foreach($value as $id => $index) {
                foreach($index as $title => $result) {
                    if(is_array($result)) {
                        if(!is_numeric($title)){
                            $subnode = $xml->addChild($title);
                            array_to_xml($result, $xml);
                        } else {
                            array_to_xml($result, $xml);
                        }
                    } else {
                        $xml->addChild($title,  htmlspecialchars($result));
                    }
                }
            }
        }
    }

event.xml:

<?xml version="1.0"?>
<Event>
  <eventid>2000307</eventid>
  <eveseq>100</eveseq>
  <fee>zz</fee>
  <eventid>2000310</eventid>
  <eveseq>101</eveseq>
  <fee>0</fee>
</Event>

我期望是,当新数组开始时它将创建一个 cd 标签:

xml:

<?xml version="1.0"?>
<Event>
 <cd>
  <eventid>2000307</eventid>
  <eveseq>100</eveseq>
  <fee>200</fee>
</cd>
<cd>
  <eventid>2000310</eventid>
  <eveseq>101</eveseq>
  <fee>300</fee>
</cd>
</Event>

我尝试过的: 我尝试直接添加属性,但遇到此错误 Call to a member function addChild() on null

$xml=new SimpleXMLElement("event.xml", 0, TRUE);
$child = $xml->event[0]->addChild("cd");

最佳答案

我会采取稍微不同的方法 - 首先,使用 DOMDocument 而不是 SimpleXML,其次,使用 xpath 和片段将元素插入到文档中。像这样的事情:

$events = array(
    "2000307" => array(
        "eventid" => 2000307,
        "eveseq" => 100,
        "fee" => 200,
    ),
    "2000310" => array(         
        "eventid" => 20003010,
        "eveseq" => 101,
        "fee" => 300,
    )
);
#create a blank document with a root element
$xml = <<<'XML'
<?xml version="1.0"?>
<Event></Event>
XML;

$document = new DOMDocument();
$document->loadXML($xml);
$xpath = new DOMXpath($document);

$expr = "//Event";
#indicate where to insert the new elements
$dest = $xpath->query($expr);

foreach($events as $event) {
   $fragment = $document->createDocumentFragment();
   #this is the new element to be inserted
   $instance = "  <cd>
    <eventid>{$event['eventid']}</eventid>
    <eveseq>{$event['eveseq']}</eveseq>
    <fee>{$event['fee']}</fee>
  </cd>\n";
  $fragment->appendXML($instance);
  $dest[0]->appendChild($fragment);
}

$document->formatOutput = TRUE;
echo $document->saveXML();

输出应该是您期望的输出。

关于php - 通过php在xml文件中添加节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74696711/

相关文章:

java - JAX-B : dynamically generate element name from XMLAttribute

xml - 如何使用Xsl向Xml文件中的每个节点添加特定属性?

.net - XML xpath Visual Studio

php - Heroku PHP 应用程序崩溃 bash : vendor/bin/heroku-php-apache2: No such file or directory

php - 简而言之,如果这在 PHP 中不是这样?

php - 从类打印或从函数返回输出和打印之间有什么区别

javascript - 从 PHP 更新 mysql 中的最后一行

java - 如何使用 JAXP DocumentBuilder 提供自定义错误消息?

xml - 使用 xslt 将多个 xml 元素的值连接成单个元素

PHP 解析错误 : syntax error, 意外的 T_STRING,期待 T_VARIABLE