2

我已经阅读了大量关于在 PHP 中将 XML 文档或片段转换为数组的文章和 Stack Overflow 问题,但到目前为止我所阅读的都没有解决我的具体问题。这是我的困境,前面是一个示例 XML 片段:

<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>

这是我的脚本将要处理的一些 XML 的真实示例。需要保留 XML 标记的顺序,因为解析结果需要正确连接以提供正确的结果。到目前为止,所有将 XML 片段转换为数组的方法都创建了不再包含正确顺序的数组。例如,这里是上述 XML 的 var 转储,一旦它被转换为数组:

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"
    }
  }
}

可以看出,数组在创建时“丢失”了 XML 片段的顺序,并且您无法以线性方式遍历数组以获得正确的响应。这是我问题的症结所在,也是我想要“解决”的问题。

我在此示例中使用的方法是json_decode(json_encode($xml), true),但我使用了其他更复杂的脚本函数,结果几乎相同。那么,正如我在这篇文章的标题中所问的那样,在 PHP 中将 XML 片段转换为数组时,如何保留“标签顺序”?

4

1 回答 1

2

simpleXML很确定 say或没有可用的标志json_decode。我不认为 XML 旨在保留这一点。该结构并非旨在传达这一点,可以看到它如何导致糟糕的设计。特定于 XML,您可以通过sequence在 XSD 中使用来解决此问题。但是你的数据看起来更像一个 DOM。

作为一种解决方法,您是否考虑过将其解析为DOM 文档并单步执行?没有太多代码可以自己利用它来解析它。

于 2012-09-14T01:57:50.043 回答