私はその場でXMLファイルを作成する必要があります。私はこのように見ていることになる通常のXMLファイルを作成することは何ら問題はなかった。PHPを使用したオンザフライでのXML作成
<?xml version="1.0" encoding="UTF-8"?>
<root>
<item>
<name></name>
</item>
</root>
をしかし、私の要件は、私は、その出力であるXMLファイルを作成する必要があるようです:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<item>
<name url = "C:\htdocs\proj1\source_file1"/>
<name url = "C:\htdocs\proj1\source_file2"/>
<name url = "C:\htdocs\proj1\source_file3"/>
</item>
</root>
私はこの方法で試しました:
<?php
$domtree = new DOMDocument('1.0', 'UTF-8');
$domtree->formatOutput = true;
$xmlRoot = $domtree->createElement("root");
$xmlRoot = $domtree->appendChild($xmlRoot);
$item = $domtree->createElement("item");
$item = $xmlRoot->appendChild($item);
$name= $domtree->createElement("name");
$name = $item->appendChild($name);
$sav_xml = $domtree->saveXML();
$handle = fopen("new.xml", "w");
fwrite($handle, $sav_xml);
fclose($handle);
?>
しかし私は、私の要素にurl = "path"を追加/追加したかったのです。私は、URLとパスで変数を宣言しようとしたが、これは私のようなエラーがスローされます。この問題にアプローチする方法任意のアイデアを
Uncaught exception 'DOMException' with message 'Invalid Character Error'
を!あなただけのPHPのDOMを経由して属性をその宣言する必要が
おかげ
あなたが属性を設定しようとしているあなたのコードのdoesntのショー。どのような場合でも、XMLで属性を設定する方法は重複しています。例については、http://pastebin.com/U2rNuZQPを参照してください。 – Gordon
[XMLの属性の値を作成および設定する方法は?](http://stackoverflow.com/questions/6226702/how-to-create-and-set-values-for-attribute-in-xml) – Gordon