Php Json编码或序列化XML
我有一些xml,这是它的一个简单版本Php Json编码或序列化XML,php,xml,json,simplexml,Php,Xml,Json,Simplexml,我有一些xml,这是它的一个简单版本 <xml> <items> <item abc="123">item one</item> <item abc="456">item two</item> </items> </xml> 我可以使用$obj->xpath('//items/item')并访问@attributes 我需要一个数组结果,所以我尝试了json\u解码(json\u encod
<xml>
<items>
<item abc="123">item one</item>
<item abc="456">item two</item>
</items>
</xml>
我可以使用$obj->xpath('//items/item')编码>并访问@attributes
我需要一个数组结果,所以我尝试了json\u解码(json\u encode($obj),true)
技巧,但这似乎正在删除对@attributes的访问(即abc=“123”)
有没有其他方法可以做到这一点,即提供对属性的访问并留给我一个数组
$xml = new SimpleXMLElement($xmlString);
$xml现在是一个对象。要获取属性的值,请执行以下操作:
$xml->something['id'];
$sxml = simplexml_load_string( $xml );
$final_array = array();
foreach ( $sxml->items->item as $xml_item )
{
$formatted_item = array();
// Text content of item
$formatted_item['content'] = (string)$xml_item;
// Specifically get 'abc' attribute
$formatted_item['abc'] = (string)$xml_item['abc'];
// Maybe one of the attributes is an integer
$formatted_item['foo_id'] = (int)$xml_item['foo_id'];
// Or maybe you want to loop over lots of possible attributes
foreach ( $xml_item->attributes() as $attr_name => $attr_value )
{
$formatted_item['attrib:' . $attr_name] = (string)$attr_value;
}
// Add it to a final list
$final_array[] = $formatted_item;
// Or maybe you want that array to be keyed on one of the attributes
$final_array[ (string)$xml_item['key'] ] = $formatted_item;
}
其中“id”是属性的名称
$xml现在是一个对象。要获取属性的值,请执行以下操作:
$xml->something['id'];
$sxml = simplexml_load_string( $xml );
$final_array = array();
foreach ( $sxml->items->item as $xml_item )
{
$formatted_item = array();
// Text content of item
$formatted_item['content'] = (string)$xml_item;
// Specifically get 'abc' attribute
$formatted_item['abc'] = (string)$xml_item['abc'];
// Maybe one of the attributes is an integer
$formatted_item['foo_id'] = (int)$xml_item['foo_id'];
// Or maybe you want to loop over lots of possible attributes
foreach ( $xml_item->attributes() as $attr_name => $attr_value )
{
$formatted_item['attrib:' . $attr_name] = (string)$attr_value;
}
// Add it to a final list
$final_array[] = $formatted_item;
// Or maybe you want that array to be keyed on one of the attributes
$final_array[ (string)$xml_item['key'] ] = $formatted_item;
}
其中“id”是属性的名称。您需要调用attributes()函数
示例代码:
$xmlString = '<xml>
<items>
<item abc="123">item one</item>
<item abc="456">item two</item>
</items>
</xml>';
$xml = new SimpleXMLElement($xmlString);
foreach( $xml->items->item as $value){
$my_array[] = strval($value->attributes());
}
print_r($my_array);
$xmlString='0
项目一
项目二
';
$xml=新的simplexmlement($xmlString);
foreach($xml->items->item as$value){
$my_array[]=strval($value->attributes());
}
打印(我的数组);
您需要调用attributes()函数
示例代码:
$xmlString = '<xml>
<items>
<item abc="123">item one</item>
<item abc="456">item two</item>
</items>
</xml>';
$xml = new SimpleXMLElement($xmlString);
foreach( $xml->items->item as $value){
$my_array[] = strval($value->attributes());
}
print_r($my_array);
$xmlString='0
项目一
项目二
';
$xml=新的simplexmlement($xmlString);
foreach($xml->items->item as$value){
$my_array[]=strval($value->attributes());
}
打印(我的数组);
虽然理论上可以编写从XML到PHP或JSON结构的通用转换,但很难捕捉到可能存在的所有细微之处—子元素和属性之间的区别、属性旁边的文本内容(正如您在这里看到的)甚至子元素旁边的文本内容,具有相同名称的多个子节点,子元素和文本节点的顺序是否重要(例如在XHTML或DocBook中),等等
如果需要生成特定的格式,那么使用API(如SimpleXML)循环XML并生成所需的结构通常会容易得多
您没有指定要实现的结构,但给定输入的一般方法是循环每个项目,访问已知属性,或循环每个属性:
$xml->something['id'];
$sxml = simplexml_load_string( $xml );
$final_array = array();
foreach ( $sxml->items->item as $xml_item )
{
$formatted_item = array();
// Text content of item
$formatted_item['content'] = (string)$xml_item;
// Specifically get 'abc' attribute
$formatted_item['abc'] = (string)$xml_item['abc'];
// Maybe one of the attributes is an integer
$formatted_item['foo_id'] = (int)$xml_item['foo_id'];
// Or maybe you want to loop over lots of possible attributes
foreach ( $xml_item->attributes() as $attr_name => $attr_value )
{
$formatted_item['attrib:' . $attr_name] = (string)$attr_value;
}
// Add it to a final list
$final_array[] = $formatted_item;
// Or maybe you want that array to be keyed on one of the attributes
$final_array[ (string)$xml_item['key'] ] = $formatted_item;
}
虽然理论上可以编写从XML到PHP或JSON结构的通用转换,但很难捕捉到可能存在的所有细微之处—子元素和属性之间的区别、属性旁边的文本内容(正如您在这里看到的)甚至子元素旁边的文本内容,具有相同名称的多个子节点,子元素和文本节点的顺序是否重要(例如在XHTML或DocBook中),等等
如果需要生成特定的格式,那么使用API(如SimpleXML)循环XML并生成所需的结构通常会容易得多
您没有指定要实现的结构,但给定输入的一般方法是循环每个项目,访问已知属性,或循环每个属性:
$xml->something['id'];
$sxml = simplexml_load_string( $xml );
$final_array = array();
foreach ( $sxml->items->item as $xml_item )
{
$formatted_item = array();
// Text content of item
$formatted_item['content'] = (string)$xml_item;
// Specifically get 'abc' attribute
$formatted_item['abc'] = (string)$xml_item['abc'];
// Maybe one of the attributes is an integer
$formatted_item['foo_id'] = (int)$xml_item['foo_id'];
// Or maybe you want to loop over lots of possible attributes
foreach ( $xml_item->attributes() as $attr_name => $attr_value )
{
$formatted_item['attrib:' . $attr_name] = (string)$attr_value;
}
// Add it to a final list
$final_array[] = $formatted_item;
// Or maybe you want that array to be keyed on one of the attributes
$final_array[ (string)$xml_item['key'] ] = $formatted_item;
}
您可以使用json\u encode
和json\u decode
进行编码,您可以添加缺少的内容,因为json\u encode
-ing遵循simplexmlement
的一些特定规则
如果你对这些规则及其细节感兴趣,我已经写了两篇博客文章:
对您来说,可能更有趣的是第三部分,它展示了如何修改json序列化并提供您自己的格式(例如保留属性):
它附带了一个完整的示例,下面是代码摘录:
$xml = '<xml>
<items>
<item abc="123">item one</item>
<item abc="456">item two</item>
</items>
</xml>';
$obj = simplexml_load_string($xml, 'JsonXMLElement');
echo $json = json_encode($obj, JSON_PRETTY_PRINT), "\n";
print_r(json_decode($json, TRUE));
您可以使用json\u encode
和json\u decode
进行编码,您可以添加缺少的内容,因为json\u encode
-ing遵循simplexmlement
的一些特定规则
如果你对这些规则及其细节感兴趣,我已经写了两篇博客文章:
对您来说,可能更有趣的是第三部分,它展示了如何修改json序列化并提供您自己的格式(例如保留属性):
它附带了一个完整的示例,下面是代码摘录:
$xml = '<xml>
<items>
<item abc="123">item one</item>
<item abc="456">item two</item>
</items>
</xml>';
$obj = simplexml_load_string($xml, 'JsonXMLElement');
echo $json = json_encode($obj, JSON_PRETTY_PRINT), "\n";
print_r(json_decode($json, TRUE));
我发现有一个类能够很好地将XML处理成数组:()。转换为json需要调用json\u encode()
。我发现有一个类可以很好地将XML处理成数组:()。转换为json需要调用json\u encode()
调用。我需要一个数组结果。我将存储它,并希望在某个时候检索它。我希望属性在解码或取消序列化后出现在数组中。我需要一个数组结果。我将存储它,并希望在某个时候检索它。我希望属性在我解码或取消序列化后出现在数组中。我需要一个数组结果,所以我猜我需要自己构建一个数组?因为json_encode()会剥离属性?正如我所怀疑的那样。我希望可能有一些json_encode()技巧,或者相关的函数可以在将对象转换为数组时保留属性。谢谢这种方法似乎很合理,但这种代码很奇怪。为什么要使用$xml->items->item[$i++]
而不是$value
?当foreach($xml->items->items as$value)
也可以工作时,为什么分配$xmlitems
只是在其上循环呢?噢,还有,strval($foo->attributes())
总是返回字符串'array'
。我猜您的意图是将strval
应用于每个属性,而不是整个列表?我需要一个数组结果,所以我猜我需要自己构建一个数组?因为json_encode()会剥离属性?正如我所怀疑的那样。我希望可能有一些json_encode()技巧,或者相关的函数可以在将对象转换为数组时保留属性。谢谢这种方法似乎很合理,但这种代码很奇怪。为什么要使用$xml