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.

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

报告相同问题?

悬赏问题

  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。
  • ¥20 CST怎么把天线放在座椅环境中并仿真
  • ¥15 任务A:大数据平台搭建(容器环境)怎么做呢?