在 simplexml 中创建 XML (php)
我尝试使用下面的代码创建一个 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());
但是当我尝试验证它时,我收到以下警告:
- 未找到 DOCTYPE!仅检查 XML 语法。
- 没有在文档级别声明字符编码。
如何清除它们? 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
- No DOCTYPE found! Checking XML syntax only.
- No Character encoding declared at document level.
How to clear them? The XML is here and the validator used is validator.w3.org.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
尽管这些警告您可能会忽略,但尝试修复它们确实是个好主意。
第一条消息表明 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.
您可以决定忽略这些警告。 XML 中不需要 DOCTYPE 或声明的字符编码。实际上,大多数 XML 文档不包含 DOCTYPE。
DOCTYPE 用于文档验证,这意味着存在对文档可以存在哪些元素和属性的正式描述,并且可以检查您的文档是否不包含任何其他内容(在这种情况下将变得无效)。除非您关心这一点并准备开发、托管和维护您自己的 DOCTYPE 规范,否则请忽略该警告。
大多数时候,XML 的创建者和消费者关心格式良好,这意味着文档遵守 XML 需要的外观规则(没有交叉嵌套的标签,所有标签都必须闭合) ,正确转义的内容)。如果做不到这一点,你的文档就会被破坏——但这里的情况并非如此。
通过在文档顶部包含一个表示编码的 XML 声明来修复字符编码警告:
如果不包含它,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:
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.
您可以通过以下方式简单地创建 XML 根元素:
这样您将获得下一个有效的 XML:
You can simply create the XML root element the next way:
This way you'll get the next valid XML:
此代码将创建一个名为 test 的 xml 文件
This code will create a xml file with the name test