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

myzjeezk  于 2022-12-10  发布在  PHP
关注(0)|答案(3)|浏览(123)

我有一个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));
                    }
                }
            }
        }
    }

事件.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");
nimxete2

nimxete21#

我将采用一种稍微不同的方法-首先,使用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();

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

gstyhher

gstyhher2#

您可以迭代数组数据并使用新的'cd'子级构造新的simplexml对象。尝试如下:

$data = array (
  "2000307" => array(
     "eventid" => 2000307,
     "eveseq" => 100,
     "fee" => 200,
   ),
  "2000310" => array(
     "eventid" => 20003010,
     "eveseq" => 101,
     "fee" => 300,
   )
);

function to_xml(SimpleXMLElement $object, array $data)
{
    foreach ($data as $key => $value) {
        if (is_array($value)) {
            $new_object = $object->addChild('cd');
            to_xml($new_object, $value);
        } else {
            $object->addChild($key, $value);
        } 
     }
}

$xml = new SimpleXMLElement('<event/>');
to_xml($xml, $data);
print $xml->asXML();
zzoitvuj

zzoitvuj3#

我找到了一种不用迭代数组数据方法

$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) {
                if(is_array($value)){
                    $neweve = $xml->addChild('cd'); 
                }
                foreach($index as $title => $result) {
                    if(is_array($result)) {
                        $subnode = $xml->addChild($title);
                        event_to_xml($subnode, $xml);
                    } else {
                        $neweve->addChild($title,  htmlspecialchars($result));
                    }
                }
            }
        }
    }

相关问题