mirror of
https://github.com/php/doc-ja.git
synced 2026-03-30 11:02:18 +02:00
git-svn-id: https://svn.php.net/repository/phpdoc/ja/trunk@195169 c90b9560-bf6c-de11-be94-00142212c4b1
281 lines
7.8 KiB
XML
281 lines
7.8 KiB
XML
<?xml version='1.0' encoding='utf-8'?>
|
||
<!-- $Revision: 1.3 $ -->
|
||
<!-- EN-Revision: 1.17 Maintainer: hirokawa Status: ready -->
|
||
<!-- CREDITS: shimooka -->
|
||
<!-- Purpose: xml -->
|
||
<!-- Membership: bundled, external -->
|
||
|
||
<reference id="ref.simplexml">
|
||
<title>SimpleXML関数</title>
|
||
<titleabbrev>SimpleXML</titleabbrev>
|
||
|
||
<partintro>
|
||
<section id="simplexml.intro">
|
||
&reftitle.intro;
|
||
<para>
|
||
SimpleXML拡張モジュールは、
|
||
XMLをオブジェクトにとても簡単かつ容易に変換するための機能を
|
||
提供します。変換後のオブジェクトでは、
|
||
通常のプロパティセレクタや配列反復子を用いて処理を行うことが
|
||
可能です。
|
||
</para>
|
||
</section>
|
||
|
||
<section id="simplexml.requirements">
|
||
&reftitle.required;
|
||
<para>
|
||
SimpleXML 拡張モジュールは PHP 5 が必要になります。
|
||
</para>
|
||
</section>
|
||
|
||
&reference.simplexml.configure;
|
||
|
||
<section id="simplexml.examples">
|
||
&reftitle.examples;
|
||
<para>
|
||
このリファレンスの多くの例ではXML文字列を必要とします。各例で
|
||
この文字列をくり返す代わりに、あるファイルにこの文字列を保存して、
|
||
各例で読みこむことにします。この読みこまれるファイルは、以下の例
|
||
に関するセクションで使用されます。
|
||
もしくは、XMLドキュメントを作成し、
|
||
<function>simplexml_load_file</function> により読みこむことも
|
||
可能です。
|
||
</para>
|
||
<para>
|
||
<example>
|
||
<title>XML文字列を設定するインクルードファイル example.php</title>
|
||
<programlisting role="php" id="simplexml.examples.movie">
|
||
<![CDATA[
|
||
<?php
|
||
$xmlstr = <<<XML
|
||
<?xml version='1.0' standalone='yes'?>
|
||
<movies>
|
||
<movie>
|
||
<title>PHP: Behind the Parser</title>
|
||
<characters>
|
||
<character>
|
||
<name>Ms. Coder</name>
|
||
<actor>Onlivia Actora</actor>
|
||
</character>
|
||
<character>
|
||
<name>Mr. Coder</name>
|
||
<actor>El ActÓr</actor>
|
||
</character>
|
||
</characters>
|
||
<plot>
|
||
So, this language. It's like, a programming language. Or is it a
|
||
scripting language? All is revealed in this thrilling horror spoof
|
||
of a documentary.
|
||
</plot>
|
||
<rating type="thumbs">7</rating>
|
||
<rating type="stars">5</rating>
|
||
</movie>
|
||
</movies>
|
||
XML;
|
||
?>
|
||
]]>
|
||
</programlisting>
|
||
</example>
|
||
</para>
|
||
<para>
|
||
SimpleXMLの容易さが最も明確に現われるのは、
|
||
簡単なXMLドキュメントから文字列または数字を展開する時です。
|
||
<example>
|
||
<title><literal><plot></literal> を取得する</title>
|
||
<programlisting role="php"><![CDATA[
|
||
<?php
|
||
include 'example.php';
|
||
|
||
$xml = simplexml_load_string($xmlstr);
|
||
|
||
echo $xml->movie[0]->plot; // "So this language. It's like..."
|
||
?>
|
||
]]>
|
||
</programlisting>
|
||
</example>
|
||
</para>
|
||
<para>
|
||
<example>
|
||
<title>SimpleXMLでユニークでない要素にアクセスする</title>
|
||
<simpara>
|
||
単一の親要素の子要素としてある要素のインスタンスが複数存在する時、
|
||
通常の反復処理を適用することができます。
|
||
</simpara>
|
||
<programlisting role="php"><![CDATA[
|
||
<?php
|
||
include 'example.php';
|
||
|
||
$xml = simplexml_load_string($xmlstr);
|
||
|
||
/* For each <movie> node, we echo a separate <plot>. */
|
||
foreach ($xml->movie as $movie) {
|
||
echo $movie->plot, '<br />';
|
||
}
|
||
|
||
?>
|
||
]]>
|
||
</programlisting>
|
||
</example>
|
||
</para>
|
||
<para>
|
||
<example>
|
||
<title>属性を使用する</title>
|
||
<simpara>
|
||
ここまでは、要素の名前と値を読む方法のみを扱って来ました。
|
||
SimpleXMLは要素の属性にアクセスすることも可能です。
|
||
要素の属性にアクセスする方法は、<type>配列</type> の要素に
|
||
アクセスするのと全く同じです。
|
||
</simpara>
|
||
<programlisting role="php">
|
||
<![CDATA[
|
||
<?php
|
||
include 'example.php';
|
||
|
||
$xml = simplexml_load_string($xmlstr);
|
||
|
||
/* Access the <rating> nodes of the first movie.
|
||
* Output the rating scale, too. */
|
||
foreach ($xml->movie[0]->rating as $rating) {
|
||
switch((string) $rating['type']) { // Get attributes as element indices
|
||
case 'thumbs':
|
||
echo $rating, ' thumbs up';
|
||
break;
|
||
case 'stars':
|
||
echo $rating, ' stars';
|
||
break;
|
||
}
|
||
}
|
||
?>
|
||
]]>
|
||
</programlisting>
|
||
</example>
|
||
</para>
|
||
<para>
|
||
<example>
|
||
<title>要素および属性をテキストと比較する</title>
|
||
<simpara>
|
||
要素または属性を文字列と比較する、もしくは、文字列を引数とする関数に
|
||
渡すには、<literal>(string)</literal> により文字列にキャストする
|
||
必要があります。さもないと、PHPはこの要素をオブジェクトとして扱います。
|
||
</simpara>
|
||
<programlisting role="php"><![CDATA[
|
||
<?php
|
||
include 'example.php';
|
||
|
||
$xml = simplexml_load_string($xmlstr);
|
||
|
||
if ((string) $xml->movie->title == 'PHP: Behind the Parser') {
|
||
print 'My favorite movie.';
|
||
}
|
||
|
||
htmlentities((string) $xml->movie->title);
|
||
?>
|
||
]]>
|
||
</programlisting>
|
||
</example>
|
||
</para>
|
||
<para>
|
||
<example>
|
||
<title>Xpathの使用</title>
|
||
<simpara>
|
||
SimpleXMLには、<acronym>Xpath</acronym>をサポートしています。
|
||
<literal><character></literal> 要素を全て見つけるには、
|
||
以下のようにします。
|
||
</simpara>
|
||
<programlisting role="php">
|
||
<![CDATA[
|
||
<?php
|
||
include 'example.php';
|
||
$xml = simplexml_load_string($xmlstr);
|
||
|
||
foreach ($xml->xpath('//character') as $character) {
|
||
echo $character->name, 'played by ', $character->actor, '<br />';
|
||
}
|
||
?>
|
||
]]>
|
||
</programlisting>
|
||
<simpara>
|
||
'<literal>//</literal>' はワイルドカードとして動作します。絶対パスを指定するには、
|
||
スラッシュを一つだけにします。
|
||
</simpara>
|
||
</example>
|
||
</para>
|
||
<para>
|
||
<example>
|
||
<title>値を設定する</title>
|
||
<simpara>
|
||
SimpleXMLの中のデータは、定数とすることができません。
|
||
オブジェクトは、その全ての要素について変更が可能です。
|
||
</simpara>
|
||
<programlisting role="php">
|
||
<![CDATA[
|
||
<?php
|
||
include 'example.php';
|
||
$xml = simplexml_load_string($xmlstr);
|
||
|
||
$xml->movie[0]->characters->character[0]->name = 'Miss Coder';
|
||
|
||
echo $xml->asXML();
|
||
?>
|
||
]]>
|
||
</programlisting>
|
||
<simpara>
|
||
上のコードは、元のXMLドキュメントと全く同じXMLドキュメントを新規に
|
||
出力しますが、新しいXMLファイルでは、Ms. Coder が Miss Coder
|
||
に変更されているところが異なります。
|
||
</simpara>
|
||
</example>
|
||
</para>
|
||
<para>
|
||
<example>
|
||
<title>DOMとの相互運用性</title>
|
||
<simpara>
|
||
PHPは、SimpleXML形式とDOM形式の間でXMLノードを変換する機構を有しています。
|
||
この例では、DOM要素をSimpleXMLに変換することができます。
|
||
</simpara>
|
||
<programlisting role="php">
|
||
<![CDATA[
|
||
<?php
|
||
$dom = new domDocument;
|
||
$dom->loadXML('<books><book><title>blah</title></book></books>');
|
||
if (!$dom) {
|
||
echo 'Error while parsing the document';
|
||
exit;
|
||
}
|
||
|
||
$s = simplexml_import_dom($dom);
|
||
|
||
echo $s->book[0]->title;
|
||
?>
|
||
]]>
|
||
</programlisting>
|
||
</example>
|
||
</para>
|
||
</section>
|
||
</partintro>
|
||
|
||
&reference.simplexml.functions;
|
||
|
||
</reference>
|
||
<!-- Keep this comment at the end of the file
|
||
Local variables:
|
||
mode: sgml
|
||
sgml-omittag:t
|
||
sgml-shorttag:t
|
||
sgml-minimize-attributes:nil
|
||
sgml-always-quote-attributes:t
|
||
sgml-indent-step:1
|
||
sgml-indent-data:t
|
||
indent-tabs-mode:nil
|
||
sgml-parent-document:nil
|
||
sgml-default-dtd-file:"../../../manual.ced"
|
||
sgml-exposed-tags:nil
|
||
sgml-local-catalogs:nil
|
||
sgml-local-ecat-files:nil
|
||
End:
|
||
vim600: syn=xml fen fdm=syntax fdl=2 si
|
||
vim: et tw=78 syn=sgml
|
||
vi: ts=1 sw=1
|
||
-->
|
||
|