PHP xml 파일 쓰기

1 개요[ | ]

PHP xml 파일 쓰기
PHP 함수 xml_emit_file()
  • write_xml_file.php
<?php
function xml_emit_file( $filepath, $rootname, $arr ) {
	function a2x( $arr, &$xml, $pk=null ) {
		foreach( $arr as $k => $v ) {
			if( !is_array($v) ) $xml->addChild($k,htmlspecialchars($v));
			else if( is_numeric(key($v)) ) a2x($v, $xml, $k);
			else a2x($v, $xml->addChild( is_null($pk)? $k: $pk));
		}
	}
	$xml = new SimpleXMLElement("<$rootname/>");
	a2x($arr, $xml);
	return $xml->asXML( $filepath );
}

$arr = array(
	'molecule' => array(
		array(
			'name' => 'Alanine',
			'symbol' => 'ala',
			'code' => 'A',
			'type' => 'hydrophobic',
		),
		array(
			'name' => 'Lysine',
			'symbol' => 'lys',
			'code' => 'K',
			'type' => 'charged',
		),
	),
);
xml_emit_file( 'moldb.xml', 'moldb', $arr );
root@zetawiki:~# php write_xml_file.php 
root@zetawiki:~# cat moldb.xml 
<?xml version="1.0"?>
<moldb><molecule><name>Alanine</name><symbol>ala</symbol><code>A</code><type>hydrophobic</type></molecule><molecule><name>Lysine</name><symbol>lys</symbol><code>K</code><type>charged</type></molecule></moldb>

2 같이 보기[ | ]

3 참고[ | ]

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}