Updated April 12, 2023
Definition of PHP XMLWriter
XML writer is used to create files or streams of data in PHP. If you want to store XML contain or want to generate any file which can contain XML data then we should go for XML writer in PHP. XML writer contains several methods to deal with XML content in PHP, we can also add our HTML data inside it. XML writer provides a various method which can be used to create document, start of attribute, end of the attribute, and so on. In the coming section, we will discuss more the XML writer and its methods that can be used to deal with XML content.
Syntax
As XML writer have so many methods available which can be used to create document, attribute, and so many things. But we have to create the object of the XML Writer after this we can call its methods to create XML content. Let’s see its syntax in detail to understand it better see below;
$mywriter = new XMLWriter();
$mywriter->anymethod('output');
In the above lines of syntax as we can see we are creating the XML Writer object after this we can call any method to deal with XML. In the coming section, we will see a practice example to understand it better.
How does XML writer work in PHP?
As now we know that XML writer is the class which provides so many methods that can be used to deal with the XML. By the use of this, we can generate a stream of data or files which contain the XML content. Inside this, we can also have our HTML attributes and tags also. In this section we will discuss more the methods that are available how to use them while programming and their usage in detail with their methods signature as well see below;
Methods
Some of methods which are available are described below;
- 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
<?php
$mywriter = new XMLWriter();
$mywriter->openURI('php://output');
$mywriter->startDocument('1.0', 'UTF-8');
$mywriter->endDocument();
?>
Output:
Conclusion
By the use of XMLWriter we can create XML content also it provides us a various methods that we have already discussed, in order to use them we required an XMLWriter object to call them. We can create comments, attribute,s, and tags by using these methods.
Recommended Articles
This is a guide to PHP XMLWriter. Here we discuss the definition of PHP XMLWriter, How does XML writer work in PHP? along with examples respectively. You may also have a look at the following articles to learn more –