SimpleXMLIterator回到XML

时间:2017-03-03 12:00:52

标签: php arrays xml simplexml

我有一个小问题。 首先,我将整个XML文件加载到SimpleXMLIterator数组中。然后我做了一些事情(比如选择元素,检查元素的条件是否为真等等)。在此之后我输出了像。

array(2) {
  [0]=>
  object(SimpleXMLIterator)#2 (8) {
    ["@attributes"]=>
    array(1) {
      ["id"]=>
      string(5) "bk101"
    }
    ["author"]=>
    string(20) "Gambardella, Matthew"
    ["title"]=>
    string(21) "XML Developer's Guide"
    ["genre"]=>
    string(8) "Computer"
    ["price"]=>
    string(5) "44.95"
    ["publish_date"]=>
    string(10) "2000-10-01"
    ["value"]=>
    string(2) "50"
    ["description"]=>
    string(58) "An in-depth look at creating applications 
      with XML."
  }
  [1]=>
  object(SimpleXMLIterator)#4 (8) {
    ["@attributes"]=>
    array(1) {
      ["id"]=>
      string(5) "bk102"
    }
    ["author"]=>
    string(10) "Ralls, Kim"
    ["title"]=>
    string(13) "Midnight Rain"
    ["genre"]=>
    string(7) "Fantasy"
    ["price"]=>
    string(4) "5.95"
    ["publish_date"]=>
    string(10) "2000-12-16"
    ["description"]=>
    string(130) "A former architect battles corporate zombies, 
      an evil sorceress, and her own childhood to become queen 
      of the world."
    ["value"]=>
    string(2) "50"
  }
}

他们的数组代表此XML

    <book id="bk101">
      <author>Gambardella, Matthew</author>
      <title>XML Developer's Guide</title>
      <genre>Computer</genre>
      <price da="aaaa">44.95</price>
      <publish_date>2000-10-01</publish_date>
      <value>50</value>
      <description>An in-depth look at creating applications 
      with XML.</description>
   </book>
   <book id="bk102">
      <author>Ralls, Kim</author>
      <title>Midnight Rain</title>
      <genre>Fantasy</genre>
      <price a="a">5.95</price>
      <publish_date>2000-12-16</publish_date>
      <description>A former architect battles corporate zombies, 
      an evil sorceress, and her own childhood to become queen 
      of the world.</description>
      <value>50</value>
   </book>

如果有任何方法可以将该数组更改为XML文件?

注意: book root 元素不存储在数组中。但这不是一个问题。我将该元素的名称存储在变量中,因此我可以使用类似

的内容
for( //number of elements of array ) {
    file_put_contents($output, "<" . $element ."/>", FILE_APPEND);
    foreach(...)
       //putting content from array
    file_put_contents($output, "<" . / $element . "/>", FILE_APPEND);
}

那么有没有机会做到这一点&#34;很好地&#34;?

0 个答案:

没有答案