PHP_PHP チュートリアルを使用して XML ファイルを読み書きするためのヒント
一般的に使用される行は次のとおりです:
header("content-type:text/html; charset=utf-8"); //UTF-8 エンコーディングを使用するように PHP を指定します
$xml = simplexml_load_file("example.xml") //xml ファイルを読み込みます
$newxml = $xml->asXML() // $xml を標準化します
;
$fp = fopen("newxml.xml", "w"); // 新しい XML ファイル
fwrite($fp, $newxml); // -------xml ファイルを書き込みます
fclose($fp);
PHP は XML ファイルを簡単に生成して読み取ることができます。 PHP は主に、DOMDocument、DOMElement、および DOMNodeList を通じて XML の読み取りおよび書き込み操作を完了します。以下に、これらのクラスの使用方法を簡単に説明します。
1. XMLファイルを生成します
XMLファイルの場合は以下のようになります。
[html]
;
http://blog.csdn.net/morewindows/article/details/7102362
記事>
PHP を使用して生成する方法を見てみましょう:
まず新しい DOMDocument オブジェクトを作成し、エンコード形式を設定します。
$dom = newDOMDocument('1.0', 'UTF-8');
$dom->formatOutput= true;
$rootelement =$dom->createElement("article");
$title =$dom->createElement("title", "PHP Access MySql データベース - 初級");
次に、テキストコンテンツを含むノードを作成します
$link =$dom->createElement("link","http://blog.csdn.net/morewindows/article/details/7102362");
最初に ノードを生成してから、そこにテキスト コンテンツを追加することもできます。
$link = $dom->createElement("link");
$linktext =$dom->createTextNode('http://blog.csdn.net/morewindows/article/details/7102362');
$link->appendChild($linktext);
次に、
$rootelement->appendChild($link);
最後に、
$dom->appendChild($rootelement);
完全な XML が生成されます。次に、XML 全体を再生成します
echo $dom->saveXML() ;
saveXML() は XML テキストの一部のみを入力することもできます。たとえば、echo $dom->saveXML($link); は http://blog.csdn のみを出力します。 .net/morewindows/article/details/7102362
以下は、PHP でデータ コンテンツを XML ファイルに出力する完全な例です。この例では、PHP 配列を XML ファイルに出力します。
[php] //配列をXMLファイルに出力します
// 投稿者 MoreWindows( http://blog.csdn.net/MoreWindows )
$article_array = array(「最初の記事」 => array(
"title"=>"PHP が MySql データベースにアクセスする - 初級",
"リンク"=>"http://blog.csdn.net/morewindows/article/details/7102362"
)、
「2番目の記事」 => array(
"title"=>"PHP Access MySql データベース中間 Smarty テクノロジー",
"リンク"=>"http://blog.csdn.net/morewindows/article/details/7094642"
)、
「パート 3」 => array(
"title"=>"PHP Access MySql データベース高度な AJAX テクノロジ",
"リンク"=>"http://blog.csdn.net/morewindows/article/details/7086524"
)、
);
$dom = 新しい DOMDocument('1.0', 'UTF-8');
$dom->formatOutput = true
$rootelement = $dom->createElement("MoreWindows");
foreach ($article_array as $key=>$value)
{
$article = $dom->createElement("article", $key);
$title = $dom->createElement("タイトル", $value['タイトル']);
$link = $dom->createElement("link", $value['link']);
$article->appendChild($title);
$article->appendChild($link);
$rootelement->appendChild($article);
}
$dom->appendChild($rootelement);
$ファイル名 = "D:test.xml";
echo 'XML ファイル サイズ' . $dom->save($filename)
?>
//配列をXMLファイルに出力します
// 投稿者 MoreWindows( http://blog.csdn.net/MoreWindows )
$article_array = 配列(
「最初の記事」 => array(
"title"=>"PHP が MySql データベースにアクセスする - 初級",
"link"=>"http://blog.csdn.net/morewindows/article/details/7102362"
)、
「2番目の記事」 => array(
"title"=>"PHP Access MySql データベース中間 Smarty テクノロジー",
"link"=>"http://blog.csdn.net/morewindows/article/details/7094642"
)、
「パート 3」 => array(
"title"=>"PHP Access MySql データベースの高度な AJAX テクノロジ",
"link"=>"http://blog.csdn.net/morewindows/article/details/7086524"
)、
);
$dom = 新しい DOMDocument('1.0', 'UTF-8');
$dom->formatOutput = true;
$rootelement = $dom->createElement("MoreWindows");
foreach ($article_array as $key=>$value)
{
$article = $dom->createElement("article", $key);
$title = $dom->createElement("title", $value['title']);
$link = $dom->createElement("link", $value['link']);
$article->appendChild($title);
$article->appendChild($link);
$rootelement->appendChild($article);
}
$dom->appendChild($rootelement);
$filename = "D:test.xml";
echo 'XML ファイル サイズ' . $dom->save($filename) .
?>
この PHP を実行すると、D ドライブに test.xml ファイルが生成されます (Win7 + XAMPP + IE9.0 テストに合格しました)
例として、前の記事で生成された D:test.xml を読んでみましょう:
[php] //XML ファイルを読み込みます
// 投稿者 MoreWindows( http://blog.csdn.net/MoreWindows )
$ファイル名 = "D:test.xml";
$article_array = 配列();
$dom = 新しい DOMDocument('1.0', 'UTF-8');
$dom->load($ファイル名);
//
foreach ($articles を $article として)
{
$id = $article->getElementsByTagName("id")->item(0)->nodeValue;
$title = $article->getElementsByTagName("title")->item(0)->nodeValue;
$link = $article->getElementsByTagName("link")->item(0)->nodeValue;
$article_array[$id] = array('title'=>$title, 'link'=>$link);
}
//結果を出力する
エコー "";
var_dump($article_array) <br>
エコー "
";
?>
//XMLファイルを読み込みます
// 投稿者 MoreWindows( http://blog.csdn.net/MoreWindows )
$filename = "D:test.xml";
$article_array = array();
$dom = 新しい DOMDocument('1.0', 'UTF-8');
$dom->load($filename);
//
;
$articles = $dom->getElementsByTagName("article");
echo '
foreach ($articles を $article として)
{
$id = $article->getElementsByTagName("id")->item(0)->nodeValue;
$title = $article->getElementsByTagName("title")->item(0)->nodeValue;
$link = $article->getElementsByTagName("link")->item(0)->nodeValue;
$article_array[$id] = array('title'=>$title, 'link'=>$link);
//結果を出力する
echo "
";<br> var_dump($article_array); echo "";
?>
http://www.bkjia.com/PHPjc/371866.html
www.bkjia.com
true
http://www.bkjia.com/PHPjc/371866.html
技術記事
次の行がよく使用されます: header(content-type:text/html; charset=utf-8); //UTF-8 エンコーディングを使用するように PHP を指定します $xml = simplexml_load_file(example.xml); //xml ファイルを読み取りますnewxml = $xml-asXML(...

tomakephpapplicationsfaster、followthesesteps:1)useopcodecachinglikeopcacheTostoredscriptbytecode.2)最小化abasequeriesecachingingindexing.3)leveragephp7機能forbettercodeefficiency.4)

依存性注入(DI)は、明示的に推移的な依存関係によりPHPコードのテスト可能性を大幅に改善します。 1)DI分離クラスと特定の実装により、テストとメンテナンスが柔軟になります。 2)3つのタイプのうち、コンストラクターは、状態を一貫性に保つために明示的な式依存性を注入します。 3)DIコンテナを使用して複雑な依存関係を管理し、コードの品質と開発効率を向上させます。

DatabaseQueryoptimizationInpholvesseveralstrategESTOEnhancePerformance.1)selectonlynlynlyndorycolumnStoredatedataTransfer.2)useindexingtospeedupdataretrieval.3)revenmecrycachingtostoreres sultsoffrequent queries.4)

phpisusededemingemailsduetoitsbuilt-inmail()functionandsupportiveLibrarieslikephpmailerandswiftmailer.1)usethemail()functionforbasicemails、butithaslimitations.2)emploadforadvancedfeatureSlikelikelivableabableabuses.3)雇用

PHPパフォーマンスボトルネックは、次の手順で解決できます。1)パフォーマンス分析にXdebugまたはBlackfireを使用して問題を見つける。 2)データベースクエリを最適化し、APCUなどのキャッシュを使用します。 3)array_filterなどの効率的な関数を使用して、配列操作を最適化します。 4)bytecodeキャッシュ用のopcacheを構成します。 5)HTTP要求の削減や写真の最適化など、フロントエンドを最適化します。 6)パフォーマンスを継続的に監視および最適化します。これらの方法により、PHPアプリケーションのパフォーマンスを大幅に改善できます。

依存関係(di)inphpisadesignpatternativats anducesclassodulencies、拡張測定性、テスト可能性、および維持可能性。

cachingemprovesppperformancebystring of computationsorquickretrieval、還元装置の削減は、reducingerloadendenhancersponseTimes.efcectivestrategiesInclude:1)opcodecaching、compiledphpscriptsinmemorytoskipcompilation;


ホットAIツール

Undresser.AI Undress
リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover
写真から衣服を削除するオンライン AI ツール。

Undress AI Tool
脱衣画像を無料で

Clothoff.io
AI衣類リムーバー

Video Face Swap
完全無料の AI 顔交換ツールを使用して、あらゆるビデオの顔を簡単に交換できます。

人気の記事

ホットツール

SublimeText3 Linux 新バージョン
SublimeText3 Linux 最新バージョン

ゼンドスタジオ 13.0.1
強力な PHP 統合開発環境

WebStorm Mac版
便利なJavaScript開発ツール

MinGW - Minimalist GNU for Windows
このプロジェクトは osdn.net/projects/mingw に移行中です。引き続きそこでフォローしていただけます。 MinGW: GNU Compiler Collection (GCC) のネイティブ Windows ポートであり、ネイティブ Windows アプリケーションを構築するための自由に配布可能なインポート ライブラリとヘッダー ファイルであり、C99 機能をサポートする MSVC ランタイムの拡張機能が含まれています。すべての MinGW ソフトウェアは 64 ビット Windows プラットフォームで実行できます。

ドリームウィーバー CS6
ビジュアル Web 開発ツール
