PHP XMLWriter

WBOY
发布: 2024-08-29 13:09:43
原创
417 人浏览过

XML writer 用于在 PHP 中创建文件或数据流。如果您想要存储 XML 内容或想要生成任何可以包含 XML 数据的文件,那么我们应该使用 PHP 中的 XML 编写器。 XML writer 包含了几种在 PHP 中处理 XML 内容的方法,我们也可以在其中添加 HTML 数据。 XML writer 提供了多种方法可用于创建文档、属性开始、属性结束等。在接下来的部分中,我们将更多地讨论 XML 编写器及其可用于处理 XML 内容的方法。

广告 该类别中的热门课程 PHP 开发人员 - 专业化 | 8 门课程系列 | 3次模拟测试

开始您的免费软件开发课程

网络开发、编程语言、软件测试及其他

语法

作为 XML 编写器,有很多可用的方法,可用于创建文档、属性等等。但是我们必须创建 XML Writer 的对象,然后才能调用它的方法来创建 XML 内容。让我们详细看看它的语法,以便更好地理解它,如下所示;

$mywriter = new XMLWriter();
$mywriter->anymethod('output');
登录后复制

在上面的语法行中,我们可以看到我们正在创建 XML Writer 对象,之后我们可以调用任何方法来处理 XML。在接下来的部分中,我们将看到一个练习示例以更好地理解它。

XML 编写器如何在 PHP 中工作?

现在我们知道 XML writer 是一个提供了许多可用于处理 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中文网其他相关文章!

相关标签:
php
来源:php
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板