在 simplexml 中创建 XML (php)

发布于 2024-10-11 22:48:35 字数 657 浏览 6 评论 0原文

我尝试使用下面的代码创建一个 XML 文件。

$xml = new SimpleXMLElement('<xml />');
while($rs = db_fetch_object($d_res)){
 $cmp = $xml->addChild('Company');
 $cmp->addChild('ID', $rs->nid);
 $cmp->addChild('Name', trim($rs->title));
 $cmp->addChild('Created', $rs->created);
 $cmp->addChild('Updated', $rs->changed);
}
header("Content-type: text/xml;charset=utf-8;");
print($xml->asXML());

但是当我尝试验证它时,我收到以下警告:

  1. 未找到 DOCTYPE!仅检查 XML 语法。
  2. 没有在文档级别声明字符编码。

如何清除它们? XML 位于此处,使用的验证器是 validator.w3.org。

I tried created an XML file using below code.

$xml = new SimpleXMLElement('<xml />');
while($rs = db_fetch_object($d_res)){
 $cmp = $xml->addChild('Company');
 $cmp->addChild('ID', $rs->nid);
 $cmp->addChild('Name', trim($rs->title));
 $cmp->addChild('Created', $rs->created);
 $cmp->addChild('Updated', $rs->changed);
}
header("Content-type: text/xml;charset=utf-8;");
print($xml->asXML());

But when I try to validate it I am getting below warnings

  1. No DOCTYPE found! Checking XML syntax only.
  2. No Character encoding declared at document level.

How to clear them? The XML is here and the validator used is validator.w3.org.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

碍人泪离人颜 2024-10-18 22:48:35

尽管这些警告您可能会忽略,但尝试修复它们确实是个好主意。

第一条消息表明 xml 文件没有指定任何 DTD;第二个是指 xml 指令不包含任何编码属性这一事实。

不幸的是,从 simplexml API 来看,看起来不可能添加任何一个(尽管我可能会遗漏一些东西,因为不熟悉它)。我猜想“简单”,它对于快速读取 xml 文件来说是完美的,但对于生成 xml 来说就没有那么多了。您可能必须转向稍微“重一点”的东西,例如 XML_Serializer

Although these are warnings that you could possibly ignore, it is indeed a good idea to try to fix them.

The first message indicates that the xml file doesn’t specify any DTD; the second one refers to the fact that the xml directive doesn’t contain any encoding attribute.

Unfortunately, from looking at the simplexml API, it doesn’t look like it is possible to add either (although I might be missing something, not being familiar with it). I guess being “simple,” it is perfect for quickly read an xml file, but no so much to produce xml. You’ll probaby have to turn to something slightly “heavier” like XML_Serializer.

久隐师 2024-10-18 22:48:35

您可以决定忽略这些警告。 XML 中不需要 DOCTYPE 或声明的字符编码。实际上,大多数 XML 文档不包含 DOCTYPE。

DOCTYPE 用于文档验证,这意味着存在对文档可以存在哪些元素和属性的正式描述,并且可以检查您的文档是否不包含任何其他内容(在这种情况下将变得无效)。除非您关心这一点并准备开发、托管和维护您自己的 DOCTYPE 规范,否则请忽略该警告。

大多数时候,XML 的创建者和消费者关心格式良好,这意味着文档遵守 XML 需要的外观规则(没有交叉嵌套的标签,所有标签都必须闭合) ,正确转义的内容)。如果做不到这一点,你的文档就会被破坏——但这里的情况并非如此。

通过在文档顶部包含一个表示编码的 XML 声明来修复字符编码警告:

<?xml version="1.0" encoding="utf-8" ?>

如果不包含它,XML 默认为“版本 1.0”和“utf-8”。如果这就是你所交付的,那么一切都很好。如果您交付其他东西,则有必要包含该信息。

You could decide to ignore these warnings. Neither a DOCTYPE nor a declared character encoding is required in XML. Actually, most XML documents do not contain a DOCTYPE.

A DOCTYPE is used for document validation, which means that a formal description exists what elements and attributes can exist for your document and a check can be made that your document does not contain anything else (in which case it would become invalid). Unless you care for that and are prepared to develop, host and maintain your own DOCTYPE specification, ignore the warning.

Most of the time, creators and consumers of XML care about well-formedness, which means that the document adheres to the rules how XML needs to look like (no cross-nested tags, all tags must be closed, properly escaped content). Failing that, your document would simply break - but that's not the case here.

The character encoding warning is fixed by including an XML declaration at the top of your document that denotes the encoding:

<?xml version="1.0" encoding="utf-8" ?>

If you do not include it, XML defaults to "version 1.0" and "utf-8". If that's what you deliver anyway, all is well. If you deliver something else, it's necessary to include that info.

沉睡月亮 2024-10-18 22:48:35

您可以通过以下方式简单地创建 XML 根元素:

$rootXml = new SimpleXMLElement("<!DOCTYPE your_root_xml_element SYSTEM 'URL_TO_DTD_SCHEME'><your_root_xml_element></your_root_xml_element>");
....
echo $rootXml->asXML();

这样您将获得下一个有效的 XML:

<?xml version="1.0"?>
<!DOCTYPE your_root_xml_element SYSTEM "URL_TO_DTD_SCHEME">
<your_root_xml_element>
</your_root_xml_element>

You can simply create the XML root element the next way:

$rootXml = new SimpleXMLElement("<!DOCTYPE your_root_xml_element SYSTEM 'URL_TO_DTD_SCHEME'><your_root_xml_element></your_root_xml_element>");
....
echo $rootXml->asXML();

This way you'll get the next valid XML:

<?xml version="1.0"?>
<!DOCTYPE your_root_xml_element SYSTEM "URL_TO_DTD_SCHEME">
<your_root_xml_element>
</your_root_xml_element>
终止放荡 2024-10-18 22:48:35
$xml = new SimpleXMLElement('<?xml version="1.0" encoding="utf-8" ?>');
$xml = new SimpleXMLElement('<?xml version="1.0" encoding="utf-8" ?>');
死开点丶别碍眼 2024-10-18 22:48:35
`$xmlstr = '<?xml version="1.0" encoding="utf-8"?>
<root>
<Data>
    <Id>0475109067</Id>
    <Contact>
        <Name>Daya</Name>
        <FirstName>Star</FirstName>
        <Email>[email protected]</Email>
    </Contact>
</Data>
<Result>
    <Name></Name>
</Result>
</root>';

$xml = new SimpleXMLElement($xmlstr); 
$xml->Result->Name = 'Xmltestfile'; 
$xml->asXML('test.xml');
$xml->asXML();`

此代码将创建一个名为 test 的 xml 文件

`$xmlstr = '<?xml version="1.0" encoding="utf-8"?>
<root>
<Data>
    <Id>0475109067</Id>
    <Contact>
        <Name>Daya</Name>
        <FirstName>Star</FirstName>
        <Email>[email protected]</Email>
    </Contact>
</Data>
<Result>
    <Name></Name>
</Result>
</root>';

$xml = new SimpleXMLElement($xmlstr); 
$xml->Result->Name = 'Xmltestfile'; 
$xml->asXML('test.xml');
$xml->asXML();`

This code will create a xml file with the name test

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文