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:
結論
透過使用 XMLWriter,我們可以建立 XML 內容,它也為我們提供了我們已經討論過的各種方法,為了使用它們,我們需要一個 XMLWriter 物件來呼叫它們。我們可以使用這些方法來建立評論、屬性和標籤。
以上是PHP XMLWriter的詳細內容。更多資訊請關注PHP中文網其他相關文章!