こんにちは私はこれらの3つのトピックをまとめてXMLエントリを更新することに関して多くの質問があることを知っていますが、DOMXmlとXpathを使用してXMLエントリを更新する
私はXPathとその方法を理解しようとしばらく時間を費やしていましたが、私はまだやりたいことを得ることができません。
ここでは、私は私がやりたい何、このXMLファイル
<?xml version="1.0" encoding="UTF-8"?>
<storagehouse xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="schema.xsd">
<item id="c7278e33ef0f4aff88da10dfeeaaae7a">
<name>HDMI Cable 3m</name>
<weight>0.5</weight>
<category>Cables</category>
<location>B3</location>
</item>
<item id="df799fb47bc1e13f3e1c8b04ebd16a96">
<name>Dell U2410</name>
<weight>2.5</weight>
<category>Monitors</category>
<location>C2</location>
</item>
</storagehouse>
を持って
を行くが、私がする必要がある時には、上記/編集にいずれかのノードを更新することです。私はそれのためのHTMLフォームを行います。 しかし、私が一番大切なのは、目的のノードを見つけて更新する方法です。
ここで私はとても一人のユーザーが目的のノードを更新することを決定し、私はそれを見つけることができる、私は
<?php
function fnDOMEditElementCond()
{
$dom = new DOMDocument();
$dom->load('storage.xml');
$library = $dom->documentElement;
$xpath = new DOMXPath($dom);
// I kind of understand this one here
$result = $xpath->query('/storagehouse/item[1]/name');
//This one not so much
$result->item(0)->nodeValue .= ' Series';
// This will remove the CDATA property of the element.
//To retain it, delete this element (see delete eg) & recreate it with CDATA (see create xml eg).
//2nd Way
//$result = $xpath->query('/library/book[author="J.R.R.Tolkein"]');
// $result->item(0)->getElementsByTagName('title')->item(0)->nodeValue .= ' Series';
header("Content-type: text/xml");
echo $dom->saveXML();
}
?>
を行うにしようとしています何のいくつかは、誰かが多分私のように属性としての例を与えることができる持っていますノードをXPathで更新してから更新しますか?
*(ヒント)* http://schlitt.info/opensource/blog/0704_xpath.html – Gordon
'storagehouse'はルート要素('/')ですので、'/storagehouse'を使用しないでください。 '/'だけです。 –