パースしたXMLファイルのデータを一部編集して保存

概要

asXML


サンプル

XMLファイル

<document>
	<title type="a">Forty What?</title>
	<from>Joe</from>
	<to>Jane</to>
	<body>
		I know that's the answer -- but what's the question?
	</body>
	<list>
		<item id="1">1111</item>
		<item id="2">2222</item>
		<item id="3">3333</item>
	</list>
</document>
 
 

PHP(asXMLでファイル名を指定)

<?php
 
	// XMLを解凍
	$xml = simplexml_load_file("sample02_1.xml");
 
	// 出力
	echo "<pre>";
	var_dump($xml);
	echo "</pre>";
 
	// データ編集
	$xml->title = "テストタイトル";
	$xml->from = "A支店";
	$xml->to = "A支店";
 
	// 値を保存
	$xml->asXML("sample03_1.xml");
 
	// XMLを解凍
	$xml2 = simplexml_load_file("sample03_1.xml");
 
	// 出力
	echo "<pre>";
	var_dump($xml2);
	echo "</pre>";
 
?>
 
 
 

PHP(asXMLで変換したものをfile_put_contentsで保存)

<?php
 
	// XMLを解凍
	$xml = simplexml_load_file("sample02_1.xml");
 
	// 出力
	echo "<pre>";
	var_dump($xml);
	echo "</pre>";
 
	// データ編集
	$xml->title = "テストタイトル";
	$xml->from = "A支店";
	$xml->to = "A支店";
 
	// 値を保存
	file_put_contents("sample03_2.xml", $xml->asXML());
 
	// XMLを解凍
	$xml2 = simplexml_load_file("sample03_2.xml");
 
	// 出力
	echo "<pre>";
	var_dump($xml2);
	echo "</pre>";
 
?>
 
 



最終更新:2012年08月23日 20:06