使用PHP函数在XML中删除元素

时间:2013-02-23 07:04:39

标签: php xml function

我有一个像这样的数组

Array
(
    [351] => Array
        (
            [0] => Array
                (
                    [unit_id] => Array
                        (
                            [0] => 10192
                            [1] => 10192
                            [2] => 10192
                        )
                    [born_from] => Array
                        (
                            [0] => 1980-08-21
                        )
                    [born_to] => Array
                        (
                            [0] => 2010-08-18
                        )
                    [start_date] => Array
                        (
                            [0] => 2013-02-21 17:29:00
                        )
                    [sex] => Array
                        (
                            [0] => 3
                        )
                    [id] => 351
                    [product_id] => 1
                    [name] => test1
                )

        )
)

我编写了一个PHP函数来将这个数组转换为xml

功能是

function generate_xml_from_array($array, $node_name) {
    $xml = '';

    if (is_array($array) || is_object($array)) {
        foreach ($array as $key=>$value) {
            if (is_numeric($key)) {
                $key = $node_name;
            }

            $xml .= '<' . $key . '>' . "\n" . generate_xml_from_array($value, $node_name) . '</' . $key . '>' . "\n";
        }
    } else {
        $xml = htmlspecialchars($array, ENT_QUOTES) . "\n";
    }

    return $xml;
}

function generate_valid_xml_from_array($array, $node_block='products', $node_name='node') {
    $xml = '<?xml version="1.0" encoding="UTF-8" ?>' . "\n";
    $xml .= '<' . $node_block . '>' . "\n";
    $xml .= generate_xml_from_array($array, $node_name);
    $xml .= '</' . $node_block . '>' . "\n";

    return $xml;
}

$xml = generate_valid_xml_from_array($MyArray);
file_put_contents('C:/product.xml',$xml);

这给了我这样的输出

<?xml version="1.0" encoding="UTF-8" ?>
<products>
<node>
    <node>
    <unit_id><node>10192</node><node>10192</node><node>10192</node></unit_id>
    <born_from><node>1980-08-21</node></born_from>
    <born_to><node>2010-08-18</node></born_to>
    <start_date><node>2013-02-21 17:29:00</node></start_date>
    <sex><node>3</node></sex>
    <id>351</id>
    <product_id>1</product_id>
    <name>test1</name>
    </node>
</node>
</products>

在此输出中有一个<node>,它会以任意方式增加文件大小以删除此<node>,因此文件大小不会增加

1 个答案:

答案 0 :(得分:0)

更改您的功能

function generate_xml_from_array($array, $node_name) {
$xml = '';

if (is_array($array) || is_object($array)) {

    foreach ($array as $key=>$value) {
        if (is_numeric($key) ) {

        if(count($array)>1)
             {
        $xml .= '<' . $node_name . '>' . "\n" . generate_xml_from_array($value, $node_name) . '</' . $node_name . '>' . "\n";
             }
          else
            {
        $xml .= generate_xml_from_array($value, $node_name). "\n";  
            }

        }
      else{ 
            $xml .= '<' . $key . '>' . "\n" . generate_xml_from_array($value, $node_name) . '</' . $key . '>' . "\n";}
          }
} else {
    $xml = htmlspecialchars($array, ENT_QUOTES) . "\n";
}

return $xml;
     }

并且您将获得以下XML

<?xml version="1.0" encoding="UTF-8" ?> 
<products>
<unit_id>
<node>10192</node> 
<node>10192</node> 
<node>10192</node> 
</unit_id>
<born_from>1980-08-21</born_from> 
<born_to>2010-08-18</born_to> 
<start_date>2013-02-21 17:29:00</start_date> 
<sex>3</sex> 
<id>351</id> 
<product_id>1</product_id> 
<name>test1</name> 
</products>