>  기사  >  백엔드 개발  >  PHP XMLWriter

PHP XMLWriter

WBOY
WBOY원래의
2024-08-29 13:09:43268검색

XML 작성기는 PHP에서 파일이나 데이터 스트림을 만드는 데 사용됩니다. XML을 저장하고 싶거나 XML 데이터를 포함할 수 있는 파일을 생성하려면 PHP에서 XML 작성기를 사용해야 합니다. XML 작성기에는 PHP에서 XML 콘텐츠를 처리하는 여러 가지 방법이 포함되어 있으며 그 안에 HTML 데이터를 추가할 수도 있습니다. XML 작성기는 문서 작성, 속성의 시작, 속성의 끝 등을 작성하는 데 사용할 수 있는 다양한 방법을 제공합니다. 다음 섹션에서는 XML 콘텐츠를 처리하는 데 사용할 수 있는 XML 작성기 및 해당 방법에 대해 자세히 설명합니다.

광고 이 카테고리에서 인기 있는 강좌 PHP 개발자 - 전문 분야 | 8개 코스 시리즈 | 3가지 모의고사

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

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

구문

XML 작성자는 문서, 속성 등을 생성하는 데 사용할 수 있는 방법이 너무 많습니다. 하지만 XML 콘텐츠를 생성하기 위해 메서드를 호출할 수 있는 후에는 XML Writer의 객체를 생성해야 합니다. 아래에서 더 잘 이해하기 위해 구문을 자세히 살펴보겠습니다.

$mywriter = new XMLWriter();
$mywriter->anymethod('output');

위 구문에서 볼 수 있듯이 XML Writer 개체를 생성한 후 XML을 처리하기 위해 모든 메서드를 호출할 수 있습니다. 다음 섹션에서는 더 잘 이해할 수 있도록 연습 예를 살펴보겠습니다.

PHP에서 XML 작성기는 어떻게 작동하나요?

이제 우리는 XML 작성기가 XML을 처리하는 데 사용할 수 있는 수많은 메소드를 제공하는 클래스라는 것을 알고 있습니다. 이를 사용하여 XML 콘텐츠가 포함된 데이터 또는 파일 스트림을 생성할 수 있습니다. 이 안에는 HTML 속성과 태그도 있을 수 있습니다. 이 섹션에서는 프로그래밍하는 동안 사용할 수 있는 메서드와 해당 메서드 서명과 함께 사용법에 대해 자세히 설명합니다.

방법

사용 가능한 몇 가지 방법은 다음과 같습니다.

  • XMLWriter::writeElementNs — This method is used to create the write full namespaced element.
  • XMLWriter::writeElement — This method is used to create the write full element.
  • XMLWriter::writePi — This method is used to create the writes PI
  • XMLWriter::writeRaw — This method is used to create the write raw XML text
  • XMLWriter::startCdata — This method is used to create the CDATA tag
  • XMLWriter::startComment — This method is used to create the comment
  • XMLWriter::startDocument — This method is used to create the document tag
  • XMLWriter::startDtdAttlist — This method is used to create the DTD AttList
  • XMLWriter::startDtdElement — This method is used to create the DTD element
  • XMLWriter::startDtdEntity — This method is used to create the DTD Entity
  • XMLWriter::writeAttribute — This method is used to create the write full attribute
  • XMLWriter::writeCdata — This method is used to create the write full CDATA tag in XMLWriter.
  • XMLWriter::writeComment — This method is used to create the write full comment tag in XMLWriter.
  • XMLWriter::writeDtdAttlist — This method is used to create the write full DTD AttList tag in XMLWriter.
  • XMLWriter::writeDtdElement — This method is used to create the write full DTD element tag in XMLWriter.
  • XMLWriter::writeDtdEntity — This method is used to create the write full DTD Entity.
  • XMLWriter::endAttribute — This method is used to end the attribute after the start.
  • XMLWriter::endCdata — By the use of this we can end the current CDATA in XMLWriter.
  • XMLWriter::endComment — This method is used to create the comment but the end comment.
  • XMLWriter::endDocument — This method is used to end the document but this document would be a current document.
  • XMLWriter::endDtdAttlist — This method is used to end the current DTD AttList (may be attricute list)
  • XMLWriter::endDtdElement — This method is used to end the current DTD element
  • XMLWriter::endDtdEntity — This method is used to end the current DTD Entity
  • XMLWriter::endDtd — this method is used to end the current DTD
  • XMLWriter::endElement — This method ends the current elements of XMLWriter.
  • XMLWriter::endPi — This methd is used to end current PI
  • XMLWriter::flush — This method is used to flush the buffer.
  • XMLWriter::fullEndElement — This method is also used to end the current element.
  • XMLWriter::openMemory — This method is sued to create xmlwriter.
  • XMLWriter::startDtd — This method is used to create the DTD in XMLWriter.
  • XMLWriter::startElementNs — This method is used to create the namespaced element tag in XMLWriter.
  • XMLWriter::startElement — This method is used to create the element tag
  • XMLWriter::startPi — This method is used to create the PI tag in XMLWriter.
  • XMLWriter::text — This method is used to create the write text.
  • XMLWriter::writeAttributeNs — This method is used to create the write full namespaced attribute
  • XMLWriter::openUri — This method is used to create xmlwriter using the specified source uri.
  • XMLWriter::outputMemory — This method is used to return the current buffer
  • XMLWriter::setIndentString — This method is used t set the string for indenting
  • XMLWriter::setIndent — This method is used to toggle the indentation (on/off)
  • XMLWriter::startAttributeNs — This method is used to create the start name spaced attribte.
  • XMLWriter::startAttribute — This method is used to create the start attribute.
  • XMLWriter::writeDtd — This method is used to create the write full DTD.

Let’s take one example to understand it better see below;

  • First, create the XML Writer object.
  • 2) After that we can call any method on the given object. We have already discussed the methods and their work in detail.
  • To create XML document we can call start document method and inside this, we can specify the XML version number and the encoding type which will be the first line available in the output.
  • Below see the sample to begin with XML writer in PHP see below;

e.g. :

$mywriter->startDocument('1.0', 'UTF-8');

These are the required parameter that need to pass while dealing with XML writer.

Example of PHP XMLWriter

In this example, we are creating one document. We have two methods from XMLWriter to create the document. First, we have start the document and at the end, we are closing it. Make sure you have the proper setup to run the program otherwise it won’t run.

Example #1

openURI('php://output');
$mywriter->startDocument('1.0', 'UTF-8');
$mywriter->endDocument();
?>

Output:

PHP XMLWriter

결론

XMLWriter를 사용하면 XML 콘텐츠를 만들 수 있으며 이미 논의한 다양한 메서드를 제공합니다. 이를 사용하려면 해당 메서드를 호출하는 XMLWriter 개체가 필요했습니다. 이러한 방법을 사용하여 주석, 속성, 태그를 생성할 수 있습니다.

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

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
이전 기사:PHP json_디코드다음 기사:PHP json_디코드