[英]Converting XML to JSON using PHP while preserving Arrays
我需要將XML文檔轉換為JSON,以便輕松訪問JavaScript中的數據。 我目前正在使用此方法將XML轉換為JSON:
json_encode(new SimpleXMLElement($xml, LIBXML_NOCDATA));
但是,當元素只包含1個子元素時,我遇到了問題。 當使用SimpleXML解析它時,它被視為對象而不是數組。 我希望它們始終被視為數組,除非元素只包含文本。
例:
$xml = <<<END
<xml>
<TESTS>
<TEST>TEXT HERE</TEST>
</TESTS>
</xml>
END;
echo json_encode(new SimpleXMLElement($xml, LIBXML_NOCDATA));
這輸出:
{"TESTS":{"TEST":"TEXT HERE"}}
如果我在下面添加另一個元素,那么輸出就是我想要的:
$xml = <<<END
<xml>
<TESTS>
<TEST>TEXT HERE</TEST>
<TEST>MORE TEXT HERE</TEST>
</TESTS>
</xml>
END;
echo json_encode(new SimpleXMLElement($xml, LIBXML_NOCDATA));
輸出:
{"TESTS":{"TEST":["TEXT HERE","TEXT HERE"]}}
請注意元素如何包含在JSON數組中而不是JSON對象中。 有沒有辦法強制元素被解析為數組?
我不得不處理同樣的情況。 這是問題的快速第一解決方案 - 適用於您的示例。
class JsonWithArrays
{
protected $root, $callback;
public function __construct( SimpleXMLElement $root )
{
$this->root = $root;
}
/**
* Set a callback to return if a node should be represented as an array
* under any circumstances.
*
* The callback receives two parameters to react to: the SimpleXMLNode in
* question, and the nesting level of that node.
*/
public function use_callback_forcing_array ( $callback )
{
$this->callback = $callback;
return $this;
}
public function to_json ()
{
$transformed = $this->transform_subnodes( $this->root, new stdClass(), 0 );
return json_encode( $transformed );
}
protected function transform_subnodes ( SimpleXMLElement $parent, stdClass $transformed_parent, $nesting_level )
{
$nesting_level++;
foreach( $parent->children() as $node )
{
$name = $node->getName();
$value = (string) $node;
if ( count( $node->children() ) > 0 )
{
$transformed_parent->$name = new stdClass();
$this->transform_subnodes( $node, $transformed_parent->$name, $nesting_level );
}
elseif ( count( $parent->$name ) > 1 or $this->force_array( $node, $nesting_level ) )
{
$transformed_parent->{$name}[] = $value;
}
else
{
$transformed_parent->$name = $value;
}
}
return $transformed_parent;
}
protected function force_array ( $node, $nesting_level )
{
if ( is_callable( $this->callback ) )
{
return call_user_func( $this->callback, $node, $nesting_level );
}
else
{
return false;
}
}
}
$xml = <<<END
<xml>
<TESTS>
<TEST>TEXT HERE</TEST>
</TESTS>
</xml>
END;
$xml2 = <<<END
<xml>
<TESTS>
<TEST>TEXT HERE</TEST>
<TEST>MORE TEXT HERE</TEST>
</TESTS>
</xml>
END;
// Callback using the node name. Could just as well be done using the nesting
// level.
function cb_testnode_as_array( SimpleXMLElement $node, $nesting_level )
{
return $node->getName() == 'TEST';
}
$transform = new JsonWithArrays( new SimpleXMLElement($xml, LIBXML_NOCDATA) );
echo $transform
->use_callback_forcing_array( 'cb_testnode_as_array' )
->to_json();
echo PHP_EOL;
$transform2 = new JsonWithArrays( new SimpleXMLElement($xml2, LIBXML_NOCDATA) );
echo $transform2
->use_callback_forcing_array( 'cb_testnode_as_array' )
->to_json();
echo json_encode(json_decode(json_encode(new SimpleXMLElement($xml, LIBXML_NOCDATA)), true));
真的這很愚蠢,但你首先如果所有轉換為對象然后在數組中解碼並轉換為json像數組樣式..))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.