简体   繁体   English

Simplexml中的XML创建(php)

[英]XML Creation in simplexml (php)

I tried created an XML file using below code. 我尝试使用下面的代码创建了一个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());

But when I try to validate it I am getting below warnings 但是当我尝试验证它时,我会收到警告

  1. No DOCTYPE found! 找不到DOCTYPE! Checking XML syntax only. 仅检查XML语法。
  2. No Character encoding declared at document level. 没有在文档级别声明的字符编码。

How to clear them? 如何清除它们? The XML is here and the validator used is validator.w3.org. XML在这里 ,使用的验证器是validator.w3.org。

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; 第一条消息表明xml文件未指定任何DTD; the second one refers to the fact that the xml directive doesn't contain any encoding attribute. 第二个是指xml指令不包含任何编码属性的事实。

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). 不幸的是,通过查看simplexml API,它看起来似乎不可能添加(虽然我可能会遗漏一些东西,不熟悉它)。 I guess being “simple,” it is perfect for quickly read an xml file, but no so much to produce xml. 我觉得“简单”,它非常适合快速读取xml文件,但没有那么多来生成xml。 You'll probaby have to turn to something slightly “heavier” like XML_Serializer . 你可能需要转向像XML_Serializer那样略显“重”的东西

You could decide to ignore these warnings. 您可以决定忽略这些警告。 Neither a DOCTYPE nor a declared character encoding is required in XML. XML中不需要DOCTYPE或声明的字符编码。 Actually, most XML documents do not contain a DOCTYPE. 实际上,大多数XML文档都不包含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 ). DOCTYPE用于文档验证 ,这意味着正式描述存在文档可以存在的元素和属性,并且可以检查您的文档不包含任何其他内容(在这种情况下它将变为无效 )。 Unless you care for that and are prepared to develop, host and maintain your own DOCTYPE specification, ignore the warning. 除非您关心并准备开发,托管和维护您自己的DOCTYPE规范,否则请忽略该警告。

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). 大多数时候,XML的创建者和消费者都关心格式良好 ,这意味着文档遵循XML需要的规则(没有交叉嵌套标记,所有标记必须关闭,正确转义内容)。 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声明来修复字符编码警告:

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

If you do not include it, XML defaults to "version 1.0" and "utf-8". 如果不包含它,XML默认为“1.0版”和“utf-8”。 If that's what you deliver anyway, all is well. 如果这就是你提供的东西,一切都很顺利。 If you deliver something else, it's necessary to include that info. 如果您提供其他内容,则必须包含该信息。

You can simply create the XML root element the next way: 您可以通过下一种方式简单地创建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();

This way you'll get the next valid XML: 这样您就可以获得下一个有效的XML:

<?xml version="1.0"?>
<!DOCTYPE your_root_xml_element SYSTEM "URL_TO_DTD_SCHEME">
<your_root_xml_element>
</your_root_xml_element>
$xml = new SimpleXMLElement('<?xml version="1.0" encoding="utf-8" ?>');
`$xmlstr = '<?xml version="1.0" encoding="utf-8"?>
<root>
<Data>
    <Id>0475109067</Id>
    <Contact>
        <Name>Daya</Name>
        <FirstName>Star</FirstName>
        <Email>test@test.com</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 此代码将创建名为test的xml文件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM