在PHP中将XML转换为JSON时,如何将@attributes移动到该元素的根目录?
我发现转换XML文件的最简单方法是json_encode
。但我想摆脱@attributes所以id
和name
例如直接在根目录,因为我将所有内容直接导入数据库进行操作和计算。 (数据是从好奇的7天后死去的。)
如何轻松地将这些XML文件转换为json,然后我可以将这些文件导入到我的数据库中而不需要那些丑陋的@attributes?我理解他们的目的,但我在这里没有必要,正如你所看到的那样。
小样本:
<block id="1" name="stone">
<property name="Material" value="stone"/>
<property name="Shape" value="Terrain"/>
<property name="Mesh" value="terrain"/>
<property name="Texture" value="1"/>
<property name="ImposterExchange" value="imposterBlock" param1="97"/>
<property name="DropScale" value="2"/>
<property name="LPHardnessScale" value="2"/>
<property name="Weight" value="125"/>
<property name="Map.Color" value="100,100,100"/>
<property class="RepairItems"> <property name="cobblestones" value="6"/> </property>
<property name="HarvestOverdamage" value="true"/> <!-- default=true -->
<drop event="Harvest" name="rockSmall" count="25" tool_category="harvestingTools"/>
<drop event="Harvest" name="ironFragment" count="2" tool_category="harvestingTools"/>
<drop event="Destroy" count="0"/>
<drop event="Fall" name="rockSmall" count="50" prob="0.23" stick_chance="0"/>
<drop event="Fall" name="ironFragment" count="4" prob="0.23" stick_chance="0"/>
<drop event="Fall" name="destroyedStone" count="1" prob="0.1" stick_chance="0.5"/>
<property name="CanMobsSpawnOn" value="true"/>
<property name="EconomicValue" value="5"/>
<property name="EconomicBundleSize" value="1"/>
<property name="SellableToTrader" value="false"/>
</block>
非常小看起来像:
{
id: 1,
name: "stone",
property: [
{
name: "Material",
value: "stone"
},
{
name: "Shape",
value: "Terrain"
}
],
drop: [
{
event: "Harverst",
name: "rockSmall",
count: 25
},
{
event: "Harvest",
name: "ironFragment",
count: 2
}
]
}
答案 0 :(得分:0)
我们试试这个:
<?php
$xmlContent = '<block id="1" name="stone">
<property name="Material" value="stone"/>
<property name="Shape" value="Terrain"/>
<property name="Mesh" value="terrain"/>
<property name="Texture" value="1"/>
<property name="ImposterExchange" value="imposterBlock" param1="97"/>
<property name="DropScale" value="2"/>
<property name="LPHardnessScale" value="2"/>
<property name="Weight" value="125"/>
<property name="Map.Color" value="100,100,100"/>
<property class="RepairItems"> <property name="cobblestones" value="6"/> </property>
<property name="HarvestOverdamage" value="true"/> <!-- default=true -->
<drop event="Harvest" name="rockSmall" count="25" tool_category="harvestingTools"/>
<drop event="Harvest" name="ironFragment" count="2" tool_category="harvestingTools"/>
<drop event="Destroy" count="0"/>
<drop event="Fall" name="rockSmall" count="50" prob="0.23" stick_chance="0"/>
<drop event="Fall" name="ironFragment" count="4" prob="0.23" stick_chance="0"/>
<drop event="Fall" name="destroyedStone" count="1" prob="0.1" stick_chance="0.5"/>
<property name="CanMobsSpawnOn" value="true"/>
<property name="EconomicValue" value="5"/>
<property name="EconomicBundleSize" value="1"/>
<property name="SellableToTrader" value="false"/>
</block>';
$xml = simplexml_load_string($xmlContent, "SimpleXMLElement", LIBXML_NOCDATA);
$array = object_to_array($xml);
echo json_encode($array, JSON_PRETTY_PRINT);
function object_to_array($input)
{
$return = array();
foreach ((array)$input as $key => $value) {
if (strpos($key, '@') === 0) {
$key = substr($key, 1);
}
if (is_array($value) || is_object($value)) {
$value = object_to_array($value);
}
$return[$key] = $value;
}
return $return;
}
输出:
{
"attributes": {
"id": "1",
"name": "stone"
},
"property": [
{
"attributes": {
"name": "Material",
"value": "stone"
}
},
答案 1 :(得分:0)
不要将其视为“将任意XML文件转换为JSON”,而应将其视为“从XML文件中提取我想要的信息”以及“将该信息序列化为JSON”。
如果文件始终具有您显示的结构,您可以轻松地从顶层提取id
和name
:
$block = simplexml_load_string($my_xml_data);
$extracted = [
'id' => (int)$block['id'],
'name' => (string)$block['name'],
];
然后,您可以明确地遍历property
元素,获取其名称和值:
$extracted['property'] = [];
foreach ( $block->property as $property ) {
$extracted['property'][] = [
'name' => (string)$property['name'],
'value' => (string)$property['value']
];
}
如果你想让它更通用一些,你可以循环遍历所有属性并捕获它们的名称和值:
$extracted['property'] = [];
foreach ( $block->property as $property ) {
$next_property = [];
foreach ( $property->attributes() as $attr_name => $attr_value ) {
$next_property[ $attr_name ] = (string)$attr_value;
}
$extracted['property'][] = $next_property;
}
然后你可以使用相同的东西来获取drop
节点,或者你可以使它真正通用并且在你得到的任何顶级元素中啜饮:
foreach ( $block->children() as $element_name => $element ) {
// Note that unlike looping over an array, you will
// get the same $element_name more than once!
if ( ! isset($extracted[$element_name]) ) {
$extracted[$element_name] = [];
}
$next_item = [];
foreach ( $element->attributes() as $attr_name => $attr_value ) {
$next_item[ $attr_name ] = (string)$attr_value;
}
$extracted[$element_name][] = $next_item;
}
请注意,这是不递归,因为创建一个既包含所有可能的XML输入又产生良好结果的递归函数非常棘手。假设您正在处理的XML的一般形状不会改变,您可以决定要创建的数据结构的形状(以及JSON)。