在PHP节点值和元素中创建的Xml
I do not understand these node value things at all i am trying to replicate an xml design in php but
having quite a bit of trouble the file i am trying to reproduce through php is.
<items>
<item>
<id></id>
<name></name>
<price></price>
<quantity></quantity>
<description></description>
<qonhold></qonhold>
<qsold></qsold>
</item>
</items>
And the PHP file to recreate this is almost all done
$dom = new DOMDocument("1.0");
// create root element
$root = $dom->createElement("Items");
$dom->appendChild($root);
$dom->formatOutput=true;
// create child element
$item = $dom->createElement("item");
$dom->appendChild($item);
// create text node
$id = $dom->createElement("id");
$root->appendChild($id);
$name = $dom->createElement("name");
$root->appendChild($name);
$price = $dom->createElement("price");
$root->appendChild($price);
$quantity = $dom->createElement("quantity");
$root->appendChild($quantity);
$description = $dom->createElement("description");
$root->appendChild($description);
$qonhold = $dom->createElement("qonhold");
$root->appendChild($qonhold);
$qsold = $dom->createElement("qsold");
$root->appendChild($qsold);
The problem i am having is its saving it all under "items" being the root.. but i can not get everything id, name, price, quantity, description, qonhold, qsold to save under just "item" which is saved under "items
我根本不理解这些节点值的东西我试图在php中复制xml设计但是
我试图通过PHP重现的文件有相当多的麻烦。
p>
&lt; items&gt;
&lt; item&gt;
&lt; id&gt;&lt; / id&gt;
&lt; name&gt;&lt; / name&gt; \ n&lt; price&gt;&lt; / price&gt;
&lt; quantity&gt;&lt; / quantity&gt;
&lt; description&gt;&lt; / description&gt;
&lt; qonhold&gt;&lt; / qonhold&gt;
&lt; qsold&gt;&lt; ; / qsold&gt;
&lt; / item&gt;
&lt; / items&gt;
code> pre>
重新创建此文件的PHP文件几乎全部完成 p>
$ dom = new DOMDocument(“1.0”);
//创建根元素
$ root = $ dom-&gt; createElement(“Items”);
$ dom-&gt; appendChild($ root);
$ dom-&gt; formatOutput = true;
//创建子元素
$ item = $ dom-&gt; createElement(“item”);
$ dom-&gt; appendChild($ item);
//创建文本节点
$ id = $ dom-&gt; createElement(“id”);
$ root-&gt; appendChild($ id); \ n
$ name = $ dom-&gt; createElement(“name”);
$ root-&gt; appendChild($ name);
$ price = $ dom-&gt; createElement(“p rice“);
$ root-&gt; appendChild($ price);
$ quantity = $ dom-&gt; createElement(”quantity“);
$ root-&gt; appendChild($ quantity); \ n
$ description = $ dom-&gt; createElement(“description”);
$ root-&gt; appendChild($ description);
$ qonhold = $ dom-&gt; createElement(“qonhold” );
$ root-&gt; appendChild($ qonhold);
$ qsold = $ dom-&gt; createElement(“qsold”);
$ root-&gt; appendChild($ qsold); \ n code> pre>
我遇到的问题是它在“items” i>作为根目录下保存了所有内容..但我无法得到所有内容 id,name,price,quantity,description,qonhold,qsold i>保存在“item” i>下,保存在“items i> p> \下 n div>
You should use ->appendChild()
on the item node created, not the root which is <items>
:
// create child element
$item = $dom->createElement("item");
$dom->appendChild($item);
// create text node
$id = $dom->createElement("id");
$item->appendChild($id); // item->appendChild not $root->appendChild
Should look like this:
$dom = new DOMDocument("1.0");
// create root element
$root = $dom->createElement("Items");
$dom->appendChild($root);
$dom->formatOutput=true;
// create child element
$item = $dom->createElement("item");
$root->appendChild($item); // append to `<Items>`
// create text node
$id = $dom->createElement("id");
$item->appendChild($id); // append to `<item>`
$name = $dom->createElement("name");
$item->appendChild($name); // append to `<item>`
$price = $dom->createElement("price");
$item->appendChild($price); // append to `<item>`
$quantity = $dom->createElement("quantity");
$item->appendChild($quantity); // append to `<item>`
$description = $dom->createElement("description");
$item->appendChild($description); // append to `<item>`
$qonhold = $dom->createElement("qonhold");
$item->appendChild($qonhold); // append to `<item>`
$qsold = $dom->createElement("qsold");
$item->appendChild($qsold); // append to `<item>`