【发布时间】:2012-08-22 08:03:04
【问题描述】:
嗯,我在将 xml 更改回数组时遇到了问题....如果每个 xml 都遵循相同的格式,但每个 XML 都不同,<Formula> 标记、公式名称和 movespeed 除外
例如:
<Formula>
<formulaname>Basic</formulaname>
<movespeed>1</movespeed>
<str>4</str>
<dex>3</dex>
<int>1</int>
<will>2</will>
</Formula>
或
<Formula>
<formulaname>Basic</formulaname>
<movespeed>1</movespeed>
<box>4</box>
<chicken>3</chicken>
<ducks>1</ducks>
<cereal>2</cereal>
</Formula>
我尝试过的:
$xml = simplexml_load_file("test.xml");
print_r($xml);
这实际上是prints 的东西,但我无法超越它甚至echo 它..
foreach($xml->text as $string) {
print_r($string);
echo 'attributes: '. $string->attributes() .'<br />';
}
没用,原来是给strings的,但都不是strings...
foreach ($xml->Formula as $element) {
foreach($element as $key => $val) {
echo "{$key}: {$val}";
}
也没有工作,我需要这样的东西才能工作,这样我就可以使用来自array 的值,而无需知道该值究竟会被调用什么......
【问题讨论】:
-
你的 XML 有根标签吗?还是只是一堆公式标签?
-
您是否能够不使用 XML?如果您能够改用 JSON,则只需一个简单的
json_decode($json, TRUE)调用即可。 -
attributes 是一个对象,将其类型转换为一个数组 $attributes = $string->attributes(); $attributes = (array) $attributes;那么你就可以随心所欲地抓住任何东西。
-
xml 显示得非常好,也是的,我很遗憾地被锁定在使用 XML 中。 $attributes = $string->attributes(); $attributes = (数组);回声'属性:'。 $attributes .'
';出错了 -
建议在这里阅读:php.net/manual/en/class.simplexmliterator.php -- cmets 中有几个 XMl2Array 示例函数。