dpjs2005 2012-09-14 01:45
浏览 45
已采纳

在PHP中将XML片段转换为数组时,如何保留“标记顺序”?

I've read a large number of articles and Stack Overflow questions about converting an XML document or fragment into an array in PHP, but none that I've read so far have addressed my specific problem. Here's my dilemma, preceded by an example XML fragment:

<category>
  <template>
    <random>
      <li>And a good</li>
      <li>Pleasant</li>
      <li>Good</li>
      <li>Fantabulous</li>
    </random>
    <set name="TOD"><srai>time of day</srai></set> to you, <get name="name" />.
    <random>
      <li>How are you?</li>
      <li>To what do I owe the pleasure of this visit?</li>
      <li>May your Athlete's Foot be under control, and may the flying monkeys never come to take your dog!</li>
      <li>I trust your <get name="TOD" /> is going well?</li>
      <li>May your <get name="TOD" /> be as pleasant as possible.</li>
    </random>
  </template>
</category>

This is a REAL WORLD example of some of the XML my script will be dealing with. The sequence order of the XML tags needs to be preserved, as the parsed results need to be concatenated correctly in order to provide the proper result. So far, all of the methods for converting XML fragments into arrays have created arrays that no longer contain the correct order. As an example, here's a var dump of the above XML, once it's been converted into an array:

Template array Var Dump: 
array(4) {
  ["random"]=>
  array(2) {
    [0]=>
    array(1) {
      ["li"]=>
      array(4) {
        [0]=>
        array(1) {
          ["text"]=>
          string(10) "And a good"
        }
        [1]=>
        array(1) {
          ["text"]=>
          string(8) "Pleasant"
        }
        [2]=>
        array(1) {
          ["text"]=>
          string(4) "Good"
        }
        [3]=>
        array(1) {
          ["text"]=>
          string(11) "Fantabulous"
        }
      }
    }
    [1]=>
    array(1) {
      ["li"]=>
      array(5) {
        [0]=>
        array(1) {
          ["text"]=>
          string(12) "How are you?"
        }
        [1]=>
        array(1) {
          ["text"]=>
          string(44) "To what do I owe the pleasure of this visit?"
        }
        [2]=>
        array(1) {
          ["text"]=>
          string(97) "May your Athlete's Foot be under control, and may the flying monkeys never come to take your dog!"
        }
        [3]=>
        array(2) {
          ["text"]=>
          array(2) {
            [0]=>
            string(12) "I trust your"
            [1]=>
            string(14) "is going well?"
          }
          ["get"]=>
          array(1) {
            ["@attributes"]=>
            array(1) {
              ["name"]=>
              string(3) "TOD"
            }
          }
        }
        [4]=>
        array(2) {
          ["text"]=>
          array(2) {
            [0]=>
            string(8) "May your"
            [1]=>
            string(27) "be as pleasant as possible."
          }
          ["get"]=>
          array(1) {
            ["@attributes"]=>
            array(1) {
              ["name"]=>
              string(3) "TOD"
            }
          }
        }
      }
    }
  }
  ["set"]=>
  array(2) {
    ["@attributes"]=>
    array(1) {
      ["name"]=>
      string(3) "TOD"
    }
    ["srai"]=>
    array(1) {
      ["text"]=>
      string(11) "time of day"
    }
  }
  ["text"]=>
  array(2) {
    [0]=>
    string(7) "to you,"
    [1]=>
    string(1) "."
  }
  ["get"]=>
  array(1) {
    ["@attributes"]=>
    array(1) {
      ["name"]=>
      string(4) "name"
    }
  }
}

As can be seen, the array, when created, "lost" the sequence order of the XML fragment, and you can't iterate through the array in a linear manner to arrive at the proper response. This is the crux of my problem, and what I'm looking to "fix".

The method I used in this example was json_decode(json_encode($xml), true), but I've used other, more complex script functions, with pretty much the same results. So, just as I asked in the title of this post, how can I preserve the "tag order" when converting an XML fragment to an array in PHP?

  • 写回答

1条回答 默认 最新

  • dtl4521 2012-09-14 01:57
    关注

    Pretty sure there are no flags available with say simpleXML or json_decode. I don't think XML is intended to preserve that. The structure is not intended to convey that, can see how it leads to bad design. Specific to XML, you can get around this by use of sequence in an XSD. But your data looks more like a DOM.

    As a work around have you looked at parsing it with as a DOM Document and stepping through it? Not too much code to parse it yourself leveraging that.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥15 帮我写一个c++工程