试图获得这样的输出
<DeclarationFile>
<Declaration Id="DEC">
<DokPVNv4>
<ParskMen>5</ParskMen>
<ParskCeturksnis xsi:nil="true"/>
不能创造<ParskCeturksnis xsi:nil="true"/>
如果只使用
$ParskCeturksnis = new SimpleXMLElement("<ParskCeturksnis></ParskCeturksnis>");
$ParskCeturksnis->addAttribute("xsi:nil", "true", "http://www.w3.org/2001/XMLSchema-instance");
echo $ParskCeturksnis->asXml();
所有作品
但是如果整个代码
$DOM = new DOMDocument('1.0','UTF-8');
$DeclarationFile = $DOM->createElement('DeclarationFile');
$DOM->appendChild($DeclarationFile);
$Declaration = $DOM->createElement('Declaration');
$DeclarationFile->appendChild($Declaration);
$Declaration_att = $DOM->createAttribute('Id');
$Declaration->appendChild($Declaration_att);
$att_Declaration_text = $DOM->createTextNode('DEC');
$Declaration_att->appendChild($att_Declaration_text);
$DokPVNv4 = $DOM->createElement('DokPVNv4');
$Declaration->appendChild($DokPVNv4);
$ParskMen = '5';
$ParskMen = $DOM->createElement('ParskMen',mb_convert_encoding($ParskMen, "UTF-8") );
$DokPVNv4->appendChild($ParskMen);
$ParskCeturksnis = new SimpleXMLElement("<ParskCeturksnis></ParskCeturksnis>");
$ParskCeturksnis->addAttribute("xsi:nil", "true", "http://www.w3.org/2001/XMLSchema-instance");
echo $ParskCeturksnis->asXml();
echo $DOM->saveXML();
得到错误XML Parsing Error: junk after document element
谷歌搜索,但没有找到解决方案......
试过了
$ParskCeturksnis = $DOM->createElement('ParskCeturksnis');
$DokPVNv4->appendChild($ParskCeturksnis)->addAttribute("xsi:nil", "true", "http://www.w3.org/2001/XMLSchema-instance");
得到XML Parsing Error: no element found