PHP XML リーダー

王林
リリース: 2024-08-29 13:09:47
オリジナル
234 人が閲覧しました

PHP では、XML リーダー拡張機能により、XML リーダーと呼ばれる XML を解析する技術が提供されます。このプル パーサーまたはストリーム ベースの XML パーサーを使用すると、XML ドキュメントの特定の部分を読み取って取得できる XML パーサーを作成できます。 XML リーダーを使用すると、名前、名前空間、またはインデックスに基づいた属性の取得、属性名、名前空間、またはインデックスを使用した要素の解析、内部レベルに移動せずに要素を解析、現在のノードの値の取得、追加のプロパティの設定などのさまざまな操作が可能になります。 XML パーサーに送信し、XML ドキュメントを検証します。

広告 このカテゴリーの人気コース XML - 専門分野 | 11 コースシリーズ

無料ソフトウェア開発コースを始めましょう

Web 開発、プログラミング言語、ソフトウェア テスト、その他

構文:

XML リーダーを宣言する構文は次のとおりです:

XMLReader();
ログイン後にコピー

XML リーダーの動作

XML リーダーの動作は次のとおりです:

  • XML リーダーを使用すると、現在のノードに基づいて XML ドキュメントの特定の部分を取得できます。
  • XML の属性は、XML Reader を使用して名前、名前空間、またはインデックスを指定することで取得できます。
    同様に、XML 内の要素は、属性の名前、名前空間、またはインデックスを指定することにより、XML Reader を使用して解析できます。
  • XML Reader を使用する利点の 1 つは、内部レベルに移動せずに要素を解析できることです。
  • XML リーダーを使用して現在のノードの値を取得すると、XML ドキュメント内のコンテンツにアクセスできます。
  • XML リーダーでは、XML パーサーに追加のプロパティを設定することもでき、柔軟性とカスタマイズ オプションが提供されます。
  • 最後に、XML Reader は XML ドキュメントの検証をサポートしており、ドキュメントが特定のスキーマまたは構造に準拠していることを確認できます。

PHP XML リーダーの例

以下に例を示します:

例 #1

PHP の XML Reader を使用して XML ドキュメントを解析し、XML ドキュメントのコンテンツを取得する PHP プログラム:

コード:

<?php
//creating an XML documents that is to be parsed using XML reader to retrieve the contents
$xmlDocument = '<?xml version="1.0"?>
<books>
<book ID="1">
<bookname>The Cobra</bookname>
<genre>Thriller</genre>
</book>
<book ID="2">
<bookname>The Killer</bookname>
<genre>Suspense</genre>
</book>
<book ID="3">
<bookname>The Popoye</bookname>
<genre>Comedy</genre>
</book>
</books>';
//declaring an instance of XML Reader
$xml = new XMLReader();
$xml->XML($xmlDocument);
//parsing the contents of the XML document and retrieving the required contents from the document
echo "The details of the books retrieved from the XML documents are:";
while( $xml->read() )
{
if($xml->name == "book")
{
print "Book ID:" . $xml->getAttribute("ID") . "<br/>";
print $xml->readInnerXML() . "<br/>";
$xml->next();
}
}
?>
ログイン後にコピー

出力:

PHP XML リーダー

このプログラムの目的は、XML リーダーを使用して XML ドキュメントを解析し、特定のコンテンツを抽出して取得することです。最初のステップでは、XML ドキュメントの読み取りと解析を処理する XML リーダーのインスタンスを作成します。その後、XML ドキュメントは解析のために XML リーダーに供給されます。次に、XML リーダーはドキュメントを横断して、さまざまな要素や属性へのアクセスを提供します。

例 #2

PHP の XML Reader を使用して XML ドキュメントを解析し、XML ドキュメントのコンテンツを取得する PHP プログラム:

コード:

<?php
//creating an XML documents that is to be parsed using XML reader to retrieve the contents
$xmlDocument = '<?xml version="1.0"?>
<capital>
<country ID="1">
<countryname>India</countryname>
<capital>New Delhi</capital>
</country>
<country ID="2">
<countryname>Nepal</countryname>
<capital>Katmandu</capital>
</country>
<country ID="3">
<countryname>SriLanka</countryname>
<capital>Columbo</capital>
</country>
<country ID="4">
<countryname>Bangladesh</countryname>
<capital>Dhaka</capital>
</country>
<country ID="5">
<countryname>Pakisthan</countryname>
<capital>Islamabad</capital>
</country>
</capital>';
//declaring an instance of XML Reader
$xml = new XMLReader();
$xml->XML($xmlDocument);
//parsing the contents of the XML document and retrieving the required contents from the document
echo "The details of the capital cities retrieved from the XML document are:";
while( $xml->read() )
{
if($xml->name == "country")
{
print "Country code:" . $xml->getAttribute("ID") . "<br/>";
print $xml->readInnerXML() . "<br/>";
$xml->next();
}
}
?>
ログイン後にコピー

出力:

PHP XML リーダー

例 #3

PHP の XML Reader を使用して XML ドキュメントを解析し、XML ドキュメントのコンテンツを取得する PHP プログラム:

コード:

<?php
//creating an XML documents that is to be parsed using XML reader to retrieve the contents
$xmlDocument = '<?xml version="1.0"?>
<socialnetworking>
<website ID="1">
<websitename>Facebook</websitename>
<address>www.facebook.com</address>
</website>
<website ID="2">
<websitename>Instagram</websitename>
<address>www.instagram.com</address>
</website>
<website ID="3">
<websitename>Twitter</websitename>
<address>www.twitter.com</address>
</website>
<website ID="4">
<websitename>Youtube</websitename>
<address>www.youtube.com</address>
</website>
<website ID="5">
<websitename>Orkut</websitename>
<address>www.orkut.com</address>
</website>
</socialnetworking>';
//declaring an instance of XML Reader
$xml = new XMLReader();
$xml->XML($xmlDocument);
//parsing the contents of the XML document and retrieving the required contents from the document
echo "The details of the social networking sites retrieved from the XML document are:";
while( $xml->read() )
{
if($xml->name == "webiste")
{
print "Webiste address:" . $xml->getAttribute("address") . "<br/>";
print $xml->readInnerXML() . "<br/>";
$xml->next();
}
}
?>
ログイン後にコピー

出力:

PHP XML リーダー

結論

この記事では、プログラミング例とその出力を通じて、XML ドキュメントの内容を解析し、PHP での XML リーダーの定義、構文、動作を通じて XML ドキュメントの内容を取得する XML リーダーの概念を学びました。

以上がPHP XML リーダーの詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

関連ラベル:
php
ソース:php
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
最新の問題
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!