当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


PHP xmlwriter_write_dtd()用法及代码示例



定义和用法

XML 是一种 mark-up 语言,用于在网络上共享数据,XML 用于人类 read-able 和机器 read-able。 XMLWriter 扩展内部具有 libxml xmlWriter API,用于编写/创建 XML 文档的内容。由此生成的 XML 文档是非缓存的和 forward-only。

这个xmlwriter_write_dtd()函数用于创建/写入完整的 DTD 标签。

用法

xmlwriter_start_dtd($writer, $name, $public_id, $system_id, $subset);

参数

Sr.No 参数及说明
1

writer(Mandatory)

这是 XMLWriter 类的一个对象,表示您要修改/创建的 XML 文档。

2

name(Mandatory)

这是一个字符串值,表示文档类型的限定名称。

3

public_id(Optional)

这是一个字符串值,表示外部子集公共标识符。

4

system_id(Optional)

这是一个表示外部子集系统标识符的字符串值。

5

subset(Optional)

这是一个字符串值,表示 DTD 标记的内容。

返回值

此函数返回一个布尔值,成功时为 TRUE,失败时为 FALSE。

PHP版本

这个函数最初是在 PHP 版本 5 中引入的,并且适用于所有后续版本。

示例

下面的例子演示了xmlwriter_start_dtd()函数 -

<?php
   //Opening a writer
   $uri = "result.xml";
   $writer = xmlwriter_open_uri($uri);

   //Starting the document
   xmlwriter_start_document($writer);

   //Setting the indentation
   xmlwriter_set_indent($writer, TRUE);
   xmlwriter_set_indent_string($writer, "    ");

   $dtd = "<!DOCTYPE address [
      <!ELEMENT address (name,company,phone)>
      <!ELEMENT name (#PCDATA)>
      <!ELEMENT company (#PCDATA)>
      <!ELEMENT phone (#PCDATA)>
   ]>";

   //Creating a DTD tag
   xmlwriter_write_dtd($writer, 'test_dtd', 'pub_1001', 'sys_1001' , $dtd);

   //Starting an element
   xmlwriter_start_element($writer, 'address');

   //Starting a element tag
   xmlwriter_start_element($writer, 'name');
   
   //Adding text to the element
   xmlwriter_text($writer, 'Tanmay Patil');  
   xmlwriter_end_element($writer);
   xmlwriter_start_element($writer, 'company');
   
   //Adding text to the element
   xmlwriter_text($writer, 'TutorialsPoint');  
   xmlwriter_end_element($writer);
   xmlwriter_start_element($writer, 'phone');
   
   //Adding text to the element
   xmlwriter_text($writer, '(011) 123-4567');  
   xmlwriter_end_element($writer);

   //Ending the element
   xmlwriter_end_element($writer);

   //Ending the document
   xmlwriter_end_document($writer);
?>

这将生成以下 XML 文档 -

<?xml version="1.0"?>
<!DOCTYPE test_dtd
PUBLIC "pub_1001" "sys_1001" [<!DOCTYPE address [
   <!ELEMENT address (name,company,phone)>
   <!ELEMENT name (#PCDATA)>
   <!ELEMENT company (#PCDATA)>
   <!ELEMENT phone (#PCDATA)>
]>]>
<address>
   <name>Tanmay Patil</name>
   <company>TutorialsPoint</company>
   <phone>(011) 123-4567</phone>
</address>

示例

以下是面向对象风格的此函数的示例 -

<?php
   //Creating an XMLWriter
   $writer = new XMLWriter();

   //Opening a writer
   $uri = "result.xml";
   $writer->openUri($uri);

   //Starting the document
   $writer->startDocument();

   //Setting the indentation on
   $writer->setIndent(TRUE);
   
   //Setting the indentation
   $writer->setIndentString("    ");
   $dtd = "<!DOCTYPE address [
      <!ELEMENT address (name,company,phone)>
      <!ELEMENT name (#PCDATA)>
      <!ELEMENT company (#PCDATA)>
      <!ELEMENT phone (#PCDATA)>
   ]>";

   //Creating a DTD tag
   $writer->writeDtd('test_dtd', 'pub_1001', 'sys_1001' , $dtd);

   //Starting an element
   $writer->startElement(address);
   $writer->startElement('name');
   
   //Adding text to the element
   $writer->text('Tanmay Patil');  
   $writer->endElement();
   $writer->startElement('company');
   
   //Adding text to the element
   $writer->text('TutorialsPoint');  
   $writer->endElement();
   $writer->startElement('phone');
   
   //Adding text to the element
   $writer->text('(011) 123-4567');  
   $writer->endElement();

   //Ending the element
   $writer->endElement();
   
   //Ending the document
   $writer->endDocument();
?>

这将生成以下 XML 文档 -

<?xml version="1.0"?>
<!DOCTYPE test_dtd
PUBLIC "pub_1001" "sys_1001" [<!DOCTYPE address [
   <!ELEMENT address (name,company,phone)>
   <!ELEMENT name (#PCDATA)>
   <!ELEMENT company (#PCDATA)>
   <!ELEMENT phone (#PCDATA)>
]>]>
<address>
   <name>Tanmay Patil</name>
   <company>TutorialsPoint</company>
   <phone>(011) 123-4567</phone>
</address>

相关用法


注:本文由纯净天空筛选整理自 PHP - xmlwriter_write_dtd() Function。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。