PHP XML 리더

王林
풀어 주다: 2024-08-29 13:09:47
원래의
233명이 탐색했습니다.

PHP에서 XML 리더 확장은 XML 리더라는 XML 구문 분석 기술을 제공합니다. 이 풀 파서 또는 스트림 기반 XML 파서는 XML 문서의 특정 부분을 읽고 검색할 수 있는 XML 파서를 생성할 수 있습니다. XML 판독기를 사용하면 이름, 네임스페이스 또는 인덱스를 기반으로 속성 검색, 속성 이름, 네임스페이스 또는 인덱스를 사용하여 요소 구문 분석, 내부 수준으로 이동하지 않고 요소 구문 분석, 현재 노드 값 가져오기, 추가 속성 설정 등 다양한 작업이 가능합니다. XML 파서에 연결하고 XML 문서의 유효성을 검사합니다.

광고 이 카테고리에서 인기 있는 강좌 XML - 전문화 | 11개 강좌 시리즈

무료 소프트웨어 개발 과정 시작

웹 개발, 프로그래밍 언어, 소프트웨어 테스팅 등

구문:

XML Reader를 선언하는 구문은 다음과 같습니다.

XMLReader();
로그인 후 복사

XML 리더 작동

XML Reader의 작동 원리는 다음과 같습니다.

  • XML 리더를 사용하면 현재 노드를 기반으로 XML 문서의 특정 부분을 검색할 수 있습니다.
  • XML의 속성은 이름, 네임스페이스 또는 인덱스를 지정하여 XML 리더를 사용하여 얻을 수 있습니다.
    마찬가지로 XML의 요소는 속성의 이름, 네임스페이스 또는 인덱스를 지정하여 XML 판독기를 사용하여 구문 분석할 수 있습니다.
  • XML 리더를 사용하면 내부 수준으로 이동하지 않고도 요소를 구문 분석할 수 있다는 장점이 있습니다.
  • XML 리더를 사용하여 현재 노드의 값을 얻을 수 있으므로 XML 문서 내의 콘텐츠에 액세스할 수 있습니다.
  • XML 리더를 사용하면 XML 파서에 추가 속성을 설정할 수 있어 유연성과 사용자 정의 옵션이 제공됩니다.
  • 마지막으로 XML Reader는 XML 문서 유효성 검사를 지원하므로 문서가 특정 스키마나 구조를 준수하는지 확인할 수 있습니다.

PHP XML 리더의 예

아래 예시는 다음과 같습니다.

예시 #1

PHP에서 XML 리더를 사용하여 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 Reader에 제공됩니다. 그런 다음 XML 리더는 문서를 탐색하여 다양한 요소와 속성에 대한 액세스를 제공합니다.

예시 #2

PHP에서 XML 리더를 사용하여 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 리더를 사용하여 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 Reader의 정의, 구문 및 작동을 통해 XML Reader의 개념을 배웠습니다.

위 내용은 PHP XML 리더의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

관련 라벨:
php
원천:php
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
최신 이슈
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!