3

使用 PHP 在 XML 中添加新元素时如何设置属性。我的 PHP 代码是这样的:

 <?php
     $xml = simplexml_load_file ( 'log.xml' );

     $movies = $xml->addChild("time");
      // add attribut `value` here in tag time

     $user = $movies->addChild("user", "");
     // add attribut `id` here in tag user

     $action = $user->addChild("action","");
     // add attribut `value` here in tag action

     $action->addChild("table","customers");

     $action->addChild("table_id","1");

     echo $xml->saveXML( 'log.xml' );
 ?>

我希望输出看起来像这样:

// log.xml
<?xml version="1.0" encoding="utf-8"?>
<log>
<time value="2013-01-10 12:20:01">
    <user id="1">
        <action value="delete">
            <table>customer</table>
            <table_id>1</table_id>
        </action>   
        <action value="insert">
            <table>customer</table>
            <data>
                <nama>budi</nama>
            </data>
        </action>
        <action value="update">
            <table>customer</table>
            <table_id>1</table_id>
            <old_data>
                <nama>andi</nama>
            </old_data>
            <new_data>
                <nama>budi</nama>
            </new_data>
        </action>
    </user>
</time>
</log>

请帮帮我..我对xml很陌生

4

3 回答 3

4

使用 SimpleXMLElement::addAttribute — 将属性添加到 SimpleXML 元素

编辑:您的用例 -

     $action = $user->addChild("action","");
     // add attribut `value` here in tag action
     $action->addAttribute('value','update'); // add this

     $action->addChild("table","customers");

     $action->addChild("table_id","1");

最好的例子:

http://php.net/manual/en/simplexmlelement.addattribute.php

<?php

include 'example.php';

$sxe = new SimpleXMLElement($xmlstr);
$sxe->addAttribute('type', 'documentary');

$movie = $sxe->addChild('movie');
$movie->addChild('title', 'PHP2: More Parser Stories');
$movie->addChild('plot', 'This is all about the people who make it work.');

$characters = $movie->addChild('characters');
$character  = $characters->addChild('character');
$character->addChild('name', 'Mr. Parser');
$character->addChild('actor', 'John Doe');

$rating = $movie->addChild('rating', '5');
$rating->addAttribute('type', 'stars');

echo $sxe->asXML();

?>

归功于 PHP.Net 参考页面中的第一个示例...

于 2013-05-20T05:03:06.193 回答
1

花了三个小时将 SimpleXML 与其他解决方案结合起来,终于搞定了。它有效,我希望它会有用。

$xml_data = new SimpleXMLElement('<?xml version="1.0"?><data></data>');

$arrXml = [
    "categories" => [
        'category' => [
            '@attributes' => [
                'id' => '123',
                'parent_id' => '12345'
            ],
            '@value' => 'Bikes'
        ]
    ],
    "properties" => [
        'property' => [
            'id' => '123',
            'categoryId' => '1',
            'name' => 'Color',
            'values' => [
                'value' => [
                    "id" => '1',
                    "name" => 'Black'
                ],
                'value' => [
                    "id" => '2',
                    "name" => 'White'
                ]
            ]
        ]
    ],
    "products" => [
        'products' => [
            'id' => '1231231',
            'categoryId' => '123',
            'model' => [
                    '@attributes' => [
                            'foo' => 'bar',
                    ],
                '@value' => 'Avalanche'
            ],
            'article' => '1.0 2011',
            'vendor' => 'GT',
        ]
    ]
];

array_to_xml($arrXml,$xml_data);

//saving generated xml file;
$result = $xml_data->asXML('test.xml');

// 打印 $xml_data->asXML();

<data>
<categories>
    <category id="123" parent_id="12345">Bikes</category>
</categories>
<properties>
    <property>
        <id>123</id>
        <categoryId>1</categoryId>
        <name>Color</name>
        <values>
            <value>
                <id>2</id>
                <name>White</name>
            </value>
        </values>
    </property>
</properties>
<products>
    <products>
        <id>1231231</id>
        <categoryId>123</categoryId>
        <category foo="bar">Avalanche</category>
        <article>1.0 2011</article>
        <vendor>GT</vendor>
    </products>
</products>

使用此功能:

function array_to_xml( $data, &$xml_data ) {
        foreach( $data as $key => $value ) {
            if (!empty($value)) {
                if( is_array($value)) {
                    if (!empty($value["@attributes"])) {
                            $subnode = $xml_data->addChild($key, $value["@value"]);
                            foreach ($value["@attributes"] as $key1 => $val1) {
                                $subnode->addAttribute($key1, $val1);
                            }
                    } else if ($key == "@value") {
                        foreach ($value as $attr => $attrVal) {
                            $subnode = $xml_data->addChild("$attr", $attrVal);
                            array_to_xml($attrVal, $subnode);
                        }
                    } else {
                            if (!empty($value)) {
                                    $subnode = $xml_data->addChild($key);
                                    array_to_xml($value, $subnode);
                            }
                    }
                } else {
                        $xml_data->addChild("$key",$value);
                }
            }
        }
    }
于 2021-03-09T00:12:37.783 回答
0

采用addAttribute()

$movies->addAttribute('value','your value here');
$action->addAttribute('value','your value here');
$user->addAttribute('id','your value here');
于 2013-05-20T05:03:18.893 回答