使用php将数组转换为xml

时间:2015-02-11 06:02:02

标签: php arrays xml

我有一个多维数组。数组是从我的xml结构精心构建的。这是var_dump

array (size=1)
  'Articles' => 
    array (size=4)
      '@About' => string 'programming' (length=11)
      '@compile' => string 'pro' (length=3)
      'Article' => 
        array (size=3)
          0 => 
            array (size=6)
              ...
          1 => 
            array (size=6)
              ...
          2 => 
            array (size=6)
              ...
      '$' => string 'Idiot ! How to programme' (length=24)

这是与print_r()

的关系

the return of print_r() :
Array ( 
        [Articles] => Array ( 
                      [@About] => programming 
                      [@compile] => pro 
         [Article] => Array 
                 ( 
                      [0] => Array 
                      ( 
                           [@ID] => 111 
                           [@Lang] => C++ 
                           [title] => Array ( 
                                 [@HELP] => Needed 
                           [$] => C++ is my boss 
                       ) 
                          [author] => Idiotic Me 
                          [summary] => if you dont like Cpp, your a Moron 
                          [$] => Best Language ) 
                       [1] => Array 
                       ( 
                          [@ID] => 222 
                          [@Lang] => Json 
                          [title] => 
                       Array 
                       ( 
                          [ox] => Array ( ) 
                          [$] => Json is a fool 
                       ) 
                          [author] => who cares ! 
                          [summary] => I detest json 
                          [$] => Or whatever it calls itself 
                        ) 
                        [2] => Array
                        ( 
                           [@ID] => 333 
                           [@Lang] => Ruby 
                           [title] => A rival of Cpp 
                           [author] => intersting to know 
                           [summary] => a good language i think 
                           [$] => Hottest in town 
                         ) 
                  ) 
                   [$] => Idiot ! How to programme 
                )
 ) 
请注意,所有属性名称都针对“@”符号和节点内容映射到“$”符号。显然,这里是获取上述数组的xml数据:

现在这里是从中获取上述数组的xml结构:

<?xml version="1.0"?>
<Articles About="programming" compile="pro"> 
    Idiot ! How to programme
    <Article ID="111" Lang= "C++"> 
        Best Language
        <title HELP="Needed">C++ is my boss</title>
        <author>Idiotic Me</author> 
        <summary>if you dont like Cpp, your a Moron </summary> 
    </Article>
    <Article ID="222" Lang="Json">
     Or whatever it calls itself
        <title> Json is a fool
              <sub> just a test </sub>
        </title>
        <author> who cares !</author> 
        <summary> I detest json </summary> 
    </Article> 
    <Article ID="333" Lang="Ruby">
     Hottest in town
        <title> A rival of Cpp</title>
        <author> intersting to know</author> 
        <summary> a good language i think </summary>  
    </Article>    
</Articles>

我发现将上面的数组转换回精确的xml形式很痛苦。任何提前解决的解决方案!

0 个答案:

没有答案